问题描述
如果复合对象的某些部分仍然被引用,python 垃圾收集器是否会清理它
Does python garbage collector cleans up a compound object if some of its parts are still referenced
例如
def foo():
A = [ [1, 3, 5, 7], [2, 4, 6, 8]]
return A[1]
B = foo()
A[0]
会被垃圾回收吗?
有没有办法通过代码确认?
Is there a way to confirm the same through code?
推荐答案
Nothing 引用列表 A
和嵌套列表 A[0]
,所以是的,它们会从内存中删除.
Nothing references the list A
and the nested list A[0]
, so yes, they will be deleted from memory.
A[1]
引用的嵌套列表对象与原始容器没有连接.
The nested list object referenced by A[1]
has no connection back to its original container.
请注意,执行此操作的不是垃圾收集器;GC 只处理破坏循环引用.这种简单的情况完全由引用计数来处理.
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.
foo()
返回的那一刻,本地命名空间被清除.这意味着 A
被删除,这意味着列表对象引用计数下降到 0.这将清除该列表对象,这意味着 包含 列表也看到它们的引用计数下降一.对于 A[0]
这意味着计数也下降到 0 并被清除.
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.
对于由 A[1]
引用的列表对象,您现在有一个对它的引用 B
,所以它的计数仍然是 1 并且它仍然是活动的".
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'.
要通过代码确认相同,只需使用带有 list 的子类.__del__">__del__
方法 让我们知道对象何时被删除:
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]
所有这些都特定于 CPython(参考 Python 实现);其他实现可能会以不同的方式处理对象的生命周期(例如,确实使用垃圾收集器在扫描中销毁对象),但 A
和 A[0]
的生命周期不会改变那些案件;GC 仍会在其他实现中收集它们,尽管可能在不同的时间点.
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.
这篇关于复合对象上的python垃圾收集器行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!