CPython的垃圾收集是否会进行压缩?(Does CPython's garbage collection do compaction?)

编程入门 行业动态 更新时间:2024-10-28 08:29:10
CPython的垃圾收集是否会进行压缩?(Does CPython's garbage collection do compaction?)

我正在与一位朋友交谈,比较语言,他提到Java的自动内存管理优于Python,因为Java会压缩,而Python则没有 - 因此对于长期运行的服务器,Python是一个糟糕的选择。

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

我知道运行CPython的垃圾收集器是可选的。 大多数情况下,它使用自动引用计数进行自动内存管理,一旦引用计数达到零,就会释放对象 - 因此,在释放对象方面,CPython的垃圾收集器唯一需要的是检测没有的循环。根集中的对象具有引用。 但我不知道除此之外是否进行任何压实的细节。

如果没有,那么长期运行的CPython进程如何解决内存碎片问题?

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?

最满意答案

我不确定,但CPython使用引用计数 ,它的对象使用内存地址作为id,所以我会说它不做压缩...而且根据这个 ,“[C] Python不使用内存压缩.. 。[] Python应该使用内存压缩,实现C扩展会更加繁琐且容易出错,并且会有更少的扩展 - 限制使用Python的域。“

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."

更多推荐

本文发布于:2023-08-04 09:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1412637.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是否会   垃圾   CPython   compaction   collection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!