Memory sizes of python objects?

Peter Hansen peter at engcorp.com
Sun Mar 24 14:40:17 EST 2002


Erno Kuusela wrote:
> 
> In article <3C9E06A0.EC4D51E3 at engcorp.com>, Peter Hansen
> | Keeping in mind Python's propensity for finding already-existing
> | entities and binding to them instead of creating new objects.
> 
> | The difference in memory consumption between creating a list
> | of size 1,000,000 filled with 0's and the same size list filled
> | with integers from 0 to 999,999 is rather large...
> 
> you make it sound more mysterious than it is - only numbers under
> 100 and identifier-like string literals get interned, afaik.

So you mean if I do the following, the first one produces only
a million references to the value 51, while the second one produces
a million instances of 102 plus the million individual references?
I don't get that behaviour.  On my machine, with Python 2.2, for 
all intents and purposes the memory allocated stays constant.
Did I overlook something?

>>> b = 1
>>> a = [0] * 1000000
>>> for i in xrange(len(a)): a[i] = b*51
...
>>> b = 2
>>> for i in xrange(len(a)): a[i] = b*51
...

-Peter



More information about the Python-list mailing list