Strange behavior of psutil

329 views Asked by At

I'm using the library psutil for python 2.7.

Consider this small program :

import os
import psutil

# return the memory usage in MB
process = psutil.Process(os.getpid())
print process.memory_info().rss / float(2 ** 20)

The memory informations returned by the program are always different. I can't understand how my application, doing all the time the same stuff (here, pretty much nothing) cannot have the same memory footprint at each execution.

Example (each line is a different execution) :

  • 10.37109375
  • 10.37109375
  • 10.359375
  • 10.41015625
  • 10.4140625
  • 10.30078125

Am I missing something here ?

1

There are 1 answers

3
jcoppens On BEST ANSWER

Measuring memory occupation is like taking a picture of someone in movement. The ps utils do not always see the same size of your script+variables, a some of the parts are not always visible. Classes get instantiated and are then destroyed. Variables are allocated, then freed again.

As the documentation says, the 'garbage' collector which is common to dynamically typed languages such as Python, runs at certain times which are not always synchronous to your measuring interval, so you see variations.

Worrysome would be if the memory footprint would be increasing continually - in that case we'd have a memory 'leakage' (eg. this could happen if there is a recursive call occupying ever-increasing resources).

The times that a program behaves predictably are long over (unless you write programs for microcontrollers). The programs run in multitasking environments, where programs and operating system are continually (and simultaneously) fighting for the computer's resources.

But modern programs, such as Python, Ruby, Perl and others, don't act the same with each execution. They are influenced by the memory they have available (which varies because of the other programs which are running).

And mind, it's not a 5-line program you are testing. Take just the line process = psutil.Process(os.getpid()). It calls os.getpid(), which calls the operating system's getpid(). The result goes into psutil.Process which probably calls a few hundred lines of code. Finally, the innocent-looking process = has to call the memory manager to reserve memory for process, and then transfer the result there...