Why does python use both reference counting and mark-and-sweep for gc?(为什么python对gc同时使用引用计数和mark-and-sweep?)
问题描述
我的问题是为什么 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么python对gc同时使用引用计数和mark-and-sweep?
基础教程推荐
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 筛选NumPy数组 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01