Add `duration` and `visited` to the `gc.callbacks` and `gc.get_stats()` dicts
@brandtbucher 已经在做这个了。
开始于 2025年10月26日。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Feature or enhancement
The documentation for gc.callbacks states that its primary use cases are:
Gathering statistics about garbage collection, such as how often various generations are collected, and how long the collection takes.
We can be doing a bit more to serve users of gc.callbacks (and gc.get_stats() too). In particular:
duration: Collection time, which is mentioned above, is something we already have a low-overhead mechanism to measure for use with thegc.DEBUG_STATSflag. It's a bit finnicky to get right in a callback, since they're called in two phases (so the start time has to be stashed away somewhere between calls).visited: The number of visited objects is also extremely useful (collected/visitedgives you the efficiency of your collection). Getting this information from Python is very expensive (sum(len(gc.get_objects(g)) for g in range(gen + 1))), but it's cheap for us to provide from the GC itself.
With these changes, many users can probably get away with just handling stop events, or ditching callbacks entirely and just periodically checking gc.get_stats() instead for aggregates.
I already ran this idea by @nascheme and @markshannon this week. They were supportive of the idea, so I plan on working on this soon.
(Supersedes #140515.)
Linked PRs
- gh-141720
- gh-141814
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
评估
这个 Issue 还没有评估数据。