我正在使用gc模块来调试泄漏。
这是一个gui程序,我把这个功能挂钩到一个按钮。
我已将set debug设置为gc.SAVE_ALL
> gc.collect()
>
> print gc.garbage
这是输出
[(<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中的物品泄漏或已被收集或将被收集?
答案 0 :(得分:1)
来自the docs:
gc.garbage
收集器发现无法访问但无法释放的对象列表(无法收集的对象)。
所以它看起来像是某种泄漏给我。现在,文档继续解释可能发生这种情况的条件:
具有 del ()方法且属于引用循环的对象会导致整个引用循环无法收集,包括不一定在循环中但只能从中获取的对象。 Python不会自动收集这样的循环,因为一般来说,Python无法猜测运行 del ()方法的安全顺序。如果您知道安全订单,则可以通过检查垃圾清单来强制解决问题,并明确地打破由于列表中的对象而导致的周期。请注意,这些对象即使存在于垃圾列表中也会保持活动状态,因此它们也应该从垃圾中删除。例如,在打破循环后,执行del gc.garbage [:]清空列表。通常不使用 del ()方法创建包含对象的循环来避免此问题,并且在这种情况下可以检查垃圾以验证是否没有创建此类循环。
现在设置DEBUG_SAVEALL
标志会使您的垃圾泄漏全部。来自同一个来源:
gc.DEBUG_SAVEALL
设置后,找到的所有无法访问的对象都将附加到垃圾而不是被释放。这对于调试泄漏程序非常有用。
所以,再次,是的,该列表是泄露的内存。但你告诉它泄漏所有这些!
答案 1 :(得分:0)
在其他语言中,我使用堆分析器非常成功地跟踪内存泄漏。我从来没有在Python中使用过,但Heapy似乎值得尝试。
在“数据处理”部分下,尝试使用此功能:
如果它像其他工具一样,您应该能够向下钻取。跟踪具有最大“主导集”的对象,直到找到看起来太大的东西,这可能是泄漏。