Size in bytes of a dictionary

Gerhard Häring g.haering at
Wed Sep 19 20:49:28 CEST 2001

Xavier Defrang wrote:

> Dear Pythoneers,
> I haven't been able to find a way to get the total size in bytes of a
> dictionary object.  I'm writing an application that deals with a large
> hashtable (a string-string mapping) and I'd like to be able to monitor its
> size in memory.  Is there any heuristic formula I may use depending on the
> number of keys and the way the interpreters allocates memory for the data
> structure?

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 
Type "help", "copyright", "credits" or "license" for more information.
 >>> import mx.Tools
 >>> dict={"somekey": "somevalue", "anotherkey": 3.1415926535}
 >>> sizeof(dict)

Neat, eh?

mxTools has other useful stuff, too:

Gerhard Häring
skynamics AG
g.haering at

More information about the Python-list mailing list