I have a python class
foo that contains:
- data (ints, floats)
- lists (of ints, of floats, and of other objects)
- dictionaries (of ints, of floats, of other objects)
Assuming that there are no back-references (cycles), is there an easy way to measure the total memory usage of a
foo object ?
Essentially, I am looking for a recursive version of
A few of the tools I came across included: heapy, objgraph and gc, but I don’t think any of them are up to the task (I may be corrected on this)
Try Pympler, which describes itself as “A development tool to measure, monitor and analyze the memory behavior of Python objects.”
Something along the lines of
import pympler print pympler.asizeof.asizeof(your_object)
has been helpful to me in the past.
See examples from the official documentation and other questions on stack overflow.
I’m not sure if you’re trying to get the size of the type, or the actual complex object, but looking at getsizeof, the documentation points to this recipe for a robust recursive version:
so I’d assume there’s no ‘stock’ python call that will do it, otherwise the docs would mention it.