dbr*_*dbr 14 python garbage-collection memory-leaks objgraph
使用objgraph,我发现了一堆像这样的对象:
Python的垃圾收集器会处理这样的循环,还是会泄漏?
稍微宽一点的循环视图:
Fré*_*idi 25
Python的标准引用计数机制不能释放循环,因此示例中的结构会泄漏.
该补充的垃圾收集设施,但是,在默认情况下启用,并且应该能够释放该结构,如果没有它的组件可达从外面了,他们没有__del__()
方法.
如果他们这样做,垃圾收集器将不会释放它们,因为它无法确定运行这些__del__()
方法的安全顺序.
dbr*_*dbr 19
为了扩展Frédéric的答案,文档的"引用计数"部分很好地解释了补充循环检测.
由于我发现解释事物是确认我理解它的好方法,这里有一些例子......有了这两个类:
class WithDel(object):
def __del__(self):
print "deleting %s object at %s" % (self.__class__.__name__, id(self))
class NoDel(object):
pass
Run Code Online (Sandbox Code Playgroud)
由于引用计数,创建一个对象并丢失引用a
会触发该__del__
方法:
>>> a = WithDel()
>>> a = None # leaving the WithDel object with no references
deleting WithDel object at 4299615184
Run Code Online (Sandbox Code Playgroud)
如果我们在没有 __del__
方法的情况下在两个对象之间进行参考循环,那么这一切仍然是无泄漏的,这次归功于循环检测.首先,启用垃圾收集调试输出:
>>> import gc
>>> gc.set_debug(gc.DEBUG_COLLECTABLE | gc.DEBUG_UNCOLLECTABLE | gc.DEBUG_OBJECTS)
Run Code Online (Sandbox Code Playgroud)
然后在两个对象之间建立一个引用循环:
>>> 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
[]
Run Code Online (Sandbox Code Playgroud)
(dict
来自对象内部__dict__
属性)
一切都很好,直到循环中的一个对象包含一个__del__
方法:
>>> 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>]
Run Code Online (Sandbox Code Playgroud)
正如保罗所说,循环可以用以下方式打破weakref
:
>>> import weakref
>>> a = NoDel(); b = WithDel()
>>> a.other = weakref.ref(b)
>>> b.other = a # could also be a weakref
Run Code Online (Sandbox Code Playgroud)
然后,当对象的b
引用WithDel
丢失时,它会被删除,尽管循环:
>>> b = None
deleting WithDel object at 4299656848
>>> a.other
<weakref at 0x10045b9f0; dead>
Run Code Online (Sandbox Code Playgroud)
哦,objgraph会有用地指出这样有问题的__del__
方法
归档时间: |
|
查看次数: |
6020 次 |
最近记录: |