blob: 5a744c3967cb1f1e259c652b459b5d9965347a88 [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.}
Fred Drakebdcf91f2000-07-12 04:22:53 +00006\moduleauthor{Neil Schemenauer}{nascheme@enme.ucalgary.ca}
7\sectionauthor{Neil Schemenauer}{nascheme@enme.ucalgary.ca}
8
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
54\begin{funcdesc}{set_threshold}{threshold0\optional{,
55 threshold1\optional{, threshold2}}}
56Set the garbage collection thresholds (the collection frequency).
57Setting \var{threshold0} to zero disables collection.
58
59The GC classifies objects into three generations depending on how many
60collection sweeps they have survived. New objects are placed in the
61youngest generation (generation \code{0}). If an object survives a
62collection it is moved into the next older generation. Since
63generation \code{2} is the oldest generation, objects in that
64generation remain there after a collection. In order to decide when
65to run, the collector keeps track of the number object allocations and
66deallocations since the last collection. When the number of
67allocations minus the number of deallocations exceeds
68\var{threshold0}, collection starts. Initially only generation
69\code{0} is examined. If generation \code{0} has been examined more
70than \var{threshold1} times since generation \code{1} has been
71examined, then generation \code{1} is examined as well. Similarly,
72\var{threshold2} controls the number of collections of generation
73\code{1} before collecting generation \code{2}.
74\end{funcdesc}
75
76\begin{funcdesc}{get_threshold}{}
77Return the current collection thresholds as a tuple of
78\code{(\var{threshold0}, \var{threshold1}, \var{threshold2})}.
79\end{funcdesc}
80
81
82The following variable is provided for read-only access:
83
84\begin{datadesc}{garbage}
85A list of objects which the collector found to be unreachable
86but could not be freed (uncollectable objects). Objects that have
87\method{__del__()} methods and create part of a reference cycle cause
88the entire reference cycle to be uncollectable.
89\end{datadesc}
90
91
92The following constants are provided for use with
93\function{set_debug()}:
94
95\begin{datadesc}{DEBUG_STATS}
96Print statistics during collection. This information can
97be useful when tuning the collection frequency.
98\end{datadesc}
99
100\begin{datadesc}{DEBUG_COLLECTABLE}
101Print information on collectable objects found.
102\end{datadesc}
103
104\begin{datadesc}{DEBUG_UNCOLLECTABLE}
105Print information of uncollectable objects found (objects which are
106not reachable but cannot be freed by the collector). These objects
107will be added to the \code{garbage} list.
108\end{datadesc}
109
110\begin{datadesc}{DEBUG_INSTANCES}
111When \constant{DEBUG_COLLECTABLE} or \constant{DEBUG_UNCOLLECTABLE} is
112set, print information about instance objects found.
113\end{datadesc}
114
115\begin{datadesc}{DEBUG_OBJECTS}
116When \constant{DEBUG_COLLECTABLE} or \constant{DEBUG_UNCOLLECTABLE} is
117set, print information about objects other than instance objects found.
118\end{datadesc}
119
120\begin{datadesc}{DEBUG_LEAK}
121The debugging flags necessary for the collector to print
122information about a leaking program (equal to \code{DEBUG_COLLECTABLE |
123DEBUG_UNCOLLECTABLE | DEBUG_INSTANCES | DEBUG_OBJECTS}).
124\end{datadesc}