Size in bytes of a dictionary

Don O'Donnell donod at home.com
Wed Sep 19 16:00:43 EDT 2001


Gerhard Häring wrote:
> 
> The mxTools package adds a new builtin function sizeof, that might do
> what you want:
> 
> ActivePython 2.1, build 211 (ActiveState)
> based on Python 2.1 (#15, Jun 18 2001, 21:42:28) [MSC 32 bit (Intel)] on
> win32
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> import mx.Tools
>  >>> dict={"somekey": "somevalue", "anotherkey": 3.1415926535}
>  >>> sizeof(dict)
> 44
> 
> Neat, eh?
> 

Yes, but that does not take into account the memory allocated to the
strings themselves.  I assume Xavier was interested in the total amount
of memory allocated, by his remark: 

"... I'd like to be able to monitor its size in memory. "

Also, the mxTools sizeof function does not agree with the memory size
formula for dictionaries as given in Beazley's first edition, which I
quoted in my previous posting:

24 + 12*2**n, n = log2(2)+1  =>  72

not 44.

Perhaps there's a more correct formula in the Second Edition.  Can
anyone help here?

Cheers,
Don



More information about the Python-list mailing list