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
无法收集的对象:[]