Garbage collection

skip at pobox.com skip at pobox.com
Wed Mar 21 09:49:33 EDT 2007


    Tom> I suspect I may be missing something vital here, but Python's
    Tom> garbage collection doesn't seem to work as I expect it to.  Here's
    Tom> a small test program which shows the problem on python 2.4 and 2.5:

    Tom> (at this point, Python is using 15MB)

    >>> a = range(int(1e7))
    >>> a = None
    >>> import gc
    >>> gc.collect()
    0

    Tom> (at this point, Python is using 252MB)

    Tom> Is there something I've forgotten to do? Why is Python still using
    Tom> such a lot of memory?

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 the
malloc/free implementation's details.  That is, the behavior is outside
Python's control.

Skip



More information about the Python-list mailing list