Skip to content
Merged
Prev Previous commit
Next Next commit
Add heap_size to stats
  • Loading branch information
sergey-miryanov committed Apr 30, 2026
commit 22814995137104f04ed1a58e5ca16f669cda18b3
2 changes: 2 additions & 0 deletions Include/internal/pycore_interp_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ struct gc_generation {
struct gc_generation_stats {
PyTime_t ts_start;
PyTime_t ts_stop;
/* heap_size on the start of the collection */
Py_ssize_t heap_size;
/* total number of collections */
Py_ssize_t collections;
/* total number of collected objects */
Expand Down
4 changes: 3 additions & 1 deletion Python/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1405,14 +1405,15 @@ add_stats(GCState *gcstate, int gen, struct gc_generation_stats *stats)
memcpy(cur_stats, prev_stats, sizeof(struct gc_generation_stats));

cur_stats->ts_start = stats->ts_start;
cur_stats->ts_stop = stats->ts_stop;
cur_stats->heap_size = stats->heap_size;

cur_stats->collections += 1;
cur_stats->collected += stats->collected;
cur_stats->uncollectable += stats->uncollectable;
cur_stats->candidates += stats->candidates;

cur_stats->duration += stats->duration;
cur_stats->ts_stop = stats->ts_stop;
}

/* This is the main function. Read this to understand how the
Expand Down Expand Up @@ -1465,6 +1466,7 @@ gc_collect_main(PyThreadState *tstate, int generation, _PyGC_Reason reason)
invoke_gc_callback(tstate, "start", generation, &stats);
}

stats.heap_size = gcstate->heap_size;
// ignore error: don't interrupt the GC if reading the clock fails
(void)PyTime_PerfCounterRaw(&stats.ts_start);
if (gcstate->debug & _PyGC_DEBUG_STATS) {
Expand Down
Loading