Python Glossary Русский

all Python 3gcterm

gc

module

An interface to the cyclic garbage collector: run a collection, turn it off, see who refers to an object.

Syntax

import gc

CPython frees memory by reference counting, and gc controls only the extra collector that finds reference cycles. The module is for debugging leaks (get_referrers, get_objects) and rare performance tuning; for weak references that do not keep an object alive there is weakref. Calling gc.collect() "just in case" is a mistake: objects without cycles are freed immediately anyway.

Examples

import gc
class Node:
    pass
a = Node()
a.me = a
del a
print(gc.collect() >= 1)  # → True — цикл ссылок собран сборщиком
print(gc.isenabled())  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy