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