How to get memory size/usage of python object

Tim Chase python.list at tim.thechases.com
Wed Jan 9 12:49:36 EST 2008


Sion Arrowsmith wrote:
> Santiago  Romero  <sromero at gmail.com> wrote:
>> Is there a way to check the REAL size in memory of a python object?
>>
>> Something like
>>
>>> print sizeof(mylist)
>> [ ... ]
> 
> Would you care to precisely define "REAL size" first? Consider:
> 
>>>> atuple = (1, 2)
>>>> mylist = [(0, 0), atuple]
> 
> Should sizeof(mylist) include sizeof(atuple) ?
> 
>>>> del atuple
> 
> What about now, when mylist has the only reference to the (1, 2)
> object that also used to be referred to as atuple?

or add to the mix

   >>> mylist = [(0,0), atuple] * 1000

where the same atuple is referenced 1000 times.  And then if you

   >>> del atuple

defining "sizeof()" becomes even more peculiar if you have a 
thousand things that "have" the same item that nothing else 
claims ownership of.

Or, if you have this:

   >>> alist = [1,2,3]
   >>> mylist = ['a', 'b', alist] * 10
   >>> s1 = sizeof(mylist)
   >>> alist.append(42)
   >>> s2 = sizeof(mylist)

should s1==s2 ?

-tkc





More information about the Python-list mailing list