Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
Using os.sysconf
¶
Notice that this ways only works on Linux but not on macOS or Windows.
Get physical memory in bytes.
In [1]:
import os
In [5]:
os.sysconf("SC_PAGE_SIZE") * os.sysconf("SC_PHYS_PAGES")
Out[5]:
Parse the File /proc/meminfo¶
In [7]:
!head /proc/meminfo
Get physical memory in KB.
In [9]:
with Path("/proc/meminfo").open() as fin:
line = fin.readline()
if line.startswith("MemTotal:"):
print(int(line[9:-3]))
In [12]:
65749848 * 1024
Out[12]:
Using psutil¶
In [10]:
import psutil
In [11]:
psutil.virtual_memory()
Out[11]:
In [13]:
psutil.virtual_memory().total
Out[13]:
Of course, psutil is much more powerful. It is capable of getting process-level resource consumption. For more discussion, please refer to Hands on the psutil Module in Python .
In [ ]: