Size in bytes of a dictionary

Gerhard Häring g.haering at ___skynamics.com.invalid
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 
win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> import mx.Tools
 >>> dict={"somekey": "somevalue", "anotherkey": 3.1415926535}
 >>> sizeof(dict)
44

Neat, eh?

mxTools has other useful stuff, too: 
http://www.lemburg.com/files/python/mxTools.html

Gerhard
-- 
Gerhard Häring
skynamics AG
g.haering at skynamics.com
http://www.skynamics.com




More information about the Python-list mailing list