Why is this a mem leak?
Mel Wilson
mwilson at the-wire.com
Fri Jul 23 07:24:24 EDT 2004
In article <410044cb$1 at clarion.carno.net.au>, Steve <steve at hotmail.com> wrote:
>I saw an article online
>(http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65333) where it
>explains how one can find out if there's a memory leak in ones program.
>How is there a memory leak in this:
>
> # make a leak
> l = []
1 reference to the new list, in the namespace
under 'l'. Reference count == 1
> l.append(l)
1 more reference to the list, in its own
[0] element. Reference count == 2
> del l
The reference in the namespace is removed.
Reference count = 1
>??? Shouldn't deleting an object explicitly give the GC a better chance
>to find the object? Won't deleting it cause it's reference to go down
>(another question: which GC is python 2.3 using? Refcount or
>Generational?). Isn't 'del' similar to 'free'ing in C??
Not really. `del` removes a reference, which usually
culminates in freeing the storage, after all the required
dels are done.
For no memory leak:
l = [] # ref count = 1, i.e. l
l.append (l) # ref count = 2, i.e. l and :itself:[0]
m = l # ref count = 3, i.e. l, :itself:[0] and m
del l # ref count = 2, i.e. :itself:[0] and m
del m[0] # ref count = 1, i.e. m
del m # ref count = 0, and the objects space can be collected
(Here :itself: is made-up language, no way a valid Python
identifier, but how else to discuss a reference that isn't
part of any namespace?)
Regards. Mel.
More information about the Python-list
mailing list