Python Glossary Русский

all Python 3gcfunction

gc.disable()

collection control

Turns off automatic collection of cyclic garbage. Reference counting keeps working.

Syntax

gc.disable()

Only cycle detection is turned off: objects without cycles are still freed immediately. Cycles, however, pile up until you call gc.collect manually or gc.enable — a long-running process with the collector off slowly leaks. The state is global for the whole interpreter, so re-enable it in finally.

Examples

import gc, weakref
class Box:
    pass
gc.disable()
try:
    b = Box()
    r = weakref.ref(b)
    del b
    print(r() is None)  # → True — без цикла объект ушёл и так
finally:
    gc.enable()

See also

Open in the full glossarydocs.python.orgReport an inaccuracy