12

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 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)

Martin Thoma
  • 108,021
  • 142
  • 552
  • 849
Ciaran
  • 533
  • 4
  • 14
  • 1
    What is your end goal for this? Would a generic code profiler be enough? – Daenyth Feb 16 '11 at 22:12
  • My python code is using a lot of memory. After ruling out a memory leak, I am trying to track the memory usage of the code over a few hours in order to see where the increase in memory is coming from. – Ciaran Feb 16 '11 at 22:28
  • 3
    Depending on your goal (as above), maybe looking at the pickled size would a good indication of memory size. – xioxox Feb 16 '11 at 22:32

2 Answers2

23

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.

user5994461
  • 3,886
  • 27
  • 43
Scott Griffiths
  • 20,729
  • 8
  • 54
  • 84
1

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.

Oren Mazor
  • 4,391
  • 2
  • 26
  • 28
  • Did you just google "Recursive version sys.getsizeof()" ? :) I hate it when the solution is so obvious!...thanks, will try it out. – Ciaran Feb 16 '11 at 22:29
  • well, I played around in IDLE for like five minutes. I've never looked at memory stuff with python, so this was a cool crash course, but yeah, that's straight out of the official docs. good luck! – Oren Mazor Feb 16 '11 at 22:44