0

我有一个大型的 python 代码库。

在某一时刻,我发现(使用guppy但这并不重要)我有一个现有的 class 实例BigClass

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

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

4

1 回答 1

0

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

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)
于 2020-09-01T16:33:40.650 回答