9

Does python garbage collector cleans up a compound object if some of its parts are still referenced

e.g.

def foo():
    A = [ [1, 3, 5, 7], [2, 4, 6, 8]]
    return A[1]
B = foo()

Will A[0] be garbage collected?

Is there a way to confirm the same through code?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
comiventor
  • 3,922
  • 5
  • 50
  • 77

2 Answers2

15

Nothing references the list A and the nested list A[0], so yes, they will be deleted from memory.

The nested list object referenced by A[1] has no connection back to its original container.

Note that it's not the garbage collector that does this; the GC only deals in breaking circular references. This simple case is handled entirely by reference counting.

The moment foo() returns, the local namespace is cleared up. That means A is removed, which means the list object reference count drops to 0. This clears that list object, which means that the contained lists also see their reference count drop by one. For A[0] that means the count drops to 0 too and it is cleared.

For the list object referenced by A[1], you now have a reference B to it, so it's count is still 1 and it remains 'alive'.

To confirm the same through code, simply use a subclass of list with a __del__ method to let us know when the object is being deleted:

>>> class DelList(list):
...     def __del__(self):
...         print 'Deleted {}'.format(self)
... 
>>> def foo():
...     A = DelList([DelList([1, 3, 5, 7]), DelList([2, 4, 6, 8])])
...     return A[1]
... 
>>> B = foo()
Deleted [[1, 3, 5, 7], [2, 4, 6, 8]]
Deleted [1, 3, 5, 7]
>>> del B
Deleted [2, 4, 6, 8]

All this is specific to CPython (the reference Python implementation); other implementations may handle object lifetimes differently (e.g. do use a garbage collector to destroy objects in sweeps), but the lifetime of A and A[0] doesn't change in those cases; the GC will still collect those in other implementations, albeit at a different point in time perhaps.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 2
    Of course, all of this is CPython-specific. Also, inserting an `__del__` can stop the "real" garbage collector from collecting the object. – Fred Foo Feb 27 '14 at 14:06
  • @larsmans: yes, a set of objects involved in a reference cycle where one of them has a `__del__` method will stop the GC from breaking that cycle. – Martijn Pieters Feb 27 '14 at 14:07
1

Martijn has explained that A[0] will be collected, and here is how to observe it with code:

class Bye(object):
   def __del__(self):
      """A special method, called when the object is destroyed."""
      print 'bye'

def foo():
    A = [Bye(), [1,2]]
    return A[1]

foo()

Prints:

bye
[1, 2]
Peter Westlake
  • 4,894
  • 1
  • 26
  • 35