Size in bytes of a dictionary
Martin von Loewis
loewis at informatik.hu-berlin.de
Fri Sep 21 17:30:15 CEST 2001
Don O'Donnell <donod at home.com> writes:
> 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?
I think there are two problems here. One is that mxTools does not take
into account the actual hash table of the dictionary; it only counts
the "basic" size of the object.
So it would suggest that what Beazley reports as a fixed overhead of
24 is actually 44 in the version on which the mxTools sizeof was
In Python 2.2, the fixed overhead of any dictobject will be 124 bytes
(on SPARC 32-bit), since the basic dictobject already contains 8
As for the variable-sized part of the dictobject: Each entry continues
to consume 12 bytes (a long and two pointers), unless
USE_CACHE_ALIGNED is defined. Also, memory consumption continues to
double each time the dictionary is resized. However, the exact point
in time *when* the dictionary is resized depends on different factors;
in general, it will be resized when it is 2/3 full. So Beazley's
formula is a rough estimate only, but it continues to be estimate the
dictionary size reasonably well.
More information about the Python-list