Why custom objects take so much memory?

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Wed Dec 19 00:06:53 EST 2007


On Tue, 18 Dec 2007 21:13:14 +0100, Hrvoje Niksic wrote:

> Each object takes 36 bytes itself: 4 bytes refcount + 4 bytes type ptr +
> 4 bytes dict ptr + 4 bytes weakptr + 12 bytes gc overhead.  That's not
> counting malloc overhead, which should be low since objects aren't
> malloced individually.  Each object requires a dict, which consumes
> additional 52 bytes of memory (40 bytes for the dict struct plus 12 for
> gc).  That's 88 bytes per object, not counting malloc overhead.

And let's not forget that if you're running on a 64-bit system, you can 
double the size of every pointer.

Is there a canonical list of how much memory Python objects take up? Or a 
canonical algorithm?

Or failing either of those, a good heuristic?


> Then there's string allocation: your average string is 6 chars long; add
> to that one additional char for the terminating zero.

Are you sure about that? If Python strings are zero terminated, how does 
Python deal with this?

>>> 'a\0string'[1]
'\x00'




-- 
Steven



More information about the Python-list mailing list