memory leak with dynamically defined functions?

Harald Kirsch kirschh at lionbioscience.com
Tue Jul 17 03:23:39 EDT 2001


Too bad having to followup on my own posting.

Harald Kirsch <kirschh at lionbioscience.com> writes:

> zooko at zooko.com 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()
> 
> This creates 2**13 references to one and the same object, namely
> silliest_func which (I suspect) has just one reference to an object
> inner_silliest_func.

Should read: 
This creates 2**13 references to one and the same object, namely
inner_silliest_func.

> 
> > 
> > and then remove the references to this memory, like this:
> > 
> > >>> del blarg
> 
> This removes the 2**13 references again. And copies of the
> inner_silliest_func were never produced and need not be freed.
> 
> > 
> > none of the memory is freed up!
> 
> The only memory that can and should be freed is the one for the list
> object blarg and this is about
> sizeof(PyListObject)+2**13*sizeof(void*) (see
> Python/Objects/listobject.c/PyList_New). This is roughly 32kBytes on
> 32bit machines. I doubt that you will notice freeing such a rather
> small amount of memory.
> 
> > 
> > Even stimulating the garbage collector, with:
> > 
> > >>> import gc
> > >>> gc.collect()
> > 0
> 
> I guess you see this because `del blarg' did already all that there
> was to do.
> 
> However, from your description it looks like you might have simplified
> your example too such that the true problem cannot be demonstrated.
> 
> Regards,
>   Harald Kirsch
> -- 
> ----------------+------------------------------------------------------
> Harald Kirsch   | kirschh at lionbioscience.com | "How old is the epsilon?"
> LION bioscience | +49 6221 4038 172          |        -- Paul Erdös
>        *** Please do not send me copies of your posts. ***

-- 
----------------+------------------------------------------------------
Harald Kirsch   | kirschh at lionbioscience.com | "How old is the epsilon?"
LION bioscience | +49 6221 4038 172          |        -- Paul Erdös
       *** Please do not send me copies of your posts. ***



More information about the Python-list mailing list