Python Glossary Русский

all Python 3gcfunction

gc.set_debug()

debugging

Turns on collector debug flags (DEBUG_STATS, DEBUG_SAVEALL, DEBUG_LEAK…); output goes to sys.stderr.

Syntax

gc.set_debug(flags)

Flags are not added but replace the old ones entirely: to add one write set_debug(get_debug() | flag), to turn all off — set_debug(0). DEBUG_SAVEALL (and DEBUG_LEAK, which includes it) puts everything collected into gc.garbage instead of freeing it — a deliberate leak, so clear the flag and gc.garbage after debugging.

Examples

import gc
class Node:
    pass
gc.set_debug(gc.DEBUG_SAVEALL)
n = Node()
n.me = n
del n
gc.collect()
print([type(o).__name__ for o in gc.garbage if isinstance(o, Node)])  # → ['Node']
gc.set_debug(0)
gc.garbage.clear()

See also

Open in the full glossarydocs.python.orgReport an inaccuracy