Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

python garbage collector behavior on compound objects

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...