I have a python class foo
that contains:
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 sys.getsizeof
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)
To open up Resource Monitor, press Windows Key + R and type resmon into the search box. Resource Monitor will tell you exactly how much RAM is being used, what is using it, and allow you to sort the list of apps using it by several different categories.
The deep\_getsizeof() Function getsizeof() can only tell you how much memory a primitive object takes, let's take a look at a more adequate solution. The deep\_getsizeof() function drills down recursively and calculates the actual memory usage of a Python object graph.
The __sizeof__() function in Python doesn't exactly tell us the size of the object. It doesn't return the size of a generator object as Python cannot tell us beforehand that how much size of a generator is. Still, in actuality, it returns the internal size for a particular object (in bytes) occupying the memory.
We can get an address using the id() function. id() function gives the address of the particular object.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With