On this page
gc
Prev: functools | Up: stdlib_module | Next: genericpath
Rel: sys, sysconfig, builtins, warnings, dataclasses
Ref: docs.python.org — gc
Tags: #public #python3 #stdlib
Import
import gc
Overview
Controls CPython's cyclic garbage collector (collect, get_count, get_referrers, get_referents, freeze, disable) that reclaims reference cycles unreachable by reference counting alone. Most Python code never needs to touch gc; reach for it when diagnosing memory leaks, writing weakref tests, or tuning fork-friendly copy-on-write memory (gc.freeze()).
Quick Start
import gc
collected = gc.collect()
counts = gc.get_count()
assert isinstance(collected, int) and len(counts) == 3
Implementation Walkthrough
1. Inspecting Why an Object Is Still Alive (get_referrers)
import gc
class Node:
pass
holder = [Node()]
referrers = gc.get_referrers(holder[0])
assert holder in referrers
2. Freezing Permanent Objects Before fork() (gc.freeze)
In pre-fork servers (like Gunicorn), call gc.freeze() after loading modules/data so cyclic GC sweeps don't touch shared pages and trigger OS copy-on-write.
Gotchas & Boundaries
- CPython frees acyclic objects immediately via reference counting—
gconly collects reference cycles (a.b = b; b.a = a). - Don't call
gc.collect()in tight loops: Full generation-2 collections pause execution and hurt throughput. - Avoid
gc.disable()unless you know your code creates zero reference cycles (or you manually rungc.collect()at safe boundaries).
Common Functions
- gc.collect() —
collect(generation=2)— Run the garbage collector. - gc.disable() —
disable()— Disable automatic garbage collection. - gc.enable() —
enable()— Enable automatic garbage collection. - gc.freeze() —
freeze()— Freeze all current tracked objects and ignore them for future collections. - gc.get_count() —
get_count()— Return a three-tuple of the current collection counts. - gc.get_debug() —
get_debug()— Get the garbage collection debugging flags. - gc.get_freeze_count() —
get_freeze_count()— Return the number of objects in the permanent generation. - gc.get_objects() —
get_objects(generation=None)— Return a list of objects tracked by the collector (excluding the list returned). - gc.get_referents() —
get_referents(*objs)— Return the list of objects that are directly referred to by 'objs'. - gc.get_referrers() —
get_referrers(*objs)— Return the list of objects that directly refer to any of 'objs'. - gc.get_stats() —
get_stats()— Return a list of dictionaries containing per-generation statistics. - gc.get_threshold() —
get_threshold()— Return the current collection thresholds. - gc.is_finalized() —
is_finalized(obj, /)— Returns true if the object has been already finalized by the GC. - gc.is_tracked() —
is_tracked(obj, /)— Returns true if the object is tracked by the garbage collector. - gc.isenabled() —
isenabled()— Returns true if automatic garbage collection is enabled. - gc.set_debug() —
set_debug(flags, /)— Set the garbage collection debugging flags. - gc.set_threshold() —
set_threshold(threshold0, [threshold1, [threshold2]])— Set the collection thresholds (the collection frequency). - gc.unfreeze() —
unfreeze()— Unfreeze all objects in the permanent generation.
Constants & Attributes
gc.DEBUG_COLLECTABLE(int) =2gc.DEBUG_LEAK(int) =38gc.DEBUG_SAVEALL(int) =32gc.DEBUG_STATS(int) =1gc.DEBUG_UNCOLLECTABLE(int) =4gc.callbacks(list) =[]gc.garbage(list) =[]
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/functools
- tuts/python3/stdlib_module/gc/collect
- tuts/python3/stdlib_module/gc/disable
- tuts/python3/stdlib_module/gc/enable
- tuts/python3/stdlib_module/gc/freeze
- tuts/python3/stdlib_module/gc/get_count
- tuts/python3/stdlib_module/gc/get_debug
- tuts/python3/stdlib_module/gc/get_freeze_count
- tuts/python3/stdlib_module/gc/get_objects
- tuts/python3/stdlib_module/gc/get_referents
- tuts/python3/stdlib_module/gc/get_referrers
- tuts/python3/stdlib_module/gc/get_stats
- tuts/python3/stdlib_module/gc/get_threshold
- tuts/python3/stdlib_module/gc/is_finalized
- tuts/python3/stdlib_module/gc/is_tracked
- tuts/python3/stdlib_module/gc/isenabled
- tuts/python3/stdlib_module/gc/set_debug
- tuts/python3/stdlib_module/gc/set_threshold
- tuts/python3/stdlib_module/gc/unfreeze
- tuts/python3/stdlib_module/genericpath
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/tutorials/advanced_python_internals
- tuts/python3/tutorials/advanced_python_internals/memory_management_and_internals
Related notes
- tuts/python3/stdlib_module/functools Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/gc/collect Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/gc/disable Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/gc/enable Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/gc/freeze Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/gc/get_count Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/gc/get_debug Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/gc/get_freeze_count Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/gc/get_objects Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/gc/get_referents Linked here; links to this page; shared tags: #stdlib