我有一個很大的 python 代碼庫。在某一時刻,我發現(使用guppy但這并不重要)我有一個現有的類實例BigClass。在代碼的這一點上,我不希望有任何活著的實例,BigClass因為它們都應該被釋放。我嘗試打電話gc.collect()我如何追蹤這個實例在哪里,為什么它還活著,它的屬性等等?
1 回答

江戶川亂折騰
TA貢獻1851條經驗 獲得超5個贊
您可以找到該類的所有實例:
for obj in gc.get_objects():
if isinstance(obj, BigClass):
# DEBUG IT
為了調試實際對象以及它們是如何引用的:
for ref in gc.get_referrers(obj):
# Should probably filter here to reduce the amount of data printed and avoid printing locals() as well
print(ref)
添加回答
舉報
0/150
提交
取消