Garbage Collection

The gc module

  • Provides an API for controlling the garbage collector
     import gc
     
     gc.disable()                 # Turn off garbage collection
     gc.enable()                  # Enable garbage collection
     gc.collect()                 # Run full garbage collection step
     gc.set_threshold(1000,10,10) # Set frequency of garbage collection
     
     # Print list of uncollectable objects
     print gc.garbage
     
     # Print debugging information to find memory leaks
     gc.set_debug(gc.DEBUG_LEAK)
     
<<< O'Reilly OSCON 2001, New Features in Python 2, Slide 31
July 26, 2001, beazley@cs.uchicago.edu
>>>