代码之家  ›  专栏  ›  技术社区  ›  QuantumEnergy

PEP 442,具有_udel_uu()方法的对象不会在gc中结束。再也不是垃圾了

  •  0
  • QuantumEnergy  · 技术社区  · 7 年前

    循环 __del__ 方法收集。(PEP 442)。
    但是为什么我得到了python2.7和pyton3.6相同的结果呢?
    a,b,foo,bar 被释放了?

    import gc
    import sys
    
    
    class Foo(object):
        def __init__(self):
            self.bar = None
            print('foo init')
    
        def __del__(self):
            print("foo del")
    
    
    class Bar(object):
        def __init__(self):
            self.foo = None
            print('bar init')
    
        def __del__(self):
            print('bar del')
    
    
    def collect_and_show_garbage():
        print("Collecting...")
        n = gc.collect()
        print("unreachable objects:", n)
    
    
    def func():
        foo = Foo()
        bar = Bar()
        foo.bar = bar
        bar.foo = foo
    
    
    def func2():
        a = [1, 2]
        b = [3, 4]
        a.append(b)
        b.append(a)
    
    
    func()
    func2()
    collect_and_show_garbage()
    


    条形图初始化
    收集。。。
    富德尔
    条形图del
    无法访问的对象:6

    当我删除时 __删除__ 方法,我得到了同样的结果

    import gc
    import sys
    
    
    class Foo(object):
        def __init__(self):
            self.bar = None
            print('foo init')
    
    
    class Bar(object):
        def __init__(self):
            self.foo = None
            print('bar init')
    
    
    
    def collect_and_show_garbage():
        print("Collecting...")
        n = gc.collect()
        print("unreachable objects:", n)
    
    
    def func():
        foo = Foo()
        bar = Bar()
        foo.bar = bar
        bar.foo = foo
    
    
    def func2():
        a = [1, 2]
        b = [3, 4]
        a.append(b)
        b.append(a)
    
    
    func()
    func2()
    collect_and_show_garbage()
    

    foo初始化
    条形图初始化
    收集。。。
    无法访问的对象:6

    1 回复  |  直到 7 年前
        1
  •  0
  •   QuantumEnergy    7 年前

    gc。收集

    返回无法访问的对象数。

    gc。垃圾:

    收集器发现无法访问但 无法释放(无法收集的对象)。

    import gc
    import sys
    
    
    class Foo(object):
        def __init__(self):
            self.bar = None
            print('foo init')
    
        def __del__(self):
            print("foo del")
    
    
    class Bar(object):
        def __init__(self):
            self.foo = None
            print('bar init')
    
        def __del__(self):
            print('bar del')
    
    
    def collect_and_show_garbage():
        print("Collecting...")
        n = gc.collect()
        print("unreachable objects:", n)
        print("uncollectable objects:", gc.garbage)
    
    
    def func():
        foo = Foo()
        bar = Bar()
        foo.bar = bar
        bar.foo = foo
    
    
    def func2():
        a = [1, 2]
        b = [3, 4]
        a.append(b)
        b.append(a)
    
    
    func()
    func2()
    collect_and_show_garbage()
    

    蟒蛇2.7

    foo初始化
    条形图初始化
    收集。。。
    ('unreachable objects:',6)
    ('uncollectable objects:',[< 主要的 .0x10cf55d0处的Foo对象(>&书信电报; 主要的 .0x10CF5650处的条形图对象(gt;])

    Python3.6

    foo初始化
    条形图初始化
    收集。。。
    富德尔
    条形图del

    无法收集的对象:[]