gc.collect()
gc.collect() is a Python function in the GC Module category. Runs the garbage collector. Returns the number of unreachable objects collected. The syntax is gc.collect(generation=2). Parameters: generation — 0, 1, or 2. Returns: Integer count. A typical example: import gc
collected = gc.collect()
print(f"Collected {collected} objects"). A close sibling is gc.get_objects(), which returns a list of all objects tracked by the garbage collector. A close sibling is gc.set_threshold(), which sets the garbage collection thresholds for each generation. A close sibling is gc.get_threshold(), which returns the current GC thresholds as a tuple. A close sibling is gc.get_count(), which returns the current collection counts as a tuple. More about this category: Garbage collector — collect, get_objects, set_threshold, get_stats. Related GC Module entries: gc.get_objects(), gc.set_threshold(), gc.get_threshold(), gc.get_count(), gc.set_debug(), gc.get_stats(). This page is part of the free Python Reference documentation covering 978+ functions, methods, and modules with examples and parameter details.