簡體   English   中英

這是內存泄漏嗎?

[英]Is this a memory leak?

我正在使用gc模塊調試泄漏。

這是一個gui程序,我已經將此功能連接到了按鈕上。

我已將更多調試設置為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中的對象正在泄漏或已被收集或將被收集?

文檔

gc垃圾

收集器發現無法訪問但無法釋放的對象列表(無法收集的對象)。

因此對我來說似乎是某種泄漏。 現在,文檔繼續說明可能發生這種情況的條件:

具有del ()方法並且屬於參考循環的對象導致整個參考循環無法收集,包括對象不一定在該循環中,而是只能從該循環訪問。 Python不會自動收集此類循環,因為通常來說,Python不可能猜測出運行del ()方法的安全順序。 如果您知道安全訂單,則可以通過檢查垃圾清單並由於清單中的對象而明確中斷周期來強制執行此問題。 請注意,即使這些對象仍處於垃圾列表中,它們也可以保持活動狀態,因此也應將它們從垃圾中刪除。 例如,在中斷循環后,請執行del gc.garbage [:]清空列表。 通常最好通過不使用del ()方法創建包含對象的循環來避免該問題,在這種情況下可以檢查垃圾以驗證是否沒有創建此類循環。

現在,設置DEBUG_SAVEALL標志會使您的所有垃圾泄漏。 來自同一來源:

gc.DEBUG_SAVEALL

設置后,找到的所有無法訪問的對象將被添加到垃圾中,而不是被釋放。 這對於調試泄漏的程序很有用。

因此,再次,是的,該列表是內存泄漏。 但是您告訴它泄漏所有這些!

在其他語言中,我成功地使用了堆分析器來跟蹤內存泄漏。 我從未在Python中使用過,但Heapy似乎值得嘗試。

在其“數據處理”部分下,嘗試以下功能:

  • 從一組根對象計算“主導”集,這將產生如果釋放根對象將被釋放的對象集。

如果它像其他工具一樣,則應該能夠進行深入研究。 跟隨具有最大“支配集”的對象,直到發現似乎太大的東西,很可能是泄漏。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM