The gc Module

(Optional, 2.0 and later) The gc module provides an interface to the built-in cyclic garbage collector.

Python uses reference counting to keep track of when to get rid of objects; as soon as the last reference to an object goes away, the object is destroyed.

Starting with Version 2.0, Python also provides a cyclic garbage collector, which runs at regular intervals. This collector looks for data structures that point to themselves, and attempts to break the cycles. Example 1-87 shows this.

You can use the gc.collect function to force full collection. This function returns the number of objects destroyed by the collector.

Example 1-87. Using the gc Module to Collect Cyclic Garbage


import gc

# create a simple object that links to itself
class Node:

 def _ _init_ _(self, name): = name
 self.parent = None
 self.children = []

 def addchild(self, node):
 node.parent = self

 def _ _repr_ _(self):
 return "" % (repr(, id(self))

# set up a self-referencing structure
root = Node("monty")


# remove our only reference
del root

print gc.collect(), "unreachable objects"
print gc.collect(), "unreachable objects"

12 unreachable objects
0 unreachable objects

If you're sure that your program doesn't create any self-referencing data structures, you can use the gc.disable function to disable collection. After calling this function, Python 2.0 works exactly like 1.5.2 and earlier.

Core Modules

More Standard Modules

Threads and Processes

Data Representation

File Formats

Mail and News Message Processing

Network Protocols


Multimedia Modules

Data Storage

Tools and Utilities

Platform-Specific Modules

Implementation Support Modules

Other Modules

Python Standard Library
Python Standard Library (Nutshell Handbooks) with
ISBN: 0596000960
EAN: 2147483647
Year: 2000
Pages: 252
Authors: Fredrik Lundh
Similar book on Amazon © 2008-2017.
If you may any questions please contact us: