Garbage collection

Tom Wright tew24 at spam.ac.uk
Wed Mar 21 11:03:17 EDT 2007


skip at pobox.com wrote:
> You haven't forgotten to do anything.  Your attempts at freeing memory are
> being thwarted (in part, at least) by Python's int free list.  I believe
> the int free list remains after the 10M individual ints' refcounts drop to
> zero. The large storage for the list is grabbed in one gulp and thus
> mmap()d I believe, so it is reclaimed by being munmap()d, hence the drop
> from 320+MB to 250+MB.
> 
> I haven't looked at the int free list or obmalloc implementations in
> awhile, but if the free list does return any of its memory to the system
> it probably just calls the free() library function.  Whether or not the
> system actually reclaims any memory from your process is dependent on the
> details of themalloc/free implementation's details.  That is, the behavior
> is outside Python's control.

Ah, thanks for explaining that.  I'm a little wiser about memory allocation
now, but am still having problems reclaiming memory from unused objects
within Python.  If I do the following:

>>>
(memory use: 15 MB)
>>> a = range(int(4e7))
(memory use: 1256 MB)
>>> a = None
(memory use: 953 MB)

...and then I allocate a lot of memory in another process (eg. open a load
of files in the GIMP), then the computer swaps the Python process out to
disk to free up the necessary space.  Python's memory use is still reported
as 953 MB, even though nothing like that amount of space is needed.  From
what you said above, the problem is in the underlying C libraries, but is
there anything I can do to get that memory back without closing Python?

-- 
I'm at CAMbridge, not SPAMbridge



More information about the Python-list mailing list