blob: 310c5822b8c7b3f927c0bd4a96be742657268770 [file] [log] [blame]
Fred Drakebdcf91f2000-07-12 04:22:53 +00001\section{\module{gc} ---
2 Garbage Collector interface}
3
4\declaremodule{extension}{gc}
Fred Drakee5441912000-09-09 03:26:51 +00005\modulesynopsis{Interface to the cycle-detecting garbage collector.}
Neil Schemenauerb2c2c9e2000-10-04 16:34:09 +00006\moduleauthor{Neil Schemenauer}{nas@arctrix.com}
7\sectionauthor{Neil Schemenauer}{nas@arctrix.com}
Fred Drakebdcf91f2000-07-12 04:22:53 +00008
Fred Drakee5441912000-09-09 03:26:51 +00009The \module{gc} module is only available if the interpreter was built
10with the optional cyclic garbage detector (enabled by default). If
11this was not enabled, an \exception{ImportError} is raised by attempts
12to import this module.
13
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +000014This module provides an interface to the optional garbage collector. It
15provides the ability to disable the collector, tune the collection
Fred Drakebdcf91f2000-07-12 04:22:53 +000016frequency, and set debugging options. It also provides access to
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +000017unreachable objects that the collector found but cannot free. Since the
18collector supplements the reference counting already used in Python, you
19can disable the collector if you are sure your program does not create
20reference cycles. Automatic collection can be disabled by calling
21\code{gc.disable()}. To debug a leaking program call
Fred Drakebdcf91f2000-07-12 04:22:53 +000022\code{gc.set_debug(gc.DEBUG_LEAK)}.
23
24The \module{gc} module provides the following functions:
25
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +000026\begin{funcdesc}{enable}{}
27Enable automatic garbage collection.
28\end{funcdesc}
29
30\begin{funcdesc}{disable}{}
31Disable automatic garbage collection.
32\end{funcdesc}
33
34\begin{funcdesc}{isenabled}{}
35Returns true if automatic collection is enabled.
36\end{funcdesc}
37
Fred Drakebdcf91f2000-07-12 04:22:53 +000038\begin{funcdesc}{collect}{}
39Run a full collection. All generations are examined and the
40number of unreachable objects found is returned.
41\end{funcdesc}
42
43\begin{funcdesc}{set_debug}{flags}
44Set the garbage collection debugging flags.
45Debugging information will be written to \code{sys.stderr}. See below
46for a list of debugging flags which can be combined using bit
47operations to control debugging.
48\end{funcdesc}
49
50\begin{funcdesc}{get_debug}{}
51Return the debugging flags currently set.
52\end{funcdesc}
53
Fred Drakee4523c42002-07-10 19:21:07 +000054\begin{funcdesc}{get_objects}{}
55Returns a list of all objects tracked by the collector, excluding the
56list returned.
57\versionadded{2.2}
58\end{funcdesc}
59
Fred Drakebdcf91f2000-07-12 04:22:53 +000060\begin{funcdesc}{set_threshold}{threshold0\optional{,
61 threshold1\optional{, threshold2}}}
62Set the garbage collection thresholds (the collection frequency).
63Setting \var{threshold0} to zero disables collection.
64
65The GC classifies objects into three generations depending on how many
66collection sweeps they have survived. New objects are placed in the
67youngest generation (generation \code{0}). If an object survives a
68collection it is moved into the next older generation. Since
69generation \code{2} is the oldest generation, objects in that
70generation remain there after a collection. In order to decide when
71to run, the collector keeps track of the number object allocations and
72deallocations since the last collection. When the number of
73allocations minus the number of deallocations exceeds
74\var{threshold0}, collection starts. Initially only generation
75\code{0} is examined. If generation \code{0} has been examined more
76than \var{threshold1} times since generation \code{1} has been
77examined, then generation \code{1} is examined as well. Similarly,
78\var{threshold2} controls the number of collections of generation
79\code{1} before collecting generation \code{2}.
80\end{funcdesc}
81
82\begin{funcdesc}{get_threshold}{}
83Return the current collection thresholds as a tuple of
84\code{(\var{threshold0}, \var{threshold1}, \var{threshold2})}.
85\end{funcdesc}
86
Martin v. Löwis560da622001-11-24 09:24:51 +000087\begin{funcdesc}{get_referrers}{*objs}
88Return the list of objects that directly refer to any of objs. This
89function will only locate those containers which support garbage
90collection; extension types which do refer to other objects but do not
91support garbage collection will not be found.
Martin v. Löwisef180dc2002-01-26 20:11:50 +000092
93Note that objects which have already been dereferenced, but which live
94in cycles and have not yet been collected by the garbage collector can
95be listed among the resulting referrers. To get only currently live
96objects, call \function{collect()} before calling
97\function{get_referrers()}.
98
Martin v. Löwis560da622001-11-24 09:24:51 +000099\versionadded{2.2}
100\end{funcdesc}
Fred Drakebdcf91f2000-07-12 04:22:53 +0000101
Tim Petersaf0a8832001-11-18 04:51:17 +0000102The following variable is provided for read-only access (you can
103mutate its value but should not rebind it):
Fred Drakebdcf91f2000-07-12 04:22:53 +0000104
105\begin{datadesc}{garbage}
106A list of objects which the collector found to be unreachable
Tim Peters169ded02001-11-03 19:57:21 +0000107but could not be freed (uncollectable objects). By default, this list
108contains only objects with \method{__del__()} methods.\footnote{Prior to
109 Python 2.2, the list contained all instance objects in unreachable
110 cycles, not only those with \method{__del__()} methods.}
111Objects that have
Tim Petersaf0a8832001-11-18 04:51:17 +0000112\method{__del__()} methods and are part of a reference cycle cause
113the entire reference cycle to be uncollectable, including objects
114not necessarily in the cycle but reachable only from it. Python doesn't
115collect such cycles automatically because, in general, it isn't possible
116for Python to guess a safe order in which to run the \method{__del__()}
117methods. If you know a safe order, you can force the issue by examining
118the \var{garbage} list, and explicitly breaking cycles due to your
119objects within the list. Note that these objects are kept alive even
120so by virtue of being in the \var{garbage} list, so they should be
121removed from \var{garbage} too. For example, after breaking cycles, do
122\code{del gc.garbage[:]} to empty the list. It's generally better
123to avoid the issue by not creating cycles containing objects with
124\method{__del__()} methods, and \var{garbage} can be examined in that
Fred Drakedda7dcb2001-12-14 21:19:08 +0000125case to verify that no such cycles are being created.
Tim Petersaf0a8832001-11-18 04:51:17 +0000126
127If \constant{DEBUG_SAVEALL} is set, then all unreachable objects will
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000128be added to this list rather than freed.
Fred Drakebdcf91f2000-07-12 04:22:53 +0000129\end{datadesc}
130
131
132The following constants are provided for use with
133\function{set_debug()}:
134
135\begin{datadesc}{DEBUG_STATS}
136Print statistics during collection. This information can
137be useful when tuning the collection frequency.
138\end{datadesc}
139
140\begin{datadesc}{DEBUG_COLLECTABLE}
141Print information on collectable objects found.
142\end{datadesc}
143
144\begin{datadesc}{DEBUG_UNCOLLECTABLE}
145Print information of uncollectable objects found (objects which are
146not reachable but cannot be freed by the collector). These objects
147will be added to the \code{garbage} list.
148\end{datadesc}
149
150\begin{datadesc}{DEBUG_INSTANCES}
151When \constant{DEBUG_COLLECTABLE} or \constant{DEBUG_UNCOLLECTABLE} is
152set, print information about instance objects found.
153\end{datadesc}
154
155\begin{datadesc}{DEBUG_OBJECTS}
156When \constant{DEBUG_COLLECTABLE} or \constant{DEBUG_UNCOLLECTABLE} is
157set, print information about objects other than instance objects found.
158\end{datadesc}
159
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000160\begin{datadesc}{DEBUG_SAVEALL}
161When set, all unreachable objects found will be appended to
162\var{garbage} rather than being freed. This can be useful for debugging
163a leaking program.
164\end{datadesc}
165
Fred Drakebdcf91f2000-07-12 04:22:53 +0000166\begin{datadesc}{DEBUG_LEAK}
167The debugging flags necessary for the collector to print
168information about a leaking program (equal to \code{DEBUG_COLLECTABLE |
Tim Petersaf0a8832001-11-18 04:51:17 +0000169DEBUG_UNCOLLECTABLE | DEBUG_INSTANCES | DEBUG_OBJECTS | DEBUG_SAVEALL}).
Fred Drakebdcf91f2000-07-12 04:22:53 +0000170\end{datadesc}