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 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]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With