Search code examples
pythonmemory-managementpsutil

psutil doesn't show changes in memory usage


I'm trying to record how memory usage is modified while new objects are created.

The easiest scenario is to create a numpy array and see how does it changes. So my goal is to measure memory status before and after and see how it changes. However, I'm getting the following:

>>> print(psutil.virtual_memory()); a=np.zeros(1024*1024*10, dtype=np.uint8);print(psutil.virtual_memory())
svmem(total=16482779136, available=7692648448, percent=53.3, used=7079854080, free=2953998336, active=9224863744, inactive=3692097536, buffers=516026368, cached=5932900352, shared=1366462464, slab=387022848)
svmem(total=16482779136, available=7692648448, percent=53.3, used=7079854080, free=2953998336, active=9224863744, inactive=3692097536, buffers=516026368, cached=5932900352, shared=1366462464, slab=387022848)

But before and after creating the numpy array, the memory status is exactly the same.

This is a toy example where I was expecting to see a difference of 1010241024=10MB of memory, consumed by the numpy array a, but they are the same.

I'm running it on Ubuntu 18.04 LTS, Python 3.7.7 and psutil 5.8.0.

Thanks in advance.


Solution

  • Memory allocation of Python is smart. Keep memory in advance as heap, and assign what they need. Lacking heap, keep memory again. I said like that roughly. In other words, general method can't measure change of memory. Therefore, there is such a way. You must be exciting? Enjoyment to deal with raw memory. Sadness of fragment pointers. Great!