Garbage Collection

Prior to Python 2.0

  • Cyclical data structures caused memory leaks.
  • No way to reclaim memory due to reference counting.
     a = [ ]
     b = [ a ]
     a.append(b)
     del a
     del b          # a, b still allocated
  • A problem for certain data structures (graphs, trees, etc.)

Python 2.0 adds garbage collection of cycles

  • Containers (lists, tuples, dictionaries) placed on internal list
  • Periodic scan of list used to detect and remove unreferenced cyclical data
  • Properties of garbage collection can be controlled using the gc module
<<< O'Reilly OSCON 2001, New Features in Python 2, Slide 29
July 26, 2001, beazley@cs.uchicago.edu
>>>