这是内存泄漏吗?

5 python garbage-collection memory-leaks

我正在使用gc模块来调试泄漏.

这是一个gui程序,我已将此功能挂钩到一个按钮.

我已将set debug更多设置为 gc.SAVE_ALL

> gc.collect()
> 
> print gc.garbage
Run Code Online (Sandbox Code Playgroud)

这是输出

[(<type '_ctypes.Array'>,), {'__module__': 'ctypes._endian', '__dict__': <attribute '__dict__' of 'c_int_Array_3' objects>, '__weakref__': <attribute '__weakref__' of 'c_int_Array_3' objects>, '_length_': 3, '_type_': <class 'ctypes.c_int'>, '__doc__': None}, <class 'ctypes._endian.c_int_Array_3'>, <attribute '__dict__' of 'c_int_Array_3' objects>, <attribute '__weakref__' of 'c_int_Array_3' objects>, (<class 'ctypes._endian.c_int_Array_3'>, <type '_ctypes.Array'>, <type '_ctypes._CData'>, <type 'object'>), (<type '_ctypes.CFuncPtr'>,), {'__module__': 'ctypes', '__dict__': <attribute '__dict__' of '_FuncPtr' objects>, '__weakref__': <attribute '__weakref__' of '_FuncPtr' objects>, '_flags_': 1, '__doc__': None, '_restype_': <class 'ctypes.c_int'>}, <class 'ctypes._FuncPtr'>, <attribute '__dict__' of '_FuncPtr' objects>, <attribute '__weakref__' of '_FuncPtr' objects>, (<class 'ctypes._FuncPtr'>, <type '_ctypes.CFuncPtr'>, <type '_ctypes._CData'>, <type 'object'>), {}, <cell at 0x10a24b0: Resource object at 0x10e6a50>, <cell at 0x10a2478: dict object at 0x11a4440>, <cell at 0x7f1703949f68: function object at 0x10ec7d0>, <cell at 0x10e2f30: NoneType object at 0x826880>, <cell at 0x10e2d70: NoneType object at 0x826880>, <cell at 0x10e2ef8: str object at 0x7f1703dd5e10>, <cell at 0x10e2de0: dict object at 0x118aaa0>, {'Accept': 'application/json', 'User-Agent': 'couchdb-python 0.6'}, (<cell at 0x10e2f30: NoneType object at 0x826880>, <cell at 0x10a24b0: Resource object at 0x10e6a50>, <cell at 0x10e2de0: dict object at 0x118aaa0>, <cell at 0x10a2478: dict object at 0x11a4440>, <cell at 0x10e2d70: NoneType object at 0x826880>, <cell at 0x10e2ef8: str object at 0x7f1703dd5e10>, <cell at 0x7f1703949f68: function object at 0x10ec7d0>), <function _make_request at 0x10ec7d0>, (1,), {}, <cell at 0x10e2bb0: Resource object at 0x10e6a50>, <cell at 0x10e2e88: dict object at 0x119f360>, <cell at 0x10f0130: function object at 0x10ec578>, <cell at 0x10f01d8: NoneType object at 0x826880>, <cell at 0x10f01a0: NoneType object at 0x826880>, <cell at 0x10f00f8: str object at 0x7f170b05d810>, <cell at 0x10f00c0: dict object at 0x11969a0>, {'Accept': 'application/json', 'User-Agent': 'couchdb-python 0.6'}, (<cell at 0x10f01d8: NoneType object at 0x826880>, <cell at 0x10e2bb0: Resource object at 0x10e6a50>, <cell at 0x10f00c0: dict object at 0x11969a0>, <cell at 0x10e2e88: dict object at 0x119f360>, <cell at 0x10f01a0: NoneType object at 0x826880>, <cell at 0x10f00f8: str object at 0x7f170b05d810>, <cell at 0x10f0130: function object at 0x10ec578>), <function _make_request at 0x10ec578>, (1,), {}, <cell at 0x10f0440: Resource object at 0x10e6a50>, <cell at 0x10f02b8: dict object at 0x11b2d70>, <cell at 0x10f0360: function object at 0x10ec6e0>, <cell at 0x10f0280: NoneType object at 0x826880>, <cell at 0x10f02f0: str object at 0x10ca228>, <cell at 0x10f0408: str object at 0x7f170b05d810>, <cell at 0x10f0050: dict object at 0x11b6370>, {'Accept': 'application/json', 'User-Agent': 'couchdb-python 0.6'}, (<cell at 0x10f0280: NoneType object at 0x826880>]

gc.garbage列表包含很多项目.这是否意味着gc.garbage中的物体正在泄漏或已被收集或将被收集?

JUS*_*ION 1

来自文档

\n
\n

垃圾回收

\n

收集器发现无法访问但无法释放的对象列表(无法收集的对象)。

\n
\n

所以对我来说这看起来像是某种泄漏。现在文档继续解释可能发生这种情况的条件:

\n
\n

具有del () 方法并且属于引用循环一部分的对象会导致整个引用循环不可收集,包括不一定在循环中但只能从循环中访问的对象。Python 不会自动收集此类循环,因为一般来说,Python 不可能猜测运行 del ( ) 方法的安全顺序。如果您知道安全顺序,则可以通过检查垃圾列表并显式中断由于列表中的对象而产生的循环来强制解决问题。请注意,即使如此,这些对象由于位于垃圾列表中而保持活动状态,因此它们也应该从垃圾中删除。例如,在中断循环后,执行 del gc.garbage[:] 来清空列表。通常最好通过不使用del () 方法创建包含对象的循环来避免该问题,并且在这种情况下可以检查垃圾以验证没有创建此类循环。

\n
\n

现在设置DEBUG_SAVEALL标志会使你所有的垃圾泄漏。来自同一来源:

\n
\n

gc.DEBUG_SAVEALL

\n

设置后,找到的所有无法访问的对象将被追加到垃圾中,而不是被释放。这对于调试泄漏程序很有用。

\n
\n

所以,再说一次,是的,该列表是内存泄漏。但你告诉它泄露所有这些!

\n