Ben Chuanlong Du's Blog

It is never too late to learn.

Get Total Physical Memory in Python

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.

Check Resources Used by Processes Using Python

Use the tracemalloc Library

https://docs.python.org/3/library/tracemalloc.html#module-tracemalloc

The tracemalloc module is a debug tool to trace memory blocks allocated by Python. It provides the following information:

  • Traceback where an object was allocated
  • Statistics on allocated memory blocks per filename and per line number: total size, number and average size of allocated memory blocks