引用计数有循环计数这个明显缺陷,那为什么Python还要使用引用计数而不是标记清除呢?
对List啊Map啊Set之类的引用计数也在起作用的。mark-sweep只是备份。整套引用计数机制嵌在ceval.c里了,所有对象都要被它折腾到。换句话说不是List不被引用计数,而是List的引用计数如果自然降到零的话就自然按照引用计数机制释放;否则当cycle GC启动的时候就会对它处理。不过通过引用计数来提高回收及时性这点仍然还是成立的。 这是一种设计取舍。用CPython的大家高兴就好呗。
The details of Python memory management depend on the implementation. The standard C implementation of Python uses reference counting to detect inaccessible objects, and another mechanism to collect reference cycles, periodically executing a cycle detection algorithm which looks for inaccessible cycles and deletes the objects involved. The gc module provides functions to perform a garbage collection, obtain debugging statistics, and tune the collector’s parameters.
In the absence of circularities and tracebacks, Python programs do not need to manage memory explicitly.
Why doesn’t Python use a more traditional garbage collection scheme? For one thing, this is not a C standard feature and hence it’s not portable. (Yes, we know about the Boehm GC library. It has bits of assembler code for most common platforms, not for all of them, and although it is mostly transparent, it isn’t completely transparent; patches are required to get Python to work with it.)
...
Traditional GC also becomes a problem when Python is embedded into other applications. While in a standalone Python it’s fine to replace the standard malloc() and free() with versions provided by the GC library, an application embedding Python may want to have its own substitute for malloc() and free(), and may not want Python’s. Right now, Python works with anything that implements malloc() and free() properly.
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号