blob: 95df2f83e493664f1595e99e182857de28668f83 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`gc` --- Garbage Collector interface
2=========================================
3
4.. module:: gc
5 :synopsis: Interface to the cycle-detecting garbage collector.
6.. moduleauthor:: Neil Schemenauer <nas@arctrix.com>
7.. sectionauthor:: Neil Schemenauer <nas@arctrix.com>
8
9
10This module provides an interface to the optional garbage collector. It
11provides the ability to disable the collector, tune the collection frequency,
12and set debugging options. It also provides access to unreachable objects that
13the collector found but cannot free. Since the collector supplements the
14reference counting already used in Python, you can disable the collector if you
15are sure your program does not create reference cycles. Automatic collection
16can be disabled by calling ``gc.disable()``. To debug a leaking program call
17``gc.set_debug(gc.DEBUG_LEAK)``. Notice that this includes
18``gc.DEBUG_SAVEALL``, causing garbage-collected objects to be saved in
19gc.garbage for inspection.
20
21The :mod:`gc` module provides the following functions:
22
23
24.. function:: enable()
25
26 Enable automatic garbage collection.
27
28
29.. function:: disable()
30
31 Disable automatic garbage collection.
32
33
34.. function:: isenabled()
35
36 Returns true if automatic collection is enabled.
37
38
Georg Brandl036490d2009-05-17 13:00:36 +000039.. function:: collect(generations=2)
Georg Brandl116aa622007-08-15 14:28:22 +000040
41 With no arguments, run a full collection. The optional argument *generation*
42 may be an integer specifying which generation to collect (from 0 to 2). A
43 :exc:`ValueError` is raised if the generation number is invalid. The number of
44 unreachable objects found is returned.
45
Georg Brandlc4a55fc2010-02-06 18:46:57 +000046 The free lists maintained for a number of built-in types are cleared
Georg Brandl2ee470f2008-07-16 12:55:28 +000047 whenever a full collection or collection of the highest generation (2)
48 is run. Not all items in some free lists may be freed due to the
49 particular implementation, in particular :class:`float`.
50
Georg Brandl116aa622007-08-15 14:28:22 +000051
52.. function:: set_debug(flags)
53
54 Set the garbage collection debugging flags. Debugging information will be
55 written to ``sys.stderr``. See below for a list of debugging flags which can be
56 combined using bit operations to control debugging.
57
58
59.. function:: get_debug()
60
61 Return the debugging flags currently set.
62
63
64.. function:: get_objects()
65
66 Returns a list of all objects tracked by the collector, excluding the list
67 returned.
68
Georg Brandl116aa622007-08-15 14:28:22 +000069
Antoine Pitroud4156c12012-10-30 22:43:19 +010070.. function:: get_stats()
71
72 Return a list of 3 per-generation dictionaries containing collection
73 statistics since interpreter start. At this moment, each dictionary will
74 contain the following items:
75
76 * ``collections`` is the number of times this generation was collected;
77
78 * ``collected`` is the total number of objects collected inside this
79 generation;
80
81 * ``uncollectable`` is the total number of objects which were found
82 to be uncollectable (and were therefore moved to the :data:`garbage`
83 list) inside this generation.
84
85 .. versionadded:: 3.4
86
87
Georg Brandl116aa622007-08-15 14:28:22 +000088.. function:: set_threshold(threshold0[, threshold1[, threshold2]])
89
90 Set the garbage collection thresholds (the collection frequency). Setting
91 *threshold0* to zero disables collection.
92
93 The GC classifies objects into three generations depending on how many
94 collection sweeps they have survived. New objects are placed in the youngest
95 generation (generation ``0``). If an object survives a collection it is moved
96 into the next older generation. Since generation ``2`` is the oldest
97 generation, objects in that generation remain there after a collection. In
98 order to decide when to run, the collector keeps track of the number object
99 allocations and deallocations since the last collection. When the number of
100 allocations minus the number of deallocations exceeds *threshold0*, collection
101 starts. Initially only generation ``0`` is examined. If generation ``0`` has
102 been examined more than *threshold1* times since generation ``1`` has been
103 examined, then generation ``1`` is examined as well. Similarly, *threshold2*
104 controls the number of collections of generation ``1`` before collecting
105 generation ``2``.
106
107
108.. function:: get_count()
109
110 Return the current collection counts as a tuple of ``(count0, count1,
111 count2)``.
112
Georg Brandl116aa622007-08-15 14:28:22 +0000113
114.. function:: get_threshold()
115
116 Return the current collection thresholds as a tuple of ``(threshold0,
117 threshold1, threshold2)``.
118
119
120.. function:: get_referrers(*objs)
121
122 Return the list of objects that directly refer to any of objs. This function
123 will only locate those containers which support garbage collection; extension
124 types which do refer to other objects but do not support garbage collection will
125 not be found.
126
127 Note that objects which have already been dereferenced, but which live in cycles
128 and have not yet been collected by the garbage collector can be listed among the
129 resulting referrers. To get only currently live objects, call :func:`collect`
130 before calling :func:`get_referrers`.
131
132 Care must be taken when using objects returned by :func:`get_referrers` because
133 some of them could still be under construction and hence in a temporarily
134 invalid state. Avoid using :func:`get_referrers` for any purpose other than
135 debugging.
136
Georg Brandl116aa622007-08-15 14:28:22 +0000137
138.. function:: get_referents(*objs)
139
140 Return a list of objects directly referred to by any of the arguments. The
141 referents returned are those objects visited by the arguments' C-level
142 :attr:`tp_traverse` methods (if any), and may not be all objects actually
143 directly reachable. :attr:`tp_traverse` methods are supported only by objects
144 that support garbage collection, and are only required to visit objects that may
145 be involved in a cycle. So, for example, if an integer is directly reachable
146 from an argument, that integer object may or may not appear in the result list.
147
Georg Brandl116aa622007-08-15 14:28:22 +0000148
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000149.. function:: is_tracked(obj)
150
151 Returns True if the object is currently tracked by the garbage collector,
152 False otherwise. As a general rule, instances of atomic types aren't
153 tracked and instances of non-atomic types (containers, user-defined
154 objects...) are. However, some type-specific optimizations can be present
155 in order to suppress the garbage collector footprint of simple instances
156 (e.g. dicts containing only atomic keys and values)::
157
158 >>> gc.is_tracked(0)
159 False
160 >>> gc.is_tracked("a")
161 False
162 >>> gc.is_tracked([])
163 True
164 >>> gc.is_tracked({})
165 False
166 >>> gc.is_tracked({"a": 1})
167 False
168 >>> gc.is_tracked({"a": []})
169 True
170
Georg Brandl705d9d52009-05-05 09:29:50 +0000171 .. versionadded:: 3.1
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000172
173
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +0000174The following variables are provided for read-only access (you can mutate the
175values but should not rebind them):
Georg Brandl116aa622007-08-15 14:28:22 +0000176
Georg Brandl116aa622007-08-15 14:28:22 +0000177.. data:: garbage
178
179 A list of objects which the collector found to be unreachable but could not be
180 freed (uncollectable objects). By default, this list contains only objects with
Amaury Forgeot d'Arcad8dcd52007-12-10 23:58:35 +0000181 :meth:`__del__` methods. Objects that have :meth:`__del__` methods and are
Georg Brandl116aa622007-08-15 14:28:22 +0000182 part of a reference cycle cause the entire reference cycle to be uncollectable,
183 including objects not necessarily in the cycle but reachable only from it.
184 Python doesn't collect such cycles automatically because, in general, it isn't
185 possible for Python to guess a safe order in which to run the :meth:`__del__`
186 methods. If you know a safe order, you can force the issue by examining the
187 *garbage* list, and explicitly breaking cycles due to your objects within the
188 list. Note that these objects are kept alive even so by virtue of being in the
189 *garbage* list, so they should be removed from *garbage* too. For example,
190 after breaking cycles, do ``del gc.garbage[:]`` to empty the list. It's
191 generally better to avoid the issue by not creating cycles containing objects
192 with :meth:`__del__` methods, and *garbage* can be examined in that case to
193 verify that no such cycles are being created.
194
Georg Brandl08be72d2010-10-24 15:11:22 +0000195 If :const:`DEBUG_SAVEALL` is set, then all unreachable objects will be added
196 to this list rather than freed.
Georg Brandl116aa622007-08-15 14:28:22 +0000197
Antoine Pitrou696e0352010-08-08 22:18:46 +0000198 .. versionchanged:: 3.2
Georg Brandl08be72d2010-10-24 15:11:22 +0000199 If this list is non-empty at interpreter shutdown, a
200 :exc:`ResourceWarning` is emitted, which is silent by default. If
201 :const:`DEBUG_UNCOLLECTABLE` is set, in addition all uncollectable objects
202 are printed.
Antoine Pitrou696e0352010-08-08 22:18:46 +0000203
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +0000204.. data:: callbacks
205
206 A list of callbacks that will be invoked by the garbage collector before and
207 after collection. The callbacks will be called with two arguments,
Brian Curtinc07bda02012-04-16 15:24:02 -0500208 *phase* and *info*.
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +0000209
Gregory P. Smith89bbe682012-09-30 10:36:07 -0700210 *phase* can be one of two values:
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +0000211
212 "start": The garbage collection is about to start.
213
214 "stop": The garbage collection has finished.
215
Gregory P. Smith89bbe682012-09-30 10:36:07 -0700216 *info* is a dict providing more information for the callback. The following
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +0000217 keys are currently defined:
218
219 "generation": The oldest generation being collected.
220
Brian Curtinc07bda02012-04-16 15:24:02 -0500221 "collected": When *phase* is "stop", the number of objects
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +0000222 successfully collected.
223
Gregory P. Smith89bbe682012-09-30 10:36:07 -0700224 "uncollectable": When *phase* is "stop", the number of objects
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +0000225 that could not be collected and were put in :data:`garbage`.
226
227 Applications can add their own callbacks to this list. The primary
228 use cases are:
229
230 Gathering statistics about garbage collection, such as how often
231 various generations are collected, and how long the collection
232 takes.
233
234 Allowing applications to identify and clear their own uncollectable
235 types when they appear in :data:`garbage`.
236
237 .. versionadded:: 3.3
238
Antoine Pitrou696e0352010-08-08 22:18:46 +0000239
Georg Brandl116aa622007-08-15 14:28:22 +0000240The following constants are provided for use with :func:`set_debug`:
241
242
243.. data:: DEBUG_STATS
244
245 Print statistics during collection. This information can be useful when tuning
246 the collection frequency.
247
248
249.. data:: DEBUG_COLLECTABLE
250
251 Print information on collectable objects found.
252
253
254.. data:: DEBUG_UNCOLLECTABLE
255
256 Print information of uncollectable objects found (objects which are not
Georg Brandl08be72d2010-10-24 15:11:22 +0000257 reachable but cannot be freed by the collector). These objects will be added
258 to the ``garbage`` list.
Georg Brandl116aa622007-08-15 14:28:22 +0000259
Antoine Pitrou696e0352010-08-08 22:18:46 +0000260 .. versionchanged:: 3.2
261 Also print the contents of the :data:`garbage` list at interpreter
Georg Brandl08be72d2010-10-24 15:11:22 +0000262 shutdown, if it isn't empty.
Georg Brandl116aa622007-08-15 14:28:22 +0000263
Georg Brandl116aa622007-08-15 14:28:22 +0000264.. data:: DEBUG_SAVEALL
265
266 When set, all unreachable objects found will be appended to *garbage* rather
267 than being freed. This can be useful for debugging a leaking program.
268
269
270.. data:: DEBUG_LEAK
271
272 The debugging flags necessary for the collector to print information about a
273 leaking program (equal to ``DEBUG_COLLECTABLE | DEBUG_UNCOLLECTABLE |
Amaury Forgeot d'Arcad8dcd52007-12-10 23:58:35 +0000274 DEBUG_SAVEALL``).