views:

2007

answers:

3

How would you go about finding out how much memory is being used by an object in python?

I know it is possible to find out how much is used by a block of code, but not by an instantiated object anytime in its life, which is what I want.

EDIT: decided to go with the stats that task manager gives me. Based on the strong evidence that what I want to do is impossible without modifying python, both from fserb and from the heapy documentation.

+3  A: 

I haven't any personal experience with either of the following, but a simple search for a "Python [memory] profiler" yield:

  • PySizer, "a memory profiler for Python," found at http://pysizer.8325.org/. However the page seems to indicate that the project hasn't been updated for a while, and refers to...

  • Heapy, "support[ing] debugging and optimization regarding memory related issues in Python programs," found at http://guppy-pe.sourceforge.net/#Heapy.

Hope that helps.

jcsalterego
+9  A: 

There's no easy way to find out the memory size of a python object. One of the problems you may find is that Python objects - like lists and dicts - may have references to other python objects (in this case, what would your size be? The size containing the size of each object or not?). There are some pointers overhead and internal structures related to object types and garbage collection. Finally, some python objects have non-obvious behaviors. For instance, lists reserve space for more objects than they have, most of the time; dicts are even more complicated since they can operate in different ways (they have a different implementation for small number of keys and sometimes they over allocate entries).

There is a big chunk of code out there to try to best approximate the size of a python object in memory. There's also some simpler approximations. But they will always be approximations.

You may also want to check some old description about PyObject (the internal C struct that represents virtually all python objects).

fserb
A: 

objgraph looks interesting: http://mg.pov.lt/objgraph/