Name
set_threshold
Synopsis
set_threshold(thresh0[,thresh1[,thresh2]])
Sets the thresholds that control how frequently cyclic garbage
collection cycles run. If you set thresh0
to 0, garbage collection is disabled. Garbage
collection is an advanced topic, and the details of the generational
garbage collection approach used in Python and its thresholds are
beyond the scope of this book.
When
you know you have no cyclic garbage loops in your program, or when
you can’t afford the delay of a cyclic garbage
collection run at some crucial time, you can suspend automatic
garbage collection by calling gc.disable( ). You
can enable collection again later by calling gc.enable( ). You can test whether automatic collection is currently
enabled by calling gc.isenabled( ), which returns
True or False. To control when
the time needed for collection is spent, you can call
gc.collect( ) to force a full cyclic collection
run to happen immediately. An idiom for wrapping some time-critical
code is therefore:
import gc
gc_was_enabled = gc.isenabled( )
if gc_was_enabled:
gc.collect( )
gc.disable( )
# insert some time-critical code here
if gc_was_enabled:
gc.enable( )The other functionality in module gc is more
advanced and rarely used, and can be grouped into two areas.
Functions get_threshold and
set_threshold and the debug flag
DEBUG_STATS can help you fine-tune garbage
collection to optimize your program’s performance.
The rest of gc’s functionality is there to help you diagnose memory leaks in your ...