memory leak with dynamically defined functions?

Guido van Rossum guido at
Tue Jul 17 09:29:11 EDT 2001

zooko at writes:

> But if I allocate memory and store a reference to it in a default 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()
> and then remove the references to this memory, like this:
> >>> del blarg
> none of the memory is freed up!

Hi Zooko,

How do you know that the memory isn't feed up?  I've tried this with
2.0 and with the current CVS, and it doesn't leak.

If you're looking at process size, there are lots of good reasons why
the process size doesn't go down -- but if you do this in a loop,
you'll see it doesn't grow:

    >>> def ps(flags = "-l"):
            import os
            os.system("ps -p %d %s" % (os.getpid(), flags))

    >>> def f():
            x = [0]*2**10
            def g(x=x): return x
            return g

    >>> while 1:
            b = {}
            for i in range(2**13): b[i] = f()

Could it be that the real code that does blow up have more intricate
behavior that isn't captured by your example?  What Python version are
you using where you have observed this in your server?

--Guido van Rossum (home page:

More information about the Python-list mailing list