Today I Learned


Print memory usage in Python program

The psutil library is great for this, but you’ll probably have to install it with pip. Then it’s as simple as:

import psutil

It will print out a named tuple with total physical memory, available memory, percent, used, free etc like this:

svmem(total=25769803776L, available=8386338816L, percent=67.5, used=22414565376L, free=72933376L, active=8504508416L, inactive=8313405440L, wired=5596651520L)

psutil has a lot of other great functionality too. You can see connected users, look at process information, spawn new processes, look at network connections and much more. It’s worth checking out.