Python 3.4+gcfunction
gc.get_stats()
thresholds and statistics
Returns collector statistics since interpreter start: a list of per-generation dicts (collections, collected, uncollectable).
Syntax
gc.get_stats()
The key set grows: 3.15 added candidates and duration — so read keys by name and never compare a whole dict. A non-zero uncollectable is a red flag: such objects end up in gc.garbage. The numbers accumulate from startup; to measure a piece of code take the difference of two calls.
Examples
import gc
before = gc.get_stats()[2]['collections']
gc.collect()
after = gc.get_stats()[2]['collections']
print(after - before) # → 1
print({'collections', 'collected', 'uncollectable'} <= gc.get_stats()[0].keys()) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy