cpython做了什么来帮助检测对象周期(引用计数)?

时间:2021-11-26 16:59:25

From what I've read about cpython it seems like it does reference counting + something extra to detect/free objects pointing to each other.(Correct me if I'm wrong). Could someone explain the something extra? Also does this guarantee* no cycle leaking? If not is there any research into an algorithm proven to add to reference counting to make it never leak*? Would this be just running a non reference counting tracing gc every so often?

从我所读到的关于cpython的内容来看,似乎它确实引用了计数+额外的东西来检测/释放彼此指向的对象。(如果我错了,请纠正我)。有人能解释一下额外的东西吗?这也保证*没有循环泄漏?如果没有,对算法的任何研究都证明可以增加参考计数,使其永不泄漏*?这是否会经常运行非引用计数跟踪gc?

*discounting bugs and problems with modules using foreign function interface

*使用外部功能接口折扣模块的错误和问题

1 个解决方案

#1


4  

As explained in the documentation for gc.garbage, there is no guarantee that no leaks occur; specifically, cyclic objects with __del__ methods are not collected by default. For such objects, the cyclic links have to be manually broken to enable further GC.

如gc.garbage文档中所述,无法保证不会发生泄漏;具体而言,默认情况下不会收集带有__del__方法的循环对象。对于此类对象,必须手动断开循环链接以启用进一步的GC。

From what I understand by browsing the CPython sourcecode, the interpreter keeps references to all objects under its control. The "extra" garbage collector runs a mark-and-sweep-like algorithm through the heap, remembers for each object if it is reachable from the "outside" and, if not, deletes it. (The GC is generational, but it may be run explicitly from the gc module with a generation argument.)

根据我通过浏览CPython源代码的理解,解释器保持对其控制下的所有对象的引用。 “额外”垃圾收集器通过堆运行类似于标记和扫描的算法,如果可以从“外部”访问,则记住每个对象,如果没有,则删除它。 (GC是分代的,但可以使用generation参数从gc模块显式运行。)

The only efficient algorithm that I could think of that satisfies your criteria would indeed be a "full" GC algorithm to augment reference counting and this is what seems to be implemented in Python. I'm not an expert in these matters though.

我能想到的唯一能满足您标准的高效算法确实是一个“完整”的GC算法来增强引用计数,这似乎是在Python中实现的。不过,我不是这方面的专家。

#1


4  

As explained in the documentation for gc.garbage, there is no guarantee that no leaks occur; specifically, cyclic objects with __del__ methods are not collected by default. For such objects, the cyclic links have to be manually broken to enable further GC.

如gc.garbage文档中所述,无法保证不会发生泄漏;具体而言,默认情况下不会收集带有__del__方法的循环对象。对于此类对象,必须手动断开循环链接以启用进一步的GC。

From what I understand by browsing the CPython sourcecode, the interpreter keeps references to all objects under its control. The "extra" garbage collector runs a mark-and-sweep-like algorithm through the heap, remembers for each object if it is reachable from the "outside" and, if not, deletes it. (The GC is generational, but it may be run explicitly from the gc module with a generation argument.)

根据我通过浏览CPython源代码的理解,解释器保持对其控制下的所有对象的引用。 “额外”垃圾收集器通过堆运行类似于标记和扫描的算法,如果可以从“外部”访问,则记住每个对象,如果没有,则删除它。 (GC是分代的,但可以使用generation参数从gc模块显式运行。)

The only efficient algorithm that I could think of that satisfies your criteria would indeed be a "full" GC algorithm to augment reference counting and this is what seems to be implemented in Python. I'm not an expert in these matters though.

我能想到的唯一能满足您标准的高效算法确实是一个“完整”的GC算法来增强引用计数,这似乎是在Python中实现的。不过,我不是这方面的专家。