python / python/cpython

Add `duration` and `visited` to the `gc.callbacks` and `gc.get_stats()` dicts

Ouverte
#140,638 2 commentaires 0 réactions 1 personne assignée Voir sur GitHub

@brandtbucher y travaille déjà.

Depuis le 26/10/2025.

3.15 interpreter-core type-feature
Langage dominant
Python
Étoiles
77.2k
Forks
36k
Métriques de merge des PR
Métriques de PR en attente

Description

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 the gc.DEBUG_STATS flag. 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/visited gives 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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.