How to measure the memory cost in Python?

Jean MrJean1 at gmail.com
Fri May 1 20:50:37 EDT 2009


On May 1, 12:50 pm, Jean <MrJe... at gmail.com> wrote:
> On May 1, 10:56 am, CTO <debat... at gmail.com> wrote:
>
>
>
> > > sys.getsizeof() [a suggested solution] isn't platform-specific.
>
> > So, to answer the OP's question, you'd just do something like
>
> > def get_totalsize(obj):
> >         total_size = sys.getsizeof(obj)
> >         for value in vars(obj).values():
> >                 try: total_size += get_total_size(value)
> >                 except: total_size += sys.getsizeof(value)
> >         return totalSize
>
> > def get_current_size(env):
> >         size = 0
> >         for value in env.values():
> >                 try: size += get_total_size(value)
> >                 except: pass
> >         return size
>
> > get_current_size(vars())
>
> > and discount the weight of the interpreter?
>
> Keep in mind, sys.getsizeof(obj) returns only the size of the given
> object.  Any referenced objects are not included.  You can get the
> latter from gc.get_referents(obj).
>
> /Jean Brouwers
>
> PS) The asizeof(obj) function from this recipe <http://
> code.activestate.com/recipes/546530> does size the object plus its
> references, recursively.

Correction, the last sentence should be: The asizeof(obj) ... plus its
referents, recursively.



More information about the Python-list mailing list