Repository navigation
Incremental GC does not collect garbage as soon as expected #142002
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Nov 27, 2025 - addedinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
on Nov 27, 2025 - added3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixesperformancePerformance or resource usagePerformance or resource usage
on Nov 28, 2025 What is "expected" in this case?
Since the test in question produces no cycles, an optimal cycle collector would always fail this test regardless of how large we set the threshold.It is the test that is broken here.
We should either increase the threshold or, better still, change the test to produce cycles.What is "expected" in this case? Since the test in question produces no cycles, an optimal cycle collector would always fail this test regardless of how large we set the threshold.
It is the test that is broken here. We should either increase the threshold or, better still, change the test to produce cycles.
The test creates 50,000 new list objects. Are you saying the GC somehow knows these are not reference cycles without running? Changing the test to create lists that reference themselves (and be cycles) would fail in the same way. This is a strange argument in how the test is broken to my mind.
This test, and I believe other tests that rely on the GC_Detector, depends heavily on the size of the global roots.
For incremental GC, there are two phases: MARK and COLLECT.
In this test, we call
gc.collectbefore the loop, it runscompleted_scavenge, sets thework_to_dovalue to zero and switchs GC to the MARK phase. In the loop the garbage collection runs when we create 2000 objects in the young generation. Then, we go to the MARK phase. In this phase, we mark all live roots and adjust value of thework_to_do, which becomes negative (I suppose a large negative). Then we switch phase to the COLLECT and stop this collection iteration. Afterwards, we repeatedly create objects in the young generation. This leads to GC runs. We check thework_to_dovalue, which is still negative, so we return early from GC. It seems that we cannot reach a positivework_to_dovalue in 50_000 iterations, and the test fails.
Bug report
Bug description:
This was found by @Yhg1s . If you run tests with this command:
The
test_gcunit test fails with:Reverting GH-140262 seems to fix this. It is possible this is related to GH-141890 as well.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs