16

Using objgraph, I found a bunch of objects like this:

InstanceState loop

Will Python's garbage collector deal with cycles like this, or will it leak?

A slightly wider view of the loop:

Wider view of InstanceState loop

ali_m
  • 71,714
  • 23
  • 223
  • 298
dbr
  • 165,801
  • 69
  • 278
  • 343

4 Answers4

28

Python's standard reference counting mechanism cannot free cycles, so the structure in your example would leak.

The supplemental garbage collection facility, however, is enabled by default and should be able to free that structure, if none of its components are reachable from the outside anymore and they do not have __del__() methods.

If they do, the garbage collector will not free them because it cannot determine a safe order to run these __del__() methods.

0 _
  • 10,524
  • 11
  • 77
  • 109
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • 9
    It should be stated explicitly that the GC you call "supplemental" is enabled by default in Python – Eli Bendersky Nov 06 '11 at 09:10
  • 3
    Hurrah ! At least, peoples (you and Raymond Hettinger) who don't make confusion about GC and refs counter and who don't use "garbage collector" as synonym of "reference counting mechanism" ! I had been perplexed for a long time about the fact that I had understood things like that but I was constantly reading texts in which peoples expressed their ideas as if the GC was the responsible of the the killing of no-more-referenced objects. In your answer, the difference is perfectly expressed and clear. Thank you. Hence +1 – eyquem Nov 06 '11 at 11:18
  • Since it's still possible to search your way here, note that this answer is no longer correct. `__del__` no longer prevents collection of cycles, and it does get called for cycles. – Glenn Maynard Nov 30 '21 at 18:42
21

To extend on Frédéric's answer a bit, the "reference counts" section of the docs explains the supplementary cycle detection nicely.

Since I find explaining things a good way to confirm I understand it, here are some examples... With these two classes:

class WithDel(object):
    def __del__(self):
        print "deleting %s object at %s" % (self.__class__.__name__, id(self))


class NoDel(object):
    pass

Creating an object and losing the reference from a triggers the __del__ method, thanks to the ref-counting:

>>> a = WithDel()
>>> a = None  # leaving the WithDel object with no references 
deleting WithDel object at 4299615184

If we make a reference loop between two objects with no __del__ method, all is still leak-free, this time thanks to the cycle detection. First, enable the garbage-collection debug output:

>>> import gc
>>> gc.set_debug(gc.DEBUG_COLLECTABLE | gc.DEBUG_UNCOLLECTABLE | gc.DEBUG_OBJECTS)

Then make a reference loop between the two objects:

>>> a = NoDel(); b = NoDel()
>>> a.other = b; b.other = a  # cyclical reference
>>> a = None; b = None # Leave only the reference-cycle
>>> gc.collect()
gc: collectable <NoDel 0x10046ed50>
gc: collectable <NoDel 0x10046ed90>
gc: collectable <dict 0x100376c20>
gc: collectable <dict 0x100376b00>
4
>>> gc.garbage
[]

(the dict is from the objects internal __dict__ attribute)

All is fine, until even one of the objects in the cycle contains a __del__ method:

>>> a = NoDel(); b = WithDel()
>>> a.other = b; b.other = a
>>> a = None; b = None
>>> gc.collect()
gc: uncollectable <WithDel 0x10046edd0>
gc: uncollectable <dict 0x100376b00>
gc: uncollectable <NoDel 0x10046ed90>
gc: uncollectable <dict 0x100376c20>
4
>>> gc.garbage
[<__main__.WithDel object at 0x10046edd0>]

As Paul mentioned, the loop can be broken with a weakref:

>>> import weakref
>>> a = NoDel(); b = WithDel()
>>> a.other = weakref.ref(b)
>>> b.other = a # could also be a weakref

Then when the b reference to the WithDel object is lost, it gets deleted, despite the cycle:

>>> b = None
deleting WithDel object at 4299656848
>>> a.other
<weakref at 0x10045b9f0; dead>

Oh, objgraph would have helpfully indicated the problematic __del__ method like this

dbr
  • 165,801
  • 69
  • 278
  • 343
  • This works for me in python2, however, in python3 I'm unable to reproduce the leak... Has something changed? – kralyk Jan 27 '17 at 09:36
  • 1
    @kralyk I guess this pep is responsible: https://www.python.org/dev/peps/pep-0442/ – ead Jan 15 '18 at 21:58
5

Python's GC is designed to traverse all live objects to locate and eliminate reference cycles with no external references.

You can validate that is what is happening by running gc.collect() and then printing gc.garbage and gc.get_objects.

0 _
  • 10,524
  • 11
  • 77
  • 109
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
2

If you use weakrefs for your parent pointers, then GC will happen normally.

PaulMcG
  • 62,419
  • 16
  • 94
  • 130