我的问题是为什么 python 对 gc 使用引用计数和标记和清除?为什么不只是标记和清除?
My question is why does python use both reference counting and mark-and-sweep for gc? Why not only mark-and-sweep?
我最初的猜测是,使用引用计数可以轻松删除非循环引用的对象,这可能会在一定程度上加快标记和清除并立即获得内存.不知道我猜对了吗?
My initial guess is that using reference counting can easily remove non-cyclic referenced objects, this may somewhat speed up mark-and-sweep and gain memory immediately. Don't know if my guess is right?
有什么想法吗?
非常感谢.
Python(该语言)没有说明它使用哪种形式的垃圾回收.主要实现(通常称为 CPython)就像您描述的那样.其他版本(例如 Jython 或 IronPython)使用纯粹的垃圾收集系统.
Python (the language) doesn't say which form of garbage collection it uses. The main implementation (often known as CPython) acts as you describe. Other versions such as Jython or IronPython use a purely garbage collected system.
是的,使用引用计数的早期集合有一个好处,但 CPython 使用它的主要原因是历史性的.最初没有针对循环对象的垃圾收集,因此循环导致内存泄漏.C API 和数据结构主要基于引用计数原则.添加真正的垃圾回收后,无法破坏现有的二进制 API 和所有依赖它们的库,因此必须保留引用计数.
Yes, there is a benefit of earlier collection with reference counting, but the main reason CPython uses it is historical. Originally there was no garbage collection for cyclic objects so cycles led to memory leaks. The C APIs and data structures are based heavily around the principle of reference counting. When real garbage collection was added it wasn't an option to break the existing binary APIs and all the libraries that depended on them so the reference counting had to remain.
这篇关于为什么python对gc同时使用引用计数和mark-and-sweep?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!