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