[Mojonation-devel] Re: memory leak with dynamically defined functions?

Roman Suzi rnd at onego.ru
Mon Jul 16 23:28:32 EDT 2001

I've run the following:

import gc

def silliest_func():
    x = [0] * (2**10)
    def inner_silliest_func(x=x):
    return inner_silliest_func

while 1:
    blarg = {}
    for i in range(2**13):
        blarg[i] = silliest_func()
    print gc.collect()

(Python 2.1, glibc 2.1.3, Linux 2.2.)

- and then watched 'top'. Memory jumps from 1.5Mb
(normal for empty Python) up to 35Mb.
and zeros are printed: refcount probably works fine.

>"Gregory P . Smith" <greg at mad-scientist.com> writes:
>> It doesn't appear to leak memory for me either.  The difference as
>> zooko noticed does show up though:
>> His first example that didn't use a function holding a reference via
>> default parameters -immediately- released the consumed memory when the
>> reference to blarg was deleted.
>> The version above that uses the function reference doesn't seem to
>> ever release that memory even when the reference to blarg has been
>> deleted.  (yet it doesn't go above 34M when I put it in a while 1 loop
>> as you have)
>That could just be a behavior of the underlying memory allocation from
>the OS.  Depending on OS (and if variable, allocation implementation),
>an application might not actually yield its working set memory back to
>the system, but just reuse it within its own memory space as necessary.
>>From the behavior described, I'd guess that the allocation system in
>use on the test system was willing to give back memory if it was
>possible to do so contiguously at the top of the currently allocated
>space for the application in simple cases, but not in other cases
>(where the space becomes more fragmented).
>This isn't really a Python issue (I expect Python has properly freed
>all relevant memory), but more a platform memory allocation issue.
>-- David

Sincerely yours, Roman Suzi
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at onego.ru _/
_/ Tuesday, July 17, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "...put knot yore trust inn spel chequers." _/

More information about the Python-list mailing list