Measuring memory usage in Python: it’s tricky!
If you want your program to use less memory, you will need to measure memory usage.
You’ll want to measure the current usage, and then you’ll need to ensure it’s using less memory once you make some improvements.
It turns out, however, that measuring memory usage isn’t as straightforward as you’d think.
Even with a highly simplified model of how memory works, different measurements are useful in different situations.
In this article you’ll learn:
- A simplified but informative model of how memory works.
- Two measures of memory–resident memory and allocated memory–and how to measure them in Python.
- The tradeoffs between the two.
A (not so) simple example
Consider the following code:
>>> import numpy as np