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.
You could read /proc/meminfo . /proc/meminfo should be available on pretty much all linux installs. You don't need /proc/meminfo because sysconf has the answer.
Press Ctrl + Shift + Esc to launch Task Manager. Or, right-click the Taskbar and select Task Manager. Select the Performance tab and click Memory in the left panel. The Memory window lets you see your current RAM usage, check RAM speed, and view other memory hardware specifications.
You can use it by putting the @profile decorator around any function or method and running python -m memory_profiler myscript. You'll see line-by-line memory usage once your script exits.
CPU Information psutil's cpu_count() function returns number of cores, whereas cpu_freq() function returns CPU frequency as a namedtuple including current, min, and max frequency expressed in Mhz, you can set percpu=True to get per CPU frequency.
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.
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_SIZE
is often 4096.SC_PAGESIZE
and SC_PAGE_SIZE
are equal.man sysconf
.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With