[Python-Dev] Int FutureWarnings and other 2.4 TODOs

Tim Peters tim.one at comcast.net
Fri Dec 5 10:50:58 EST 2003

[maybe Thomas Heller]
>> IIUC, doing range(10000000) in a program somewhere allocates 10
>> million integers, and these are *never* freed, they live in the
>> cache forever. Correct?

[Anthony Baxter]
> My understanding was that it was only ints between -5 and +100 that
> were cached forever.

Tiny ints are *shared*:  no matter how many computations return, and data
structures hold, the integer 3, they all point to the same integer object.

Space for all ints (tiny or not) is taken from a dedicated freelist, used
for ints and nothing but ints.  Whenever an int is deallocated, its memory
is added to this dedicated freelist.  Unlike most dedicated freelists in
Python, the int freelist is unbounded in size.  So when you do
range(10000000), and that list goes away, most of the space for the int
objects it held ends up in millions of int objects pushed onto the int
freelist.  They all stay there until the program shuts down (although they
get reused to satisfy requests for new int objects).

floats have a very similar (unbounded and immortal) dedicated freelist, so,
e.g., [abs(0.5) for i in xrange(10000000)] reserves space for 10 million
float objects forever after (although [0.5 for i in xrange(10000000)] only
creates 1 float object -- as is obvious to the most casual observer <wink>).

More information about the Python-Dev mailing list