Ciaran
Ciaran

Reputation: 573

How do I measure the memory usage of an object in python?

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)

Upvotes: 15

Views: 7070

Answers (2)

Scott Griffiths
Scott Griffiths

Reputation: 21925

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.

Upvotes: 24

Oren Mazor
Oren Mazor

Reputation: 4487

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:

http://code.activestate.com/recipes/577504/

so I'd assume there's no 'stock' python call that will do it, otherwise the docs would mention it.

Upvotes: 0

Related Questions