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