ChatGPT解决这个技术问题 Extra ChatGPT

Python garbage collector documentation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations. Closed 7 years ago. Improve this question

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

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?

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.


0
0xdd

Python Garbage Collection

gc module docs

Details on Garbage Collection for Python

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.

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() function and the comments explain the process well (albeit in very technical terms).


CPython no longer uses Subversion. They switched to Mercurial hosted at hg.python.org. Since I can't figure out how to link to the current version of a file on that site, I'll link to an unofficial GitHub mirror instead.
Never mind that, figured out how to link to the latest gcmodule.c on the official site. I still find GitHub more readable.
learn a lot. Thx
The first link does not point to anything related to Python garbage collection. Please update the link.