[egenix-users] Calc size of Python objects

Dirk Holtwick holtwick at spirito.de
Tue Nov 12 12:12:31 CET 2002


So this one would be more accurate? If I understand you right, it's not 
possible to calculate the exact amount of memory used by a python object?

class myPyObject:
     pass

def calcsize(i, s=0):
     s = sizeof(i)
     if type(i) == type({}):
         for k, v in i.items():
             s += calcsize(k)
             s += calcsize(v)
             s += 2 * sizeof(myPyObject())
     elif type(i) == type([]):
         for v in i:
             s += calcsize(v)
             s += 1 * sizeof(myPyObject())
     elif type(i) == type((1,)):
         for v in i:
             s += calcsize(v)
     return s

I don't know exctly how to determine the size of a PyObject.

The proc.py is exactly what I was looking for and it's working fine. 
Thanks a lot Marc-Andre! Helps me tracking down some strange memory bugs 
in my application.

yours, dirk




More information about the egenix-users mailing list