Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Have you tried <a href="http://docs.python.org/library/gc.html#gc.set_debug" rel="noreferrer">gc.set_debug()</a> ?</p> <p>You need to ask yourself simple questions:</p> <ul> <li>Am I using objects with <code>__del__</code> methods? Do I absolutely, unequivocally, need them?</li> <li>Can I get reference cycles in my code? Can't we break these circles before getting rid of the objects?</li> </ul> <p>See, the main issue would be a cycle of objects containing <code>__del__</code> methods:</p> <pre><code>import gc class A(object): def __del__(self): print 'a deleted' if hasattr(self, 'b'): delattr(self, 'b') class B(object): def __init__(self, a): self.a = a def __del__(self): print 'b deleted' del self.a def createcycle(): a = A() b = B(a) a.b = b return a, b gc.set_debug(gc.DEBUG_LEAK) a, b = createcycle() # remove references del a, b # prints: ## gc: uncollectable &lt;A 0x...&gt; ## gc: uncollectable &lt;B 0x...&gt; ## gc: uncollectable &lt;dict 0x...&gt; ## gc: uncollectable &lt;dict 0x...&gt; gc.collect() # to solve this we break explicitely the cycles: a, b = createcycle() del a.b del a, b # objects are removed correctly: ## a deleted ## b deleted gc.collect() </code></pre> <p>I would really encourage you to flag objects / concepts that are cycling in your application and focus on their lifetime: when you don't need them anymore, do we have anything referencing it?</p> <p>Even for cycles without <code>__del__</code> methods, we can have an issue:</p> <pre><code>import gc # class without destructor class A(object): pass def createcycle(): # a -&gt; b -&gt; c # ^ | # ^&lt;--&lt;--&lt;--| a = A() b = A() a.next = b c = A() b.next = c c.next = a return a, b, b gc.set_debug(gc.DEBUG_LEAK) a, b, c = createcycle() # since we have no __del__ methods, gc is able to collect the cycle: del a, b, c # no panic message, everything is collectable: ##gc: collectable &lt;A 0x...&gt; ##gc: collectable &lt;A 0x...&gt; ##gc: collectable &lt;dict 0x...&gt; ##gc: collectable &lt;A 0x...&gt; ##gc: collectable &lt;dict 0x...&gt; ##gc: collectable &lt;dict 0x...&gt; gc.collect() a, b, c = createcycle() # but as long as we keep an exterior ref to the cycle...: seen = dict() seen[a] = True # delete the cycle del a, b, c # nothing is collected gc.collect() </code></pre> <p>If you have to use "seen"-like dictionaries, or history, be careful that you keep only the actual data you need, and no external references to it.</p> <p>I'm a bit disappointed now by <code>set_debug</code>, I wish it could be configured to output data somewhere else than to stderr, but hopefully <a href="http://bugs.python.org/issue5851" rel="noreferrer">that should change soon</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload