How can I get the total physical memory within Python in a distribution agnostic fashion? I don’t need used memory, just the total physical memory.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
your best bet for a cross-platform solution is to use the psutil package (available on PyPI).
import psutil
psutil.virtual_memory().total # total physical memory in Bytes
Documentation for virtual_memory is here.
Method 2
Using os.sysconf on Linux:
import os
mem_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') # e.g. 4015976448
mem_gib = mem_bytes/(1024.**3) # e.g. 3.74
Note:
SC_PAGE_SIZEis often 4096.SC_PAGESIZEandSC_PAGE_SIZEare equal.- For more info, see
man sysconf. - For MacOS, as per user reports, this works with Python 3.7 but not with Python 3.8.
Using /proc/meminfo on Linux:
meminfo = dict((i.split()[0].rstrip(':'),int(i.split()[1])) for i in open('/proc/meminfo').readlines())
mem_kib = meminfo['MemTotal'] # e.g. 3921852
Method 3
Regular expressions work well for this sort of thing, and might help with any minor differences across distributions.
import re
with open('/proc/meminfo') as f:
meminfo = f.read()
matched = re.search(r'^MemTotal:s+(d+)', meminfo)
if matched:
mem_total_kB = int(matched.groups()[0])
Method 4
This code worked for me without any external library at Python 2.7.9
import os
mem=str(os.popen('free -t -m').readlines())
"""
Get a whole line of memory output, it will be something like below
[' total used free shared buffers cachedn',
'Mem: 925 591 334 14 30 355n',
'-/+ buffers/cache: 205 719n',
'Swap: 99 0 99n',
'Total: 1025 591 434n']
So, we need total memory, usage and free memory.
We should find the index of capital T which is unique at this string
"""
T_ind=mem.index('T')
"""
Than, we can recreate the string with this information. After T we have,
"Total: " which has 14 characters, so we can start from index of T +14
and last 4 characters are also not necessary.
We can create a new sub-string using this information
"""
mem_G=mem[T_ind+14:-4]
"""
The result will be like
1025 603 422
we need to find first index of the first space, and we can start our substring
from from 0 to this index number, this will give us the string of total memory
"""
S1_ind=mem_G.index(' ')
mem_T=mem_G[0:S1_ind]
print 'Summary = ' + mem_G
print 'Total Memory = ' + mem_T +' MB'
Easily we can get the Used Memory and Free Memory
"""
Similarly we will create a new sub-string, which will start at the second value.
The resulting string will be like
603 422
Again, we should find the index of first space and than the
take the Used Memory and Free memory.
"""
mem_G1=mem_G[S1_ind+8:]
S2_ind=mem_G1.index(' ')
mem_U=mem_G1[0:S2_ind]
mem_F=mem_G1[S2_ind+8:]
print 'Used Memory = ' + mem_U +' MB'
print 'Free Memory = ' + mem_F +' MB'
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0