本文介绍了CPython的垃圾收集是否会压缩?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和一位朋友通过语言进行比较,他提到Java的自动化内存管理优于Python,因为Java不是压缩,而Python不是,因此对于长时间运行的服务器,Python是一个糟糕的选择。

没有进入哪个更好或更糟的是,他的声明是否正确 - CPython的垃圾收集器是否不紧凑内存,因此,长时间运行的Python进程会越来越分散随着时间的推移?



我知道运行CPython的垃圾收集器是可选的。大多数情况下,它使用自动引用计数来实现自动化内存管理,只要引用计数达到零,就释放该对象 - 因此,释放对象所需的唯一一项CPython垃圾收集器就是检测循环,对象在根集中有一个引用。但是我不知道它是否会进行压缩的细节。



如果没有,那么长时间运行的CPython进程如何处理内存碎片问题?

解决方案

我不确定,但CPython使用及其对象将内存地址用作,所以我会说它不会压缩...并根据,[C] Python不使用内存压缩... [W] Python使用内存压缩,实现C扩展将会使
更多单调乏味且容易出错,并且会减少这样的
扩展 - 限制使用Python的领域。


I was talking with a friend, comparing languages, and he mentioned that Java's automated memory management is superior to Python's as Java's does compaction, while Python's does not - and hence for long-running servers, Python is a poor choice.

Without getting into which is better or worse, is his claim true - does CPython's garbage collector not compact memory and, thus, long-running Python processes get more and more fragmented over time?

I know that running CPython's garbage collector is optional. Mostly it uses automated reference counting for automated memory management, and as soon as a reference count hits zero, the object is freed - thus the only thing that CPython's garbage collector is needed for, in terms of freeing objects, is to detect cycles which no object in the root set has a reference to. But I don't know the details of whether it does any compaction in addition to that.

If it does not, then how do long-running CPython processes address the memory fragmentation issue?

解决方案

I don't know for sure, but CPython uses reference counting and its objects use memory addresses as ids so I would say it does not do compaction... And according to this, "[C]Python does not use memory compaction ... [w]ould Python use memory compaction, implementing C extensions would be much more tedious and error prone and there would be less such extensions - limiting the domain where Python is used."

这篇关于CPython的垃圾收集是否会压缩?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 05:56