为什么python同时使用引用计数和mark-and-sweep for gc? [英] Why does python use both reference counting and mark-and-sweep for gc?

查看:292
本文介绍了为什么python同时使用引用计数和mark-and-sweep for gc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是为什么python同时使用引用计数和mark-and-sweep for 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同时使用引用计数和mark-and-sweep for gc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆