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