【问题标题】:python - Finding a leaking objectpython - 查找泄漏对象
【发布时间】:2020-12-20 06:48:07
【问题描述】:

我有一个庞大的 Python 代码库。

有一次我发现(使用guppy,但这并不重要)我有一个现有的我的班级实例BigClass

在代码的这一点上,我不希望有任何 BigClass 的活动实例,因为它们都应该被释放。我试过打电话给gc.collect()

我如何追踪这个实例在哪里,为什么它还活着,它的属性等等?

【问题讨论】:

标签: python python-3.x memory-management guppy


【解决方案1】:

您可以找到该类的所有实例:

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)

【讨论】:

    猜你喜欢
    • 2011-09-08
    • 2011-12-09
    • 2014-12-10
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多