all Python 3gcfunction
gc.set_threshold()
thresholds and statistics
Sets the thresholds of automatic garbage collection; threshold0=0 disables automatic collection.
Syntax
gc.set_threshold(threshold0[, threshold1[, threshold2]])
A higher threshold0 runs collections less often and speeds up programs that create millions of objects, but cycles live longer and peak memory grows. set_threshold(0) disables collection just like gc.disable, yet gc.isenabled() still says True — an isenabled check will not notice. In 3.14.0–3.14.4 the collector was incremental and ignored threshold2; 3.14.5 restored the 3.13 behaviour.
Examples
import gc
old = gc.get_threshold()
gc.set_threshold(50_000)
print(gc.get_threshold()[0]) # → 50000
gc.set_threshold(0)
print(gc.isenabled()) # → True — хотя автоматическая сборка выключена
gc.set_threshold(*old)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy