How to free memory ( ie garbage collect) at run time with Python2.5.1(windows)

Delaney, Timothy (Tim) tdelaney at avaya.com
Tue Aug 28 20:02:44 EDT 2007


Alex Martelli wrote:

> rfv-370 <robert.vergnes at yahoo.fr> wrote:
> 
>> have made the following small test:
>> 
>> Before starting my test my UsedPhysicalMemory(PF): 555Mb
>> 
>>>>> tf=range(0,10000000)    PF: 710Mb ( so 155Mb for my List)
>>>>> tf=[0,1,2,3,4,5]             PF: 672Mb (Why? Why the remaining
>>>>> 117Mb is not freed?) del tf                            PF: 672Mb
>>>>> (unused memory not freed)
> 
> Integer objects that are once generated are kept around in a "free
> list" against the probability that they might be needed again in the
> future (a few other types of objects similarly keep a per-type
> free-list, but I think int is the only one that keeps an unbounded
> amount of memory there).  Like any other kind of "cache", this
> free-list (in normal cases) hoards a bit more memory than needed, but
> results in better runtime performance; anomalous cases like your
> example can however easily "bust" this too-simple heuristic.

In particular, the *memory* for integer objects is kept around - the
free list will be as big as the total number of integer objects that
exist simultaneously (with a few caveats, but it's close enough). Small
integers are pre-created and cached, and don't use the free list - off
the top of my head, from -5..255 now to cover the entire bytes range,
plus a few negatives, but that depends on the version of python.

When you call range(), you create that number of integer objects
simultaneously, and so you have that amount of memory allocated for use
by other integers for the lifetime of the python process.

If on the other hand you only created one of those integers at a time
(e.g. by iterating over the result from an xrange() call) you would only
be using (continually re-using) a single entry in the free list in total
and the python memory usage would be much smaller.

Tim Delaney



More information about the Python-list mailing list