Python垃圾收集器文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4484167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:57:12  来源:igfitidea点击:

Python garbage collector documentation

pythongarbage-collection

提问by Ante

I'm looking for documents that describes in details how python garbage collection works.

我正在寻找详细描述 python 垃圾收集如何工作的文档。

I'm interested what is done in which step. What objects are in these 3 collections? What kinds of objects are deleted in each step? What algorithm is used for reference cycles finding?

我对哪一步做了什么感兴趣。这 3 个集合中有哪些对象?每一步都删除了哪些对象?什么算法用于查找参考周期?

Background: I'm implementing some searches that have to finish in small amount of time. When the garbage collector starts collecting the oldest generation, it is "much" slower than in other cases. It took more time than it is intended for searches. I'm looking how to predict when it will collect oldest generation and how long it will take.

背景:我正在执行一些必须在短时间内完成的搜索。当垃圾收集器开始收集最老的一代时,它比其他情况下“慢得多”。花费的时间超过了搜索的预期时间。我正在寻找如何预测何时收集最老的一代以及需要多长时间。

It is easy to predict when it will collect oldest generation with get_count()and get_threshold(). That also can be manipulated with set_threshold(). But I don't see how easy to decide is it better to make collect()by force or wait for scheduled collection.

很容易预测它什么时候会用get_count()和收集最老的一代get_threshold()。这也可以用set_threshold(). 但我不明白决定是collect()强制制造还是等待预定收集更好。

采纳答案by Rafe Kettler

There's no definitive resource on how Python does its garbage collection (other than the source code itself), but those 3 links should give you a pretty good idea.

没有关于 Python 如何进行垃圾收集的明确资源(除了源代码本身),但是这 3 个链接应该给你一个很好的主意。

Update

更新

The source is actually pretty helpful. How much you get out of it depends on how well you read C, but the comments are actually very helpful. Skip down to the collect()functionand the comments explain the process well (albeit in very technical terms).

来源实际上非常有帮助。你从中得到多少取决于你对 C 的阅读程度,但这些注释实际上非常有帮助。跳到collect()函数和注释很好地解释了这个过程(尽管是非常技术性的术语)。