5
class Member(object):
    def __init__(self, identifier):
        self.identifier = identifier
        print "Member __init__", self.identifier

    def __del__(self):
        print "Member __del__", self.identifier
        with open("/home/might/" + self.identifier, "w") as outF:
            outF.write(self.identifier)

class WithMembers(object):
    def __init__(self):
        print "WithMembers __init__"
        print WithMembers.classMem
        self.instanceMem = Member("instance mem")

    def __del__(self):
        print "WithMembers __del__"

    classMem = Member("class mem")

if __name__ == "__main__":
    print "main"
    WithMembers()
    #del WithMembers.classMem       # "Member __del__ class mem" before "end"
    #del WithMembers                # "Member __del__ class mem" after "end"
    print "end"

The above code is in Hidden.py and running python Hidden.py produces the following output:

Member __init__ class mem
main
WithMembers __init__
<__main__.Member object at 0x935aeec>
Member __init__ instance mem
WithMembers __del__
Member __del__ instance mem
end

I don't see Member __del__ class mem in output or the class mem file unless I un-comment one of the del statements. Why is this? When are python classes and class attributes garbage collected?

4

2 回答 2

4

这被报告为http://bugs.python.org/issue1545463中的一个错误,该错误已在 3.4 中修复但未向后移植(我运行的是 2.7)。这也在http://code.activestate.com/lists/python-list/504216/中进行了解释。有关 python 3.5 中的输出,请参见下文。

基于以上,我的理解是在 2.7 中,WithMembers当解释器退出时,新样式类仍然存在(没有被 GC 清理)。结果,classMem不是垃圾收集,因为WithMembers仍然引用它。

请注意,新样式类具有对自身的循环引用__mro__和一些内置描述符(http://bugs.python.org/issue17950)。尽管模块级新式类在模块清理后被 GC 认为是死的,但在模块清理后清理它们的 GC 调用被禁用,因为它导致了太多其他问题。

这不会导致内存泄漏,因为操作系统会在解释器退出后清理资源。

class Member(object):
    def __init__(self, identifier):
        self.identifier = identifier
        print("Member __init__ " + self.identifier)

    def __del__(self):
        print("Member __del__ " + self.identifier)
        with open("/home/might/" + self.identifier, "w") as outF:
            outF.write(self.identifier)

class WithMembers(object):
    def __init__(self):
        print("WithMembers __init__")
        print(WithMembers.classMem)
        self.instanceMem = Member("instance mem")

    def __del__(self):
        print("WithMembers __del__")

    classMem = Member("class mem")

if __name__ == "__main__":
    print("main")
    WithMembers()
    print("end")

运行时输出以下内容python3 Hidden.py

Member __init__ class mem
main
WithMembers __init__
<__main__.Member object at 0xb6fc8e2c>
Member __init__ instance mem
WithMembers __del__
Member __del__ instance mem
end
Member __del__ class mem
Exception ignored in: <bound method Member.__del__ of <__main__.Member object at 0xb6fc8e2c>>
Traceback (most recent call last):
  File "class_member_gc.py", line 8, in __del__
NameError: name 'open' is not defined
于 2015-02-17T00:46:19.287 回答
0

classMem是类的类变量,WithMembers这意味着它将由该类的所有实例共享。这在 Python 中是一个全局性的东西。这就是__del__退出程序时没有调用类成员的原因。

这里有一个问题:为什么 Python 在退出程序时不简单地将所有引用计数设置为 0,以便__del__调用所有函数?

像 C++ 一样,它保证调用全局变量的析构函数。在 Python 中保证这一点的唯一方法是运行所有模块并删除它们的所有变量。但这意味着该__del__方法不能相信它可能想要使用的任何全局变量仍然存在,因为无法知道要删除变量的顺序。

有两种方法可以强制销毁classMem.

一个是del WithMembers.classMem。这将减少对 0 的引用计数,__del__并将自动调用。

另一个是使类WithMembers成为旧式类(不继承自object)。像这样:

...

class WithMembers:
    def __init__(self):
        print "WithMembers __init__"
        print WithMembers.classMem
        self.instanceMem = Member("instance mem")
...        

输出将是:

Member __init__ class mem
main
WithMembers __init__
<__main__.Member object at 0x00000000026C5278>
Member __init__ instance mem
WithMembers __del__
Member __del__ instance mem
end
Member __del__ class mem

这是一个非常有用的链接,可帮助您更好地理解此答案。 http://www.electricmonk.nl/log/2008/07/07/python-destructor-and-garbage-collection-notes/

希望能帮助到你。:)

于 2015-02-15T11:01:46.110 回答