| Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 1 | \section{\module{gc} --- | 
 | 2 |          Garbage Collector interface} | 
 | 3 |  | 
 | 4 | \declaremodule{extension}{gc} | 
| Fred Drake | e544191 | 2000-09-09 03:26:51 +0000 | [diff] [blame] | 5 | \modulesynopsis{Interface to the cycle-detecting garbage collector.} | 
| Neil Schemenauer | b2c2c9e | 2000-10-04 16:34:09 +0000 | [diff] [blame] | 6 | \moduleauthor{Neil Schemenauer}{nas@arctrix.com} | 
 | 7 | \sectionauthor{Neil Schemenauer}{nas@arctrix.com} | 
| Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 8 |  | 
| Fred Drake | e544191 | 2000-09-09 03:26:51 +0000 | [diff] [blame] | 9 | The \module{gc} module is only available if the interpreter was built | 
 | 10 | with the optional cyclic garbage detector (enabled by default).  If | 
 | 11 | this was not enabled, an \exception{ImportError} is raised by attempts | 
 | 12 | to import this module. | 
 | 13 |  | 
| Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 14 | This module provides an interface to the optional garbage collector.  It | 
 | 15 | provides the ability to disable the collector, tune the collection | 
| Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 16 | frequency, and set debugging options.  It also provides access to | 
| Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 17 | unreachable objects that the collector found but cannot free.  Since the | 
 | 18 | collector supplements the reference counting already used in Python, you | 
 | 19 | can disable the collector if you are sure your program does not create | 
 | 20 | reference cycles.  Automatic collection can be disabled by calling | 
 | 21 | \code{gc.disable()}.  To debug a leaking program call | 
| Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 22 | \code{gc.set_debug(gc.DEBUG_LEAK)}. | 
 | 23 |  | 
 | 24 | The \module{gc} module provides the following functions: | 
 | 25 |  | 
| Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 26 | \begin{funcdesc}{enable}{} | 
 | 27 | Enable automatic garbage collection. | 
 | 28 | \end{funcdesc} | 
 | 29 |  | 
 | 30 | \begin{funcdesc}{disable}{} | 
 | 31 | Disable automatic garbage collection. | 
 | 32 | \end{funcdesc} | 
 | 33 |  | 
 | 34 | \begin{funcdesc}{isenabled}{} | 
 | 35 | Returns true if automatic collection is enabled. | 
 | 36 | \end{funcdesc} | 
 | 37 |  | 
| Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 38 | \begin{funcdesc}{collect}{} | 
 | 39 | Run a full collection.  All generations are examined and the | 
 | 40 | number of unreachable objects found is returned. | 
 | 41 | \end{funcdesc} | 
 | 42 |  | 
 | 43 | \begin{funcdesc}{set_debug}{flags} | 
 | 44 | Set the garbage collection debugging flags. | 
 | 45 | Debugging information will be written to \code{sys.stderr}.  See below | 
 | 46 | for a list of debugging flags which can be combined using bit | 
 | 47 | operations to control debugging. | 
 | 48 | \end{funcdesc} | 
 | 49 |  | 
 | 50 | \begin{funcdesc}{get_debug}{} | 
 | 51 | Return the debugging flags currently set. | 
 | 52 | \end{funcdesc} | 
 | 53 |  | 
 | 54 | \begin{funcdesc}{set_threshold}{threshold0\optional{, | 
 | 55 |                                 threshold1\optional{, threshold2}}} | 
 | 56 | Set the garbage collection thresholds (the collection frequency). | 
 | 57 | Setting \var{threshold0} to zero disables collection. | 
 | 58 |  | 
 | 59 | The GC classifies objects into three generations depending on how many | 
 | 60 | collection sweeps they have survived.  New objects are placed in the | 
 | 61 | youngest generation (generation \code{0}).  If an object survives a | 
 | 62 | collection it is moved into the next older generation.  Since | 
 | 63 | generation \code{2} is the oldest generation, objects in that | 
 | 64 | generation remain there after a collection.  In order to decide when | 
 | 65 | to run, the collector keeps track of the number object allocations and | 
 | 66 | deallocations since the last collection.  When the number of | 
 | 67 | allocations minus the number of deallocations exceeds | 
 | 68 | \var{threshold0}, collection starts.  Initially only generation | 
 | 69 | \code{0} is examined.  If generation \code{0} has been examined more | 
 | 70 | than \var{threshold1} times since generation \code{1} has been | 
 | 71 | examined, then generation \code{1} is examined as well.  Similarly, | 
 | 72 | \var{threshold2} controls the number of collections of generation | 
 | 73 | \code{1} before collecting generation \code{2}. | 
 | 74 | \end{funcdesc} | 
 | 75 |  | 
 | 76 | \begin{funcdesc}{get_threshold}{} | 
 | 77 | Return the current collection thresholds as a tuple of | 
 | 78 | \code{(\var{threshold0}, \var{threshold1}, \var{threshold2})}. | 
 | 79 | \end{funcdesc} | 
 | 80 |  | 
 | 81 |  | 
 | 82 | The following variable is provided for read-only access: | 
 | 83 |  | 
 | 84 | \begin{datadesc}{garbage} | 
 | 85 | A list of objects which the collector found to be unreachable | 
| Tim Peters | 169ded0 | 2001-11-03 19:57:21 +0000 | [diff] [blame^] | 86 | but could not be freed (uncollectable objects).  By default, this list | 
 | 87 | contains only objects with \method{__del__()} methods.\footnote{Prior to | 
 | 88 |   Python 2.2, the list contained all instance objects in unreachable | 
 | 89 |   cycles,  not only those with \method{__del__()} methods.} | 
 | 90 | Objects that have | 
| Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 91 | \method{__del__()} methods and create part of a reference cycle cause | 
| Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 92 | the entire reference cycle to be uncollectable.  If | 
 | 93 | \constant{DEBUG_SAVEALL} is set, then all unreachable objects will | 
 | 94 | be added to this list rather than freed. | 
| Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 95 | \end{datadesc} | 
 | 96 |  | 
 | 97 |  | 
 | 98 | The following constants are provided for use with | 
 | 99 | \function{set_debug()}: | 
 | 100 |  | 
 | 101 | \begin{datadesc}{DEBUG_STATS} | 
 | 102 | Print statistics during collection.  This information can | 
 | 103 | be useful when tuning the collection frequency. | 
 | 104 | \end{datadesc} | 
 | 105 |  | 
 | 106 | \begin{datadesc}{DEBUG_COLLECTABLE} | 
 | 107 | Print information on collectable objects found. | 
 | 108 | \end{datadesc} | 
 | 109 |  | 
 | 110 | \begin{datadesc}{DEBUG_UNCOLLECTABLE} | 
 | 111 | Print information of uncollectable objects found (objects which are | 
 | 112 | not reachable but cannot be freed by the collector).  These objects | 
 | 113 | will be added to the \code{garbage} list. | 
 | 114 | \end{datadesc} | 
 | 115 |  | 
 | 116 | \begin{datadesc}{DEBUG_INSTANCES} | 
 | 117 | When \constant{DEBUG_COLLECTABLE} or \constant{DEBUG_UNCOLLECTABLE} is | 
 | 118 | set, print information about instance objects found. | 
 | 119 | \end{datadesc} | 
 | 120 |  | 
 | 121 | \begin{datadesc}{DEBUG_OBJECTS} | 
 | 122 | When \constant{DEBUG_COLLECTABLE} or \constant{DEBUG_UNCOLLECTABLE} is | 
 | 123 | set, print information about objects other than instance objects found. | 
 | 124 | \end{datadesc} | 
 | 125 |  | 
| Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 126 | \begin{datadesc}{DEBUG_SAVEALL} | 
 | 127 | When set, all unreachable objects found will be appended to | 
 | 128 | \var{garbage} rather than being freed.  This can be useful for debugging | 
 | 129 | a leaking program. | 
 | 130 | \end{datadesc} | 
 | 131 |  | 
| Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 132 | \begin{datadesc}{DEBUG_LEAK} | 
 | 133 | The debugging flags necessary for the collector to print | 
 | 134 | information about a leaking program (equal to \code{DEBUG_COLLECTABLE | | 
| Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 135 | DEBUG_UNCOLLECTABLE | DEBUG_INSTANCES | DEBUG_OBJECTS | DEBUG_SAVEALL}).   | 
| Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 136 | \end{datadesc} |