Sud*_*kla 7 python garbage-collection
当我gc.collect()在我的Python脚本中,它返回像86,14等的值.
我知道这个调用执行垃圾收集,我已经在这里完成了文档.但有人可以通过一个例子来解释这些数字究竟意味着什么?
Tim*_*ers 18
因为你因为不读自己而被责备;-),它会返回"无法访问的对象的数量".但是这些文档还不够详细,无法确切知道这意味着什么.
这真是两个数字的总和:被认定是垃圾,实际上释放对象的数量,以及被认定是垃圾,但无法释放对象的数量.对于后者的一个例子,物体直接参与包含至少一个对象与不可达("垃圾")参考周期__del__方法无法的Python 3.4之前自动释放.
这是Python 3.6.5下的一个例子:
>>> gc.collect() # no trash to begin with
0
>>> a = []
>>> a.append(a) # create an object that references itself
>>> gc.collect() # but it's not trash because name "a" is bound to it
0
>>> a = None # break the binding; _now_ it's trash
# but refcounting alone can't discover that it's trash
>>> gc.collect() # .collect() finds this cyclic trash
1 # and reports that one trash object was collected
Run Code Online (Sandbox Code Playgroud)
通常,这种返回值很少使用.
| 归档时间: |
|
| 查看次数: |
2827 次 |
| 最近记录: |