blob: 54ca26c6fea71065097bb6c1dd40b5c0e0bcec28 [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
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00009This module provides an interface to the optional garbage collector. It
10provides the ability to disable the collector, tune the collection
Fred Drakebdcf91f2000-07-12 04:22:53 +000011frequency, and set debugging options. It also provides access to
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +000012unreachable objects that the collector found but cannot free. Since the
13collector supplements the reference counting already used in Python, you
14can disable the collector if you are sure your program does not create
15reference cycles. Automatic collection can be disabled by calling
16\code{gc.disable()}. To debug a leaking program call
Martin v. Löwis0f9679d2005-03-28 15:29:28 +000017\code{gc.set_debug(gc.DEBUG_LEAK)}. Notice that this includes
18\code{gc.DEBUG_SAVEALL}, causing garbage-collected objects to be
19saved in gc.garbage for inspection.
Fred Drakebdcf91f2000-07-12 04:22:53 +000020
21The \module{gc} module provides the following functions:
22
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +000023\begin{funcdesc}{enable}{}
24Enable automatic garbage collection.
25\end{funcdesc}
26
27\begin{funcdesc}{disable}{}
28Disable automatic garbage collection.
29\end{funcdesc}
30
31\begin{funcdesc}{isenabled}{}
32Returns true if automatic collection is enabled.
33\end{funcdesc}
34
Barry Warsawd3c38ff2006-03-07 09:46:03 +000035\begin{funcdesc}{collect}{\optional{generation}}
36With no arguments, run a full collection. The optional argument
37\var{generation} may be an integer specifying which generation to collect
38(from 0 to 2). A ValueError is raised if the generation number is invalid.
39The number of unreachable objects found is returned.
40
41\versionchanged[The optional \var{generation} argument was added]{2.5}
Fred Drakebdcf91f2000-07-12 04:22:53 +000042\end{funcdesc}
43
44\begin{funcdesc}{set_debug}{flags}
45Set the garbage collection debugging flags.
46Debugging information will be written to \code{sys.stderr}. See below
47for a list of debugging flags which can be combined using bit
48operations to control debugging.
49\end{funcdesc}
50
51\begin{funcdesc}{get_debug}{}
52Return the debugging flags currently set.
53\end{funcdesc}
54
Fred Drakee4523c42002-07-10 19:21:07 +000055\begin{funcdesc}{get_objects}{}
56Returns a list of all objects tracked by the collector, excluding the
57list returned.
58\versionadded{2.2}
59\end{funcdesc}
60
Fred Drakebdcf91f2000-07-12 04:22:53 +000061\begin{funcdesc}{set_threshold}{threshold0\optional{,
62 threshold1\optional{, threshold2}}}
63Set the garbage collection thresholds (the collection frequency).
64Setting \var{threshold0} to zero disables collection.
65
66The GC classifies objects into three generations depending on how many
67collection sweeps they have survived. New objects are placed in the
68youngest generation (generation \code{0}). If an object survives a
69collection it is moved into the next older generation. Since
70generation \code{2} is the oldest generation, objects in that
71generation remain there after a collection. In order to decide when
72to run, the collector keeps track of the number object allocations and
73deallocations since the last collection. When the number of
74allocations minus the number of deallocations exceeds
75\var{threshold0}, collection starts. Initially only generation
76\code{0} is examined. If generation \code{0} has been examined more
77than \var{threshold1} times since generation \code{1} has been
78examined, then generation \code{1} is examined as well. Similarly,
79\var{threshold2} controls the number of collections of generation
80\code{1} before collecting generation \code{2}.
81\end{funcdesc}
82
Barry Warsawd3c38ff2006-03-07 09:46:03 +000083\begin{funcdesc}{get_count}{}
84Return the current collection counts as a tuple of
85\code{(\var{count0}, \var{count1}, \var{count2})}.
86\versionadded{2.5}
87\end{funcdesc}
88
Fred Drakebdcf91f2000-07-12 04:22:53 +000089\begin{funcdesc}{get_threshold}{}
90Return the current collection thresholds as a tuple of
91\code{(\var{threshold0}, \var{threshold1}, \var{threshold2})}.
92\end{funcdesc}
93
Martin v. Löwis560da622001-11-24 09:24:51 +000094\begin{funcdesc}{get_referrers}{*objs}
95Return the list of objects that directly refer to any of objs. This
96function will only locate those containers which support garbage
97collection; extension types which do refer to other objects but do not
98support garbage collection will not be found.
Martin v. Löwisef180dc2002-01-26 20:11:50 +000099
100Note that objects which have already been dereferenced, but which live
101in cycles and have not yet been collected by the garbage collector can
102be listed among the resulting referrers. To get only currently live
103objects, call \function{collect()} before calling
104\function{get_referrers()}.
105
Armin Rigo3be6d5d2003-10-28 12:10:38 +0000106Care must be taken when using objects returned by
107\function{get_referrers()} because some of them could still be under
108construction and hence in a temporarily invalid state. Avoid using
109\function{get_referrers()} for any purpose other than debugging.
110
Martin v. Löwis560da622001-11-24 09:24:51 +0000111\versionadded{2.2}
112\end{funcdesc}
Fred Drakebdcf91f2000-07-12 04:22:53 +0000113
Tim Peters730f5532003-04-08 17:17:17 +0000114\begin{funcdesc}{get_referents}{*objs}
Tim Peters0f81ab62003-04-08 16:39:48 +0000115Return a list of objects directly referred to by any of the arguments.
Tim Peters730f5532003-04-08 17:17:17 +0000116The referents returned are those objects visited by the arguments'
Fred Drakede7ad2c2003-04-08 17:37:47 +0000117C-level \member{tp_traverse} methods (if any), and may not be all
118objects actually directly reachable. \member{tp_traverse} methods
Tim Peters0f81ab62003-04-08 16:39:48 +0000119are supported only by objects that support garbage collection, and are
120only required to visit objects that may be involved in a cycle. So,
121for example, if an integer is directly reachable from an argument, that
122integer object may or may not appear in the result list.
123
124\versionadded{2.3}
125\end{funcdesc}
126
Tim Petersaf0a8832001-11-18 04:51:17 +0000127The following variable is provided for read-only access (you can
128mutate its value but should not rebind it):
Fred Drakebdcf91f2000-07-12 04:22:53 +0000129
130\begin{datadesc}{garbage}
131A list of objects which the collector found to be unreachable
Tim Peters169ded02001-11-03 19:57:21 +0000132but could not be freed (uncollectable objects). By default, this list
133contains only objects with \method{__del__()} methods.\footnote{Prior to
134 Python 2.2, the list contained all instance objects in unreachable
135 cycles, not only those with \method{__del__()} methods.}
136Objects that have
Tim Petersaf0a8832001-11-18 04:51:17 +0000137\method{__del__()} methods and are part of a reference cycle cause
138the entire reference cycle to be uncollectable, including objects
139not necessarily in the cycle but reachable only from it. Python doesn't
140collect such cycles automatically because, in general, it isn't possible
141for Python to guess a safe order in which to run the \method{__del__()}
142methods. If you know a safe order, you can force the issue by examining
143the \var{garbage} list, and explicitly breaking cycles due to your
144objects within the list. Note that these objects are kept alive even
145so by virtue of being in the \var{garbage} list, so they should be
146removed from \var{garbage} too. For example, after breaking cycles, do
147\code{del gc.garbage[:]} to empty the list. It's generally better
148to avoid the issue by not creating cycles containing objects with
149\method{__del__()} methods, and \var{garbage} can be examined in that
Fred Drakedda7dcb2001-12-14 21:19:08 +0000150case to verify that no such cycles are being created.
Tim Petersaf0a8832001-11-18 04:51:17 +0000151
152If \constant{DEBUG_SAVEALL} is set, then all unreachable objects will
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000153be added to this list rather than freed.
Fred Drakebdcf91f2000-07-12 04:22:53 +0000154\end{datadesc}
155
156
157The following constants are provided for use with
158\function{set_debug()}:
159
160\begin{datadesc}{DEBUG_STATS}
161Print statistics during collection. This information can
162be useful when tuning the collection frequency.
163\end{datadesc}
164
165\begin{datadesc}{DEBUG_COLLECTABLE}
166Print information on collectable objects found.
167\end{datadesc}
168
169\begin{datadesc}{DEBUG_UNCOLLECTABLE}
170Print information of uncollectable objects found (objects which are
171not reachable but cannot be freed by the collector). These objects
172will be added to the \code{garbage} list.
173\end{datadesc}
174
175\begin{datadesc}{DEBUG_INSTANCES}
176When \constant{DEBUG_COLLECTABLE} or \constant{DEBUG_UNCOLLECTABLE} is
177set, print information about instance objects found.
178\end{datadesc}
179
180\begin{datadesc}{DEBUG_OBJECTS}
181When \constant{DEBUG_COLLECTABLE} or \constant{DEBUG_UNCOLLECTABLE} is
182set, print information about objects other than instance objects found.
183\end{datadesc}
184
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000185\begin{datadesc}{DEBUG_SAVEALL}
186When set, all unreachable objects found will be appended to
187\var{garbage} rather than being freed. This can be useful for debugging
188a leaking program.
189\end{datadesc}
190
Fred Drakebdcf91f2000-07-12 04:22:53 +0000191\begin{datadesc}{DEBUG_LEAK}
192The debugging flags necessary for the collector to print
193information about a leaking program (equal to \code{DEBUG_COLLECTABLE |
Tim Petersaf0a8832001-11-18 04:51:17 +0000194DEBUG_UNCOLLECTABLE | DEBUG_INSTANCES | DEBUG_OBJECTS | DEBUG_SAVEALL}).
Fred Drakebdcf91f2000-07-12 04:22:53 +0000195\end{datadesc}