Why is this a mem leak?

Steve steve at hotmail.com
Thu Jul 22 19:08:46 EDT 2004


Steve wrote:
> Hi,
> 
> 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 = []
>     l.append(l)
>     del l
> 
> ??? 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??
> 
> Thanks,
> 
> Steve
> 

Another question:

I get the following when I set DEBUG in gc to DEBUG_LEAK:

GARBAGE:
gc: uncollectable <JCollector instance at 0x403a7f2c>
gc: uncollectable <NCollector instance at 0x403fa2cc>
gc: uncollectable <dict 0x403a9c64>
gc: uncollectable <dict 0x403f9934>
gc: uncollectable <instancemethod 0x403a5dec>
gc: uncollectable <instancemethod 0x403aa0f4>

this was after calling a method foo() which created two instances of a 
single class, which then created the dictionaries and instancemethods 
named above. After foo() ended, I would have thought that these 
instances/objects would have been collected. I called gc.collect() 
explicitly two times but the gc.garbage list still held the same list as 
'garbage'. Does 'uncollectable' here means that these objects will never 
be collected? Why is that? They're not bound by anything once foo() 
exits. How can I make sure the memory allocated for this junk is deleted 
(since I want to run foo() in a loop). Can someone please tell me what's 
happening? Thanks,

Steve




More information about the Python-list mailing list