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.
import osos.sysconf("SC_PAGE_SIZE") * os.sysconf("SC_PHYS_PAGES")67327844352Parse the File /proc/meminfo¶
!head /proc/meminfoMemTotal: 65749848 kB
MemFree: 632084 kB
MemAvailable: 52502788 kB
Buffers: 1528080 kB
Cached: 47313580 kB
SwapCached: 60 kB
Active: 18386936 kB
Inactive: 40492096 kB
Active(anon): 5928 kB
Inactive(anon): 10750316 kB
Get physical memory in KB.
with Path("/proc/meminfo").open() as fin:
line = fin.readline()
if line.startswith("MemTotal:"):
print(int(line[9:-3]))65749848
65749848 * 102467327844352Using psutil¶
import psutilpsutil.virtual_memory()svmem(total=67327844352, available=53696008192, percent=20.2, used=12146085888, free=561696768, active=18897117184, inactive=41479438336, buffers=1565102080, cached=53054959616, shared=806260736, slab=5218934784)psutil.virtual_memory().total67327844352Of 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 .