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

Gregory P . Smith greg at mad-scientist.com
Mon Jul 16 18:23:33 EDT 2001


On Mon, Jul 16, 2001 at 03:12:19PM -0700, Neil Schemenauer wrote:
> Skip Montanaro wrote:
> >     zooko> But if I allocate memory and store a reference to it in a default
> >     zooko> argument to an inner function, like this:
> > 
> >     >>> def silliest_func():
> >     >>>     x = [0] * (2**10)
> >     >>>     def inner_silliest_func(x=x):
> >     >>>         pass
> >     >>>     return inner_silliest_func
> >     >>> 
> >     >>> blarg = {}
> >     >>> for i in range(2**13):
> >     >>>     blarg[i] = silliest_func()
> > 
> >     zooko> and then remove the references to this memory, like this:
> > 
> >     >>> del blarg
> > 
> >     zooko> none of the memory is freed up!
> 
> If I do this:
> 
>     while 1:
>         blarg = {}
>         for i in range(2**13):
>             blarg[i] = silliest_func()
> 
> the Python process size seems to remain constant.  Are you sure this
> leaks memory?  I'm testing with Python 1.5.2 BTW.
> 
>   Neil

[Removed mojonation-devel from the CC list]

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)

I tested with python 2.1.1c1.

Greg




More information about the Python-list mailing list