[egenix-users] Calc size of Python objects

Dirk Holtwick holtwick at spirito.de
Tue Nov 12 11:16:02 CET 2002


hi,

in mxTools there is a nice function called sizeof() to calculate the 
amount of memory an object uses, but that's only true for simple objects 
like strings and integers I believe. but I'm interested in the total 
amount a complex object uses. is this the right way to do it?

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

BTW, does someone have an idea how to get aware of the total memory 
usage of a running python program within itself? the module "resource" 
doesn't work on my machine.

thanks,
dirk





More information about the egenix-users mailing list