blob: 2d85cd3431711ab603ba517858725c5c06ec0e91 [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.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Georg Brandl116aa622007-08-15 14:28:22 +00007.. moduleauthor:: Neil Schemenauer <nas@arctrix.com>
8.. sectionauthor:: Neil Schemenauer <nas@arctrix.com>
9
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040010--------------
Georg Brandl116aa622007-08-15 14:28:22 +000011
12This module provides an interface to the optional garbage collector. It
13provides the ability to disable the collector, tune the collection frequency,
14and set debugging options. It also provides access to unreachable objects that
15the collector found but cannot free. Since the collector supplements the
16reference counting already used in Python, you can disable the collector if you
17are sure your program does not create reference cycles. Automatic collection
18can be disabled by calling ``gc.disable()``. To debug a leaking program call
19``gc.set_debug(gc.DEBUG_LEAK)``. Notice that this includes
20``gc.DEBUG_SAVEALL``, causing garbage-collected objects to be saved in
21gc.garbage for inspection.
22
23The :mod:`gc` module provides the following functions:
24
25
26.. function:: enable()
27
28 Enable automatic garbage collection.
29
30
31.. function:: disable()
32
33 Disable automatic garbage collection.
34
35
36.. function:: isenabled()
37
Serhiy Storchaka138ccbb2019-11-12 16:57:03 +020038 Return ``True`` if automatic collection is enabled.
Georg Brandl116aa622007-08-15 14:28:22 +000039
40
Benjamin Petersonbc51a8a2016-10-24 23:00:03 -070041.. function:: collect(generation=2)
Georg Brandl116aa622007-08-15 14:28:22 +000042
43 With no arguments, run a full collection. The optional argument *generation*
44 may be an integer specifying which generation to collect (from 0 to 2). A
45 :exc:`ValueError` is raised if the generation number is invalid. The number of
46 unreachable objects found is returned.
47
Georg Brandlc4a55fc2010-02-06 18:46:57 +000048 The free lists maintained for a number of built-in types are cleared
Georg Brandl2ee470f2008-07-16 12:55:28 +000049 whenever a full collection or collection of the highest generation (2)
50 is run. Not all items in some free lists may be freed due to the
51 particular implementation, in particular :class:`float`.
52
Georg Brandl116aa622007-08-15 14:28:22 +000053
54.. function:: set_debug(flags)
55
56 Set the garbage collection debugging flags. Debugging information will be
57 written to ``sys.stderr``. See below for a list of debugging flags which can be
58 combined using bit operations to control debugging.
59
60
61.. function:: get_debug()
62
63 Return the debugging flags currently set.
64
65
Pablo Galindo175421b2019-02-23 03:02:06 +000066.. function:: get_objects(generation=None)
Georg Brandl116aa622007-08-15 14:28:22 +000067
68 Returns a list of all objects tracked by the collector, excluding the list
Pablo Galindo175421b2019-02-23 03:02:06 +000069 returned. If *generation* is not None, return only the objects tracked by
70 the collector that are in that generation.
Georg Brandl116aa622007-08-15 14:28:22 +000071
Pablo Galindo175421b2019-02-23 03:02:06 +000072 .. versionchanged:: 3.8
73 New *generation* parameter.
Georg Brandl116aa622007-08-15 14:28:22 +000074
Antoine Pitroud4156c12012-10-30 22:43:19 +010075.. function:: get_stats()
76
R David Murray0e814632013-12-26 15:11:28 -050077 Return a list of three per-generation dictionaries containing collection
78 statistics since interpreter start. The number of keys may change
79 in the future, but currently each dictionary will contain the following
80 items:
Antoine Pitroud4156c12012-10-30 22:43:19 +010081
82 * ``collections`` is the number of times this generation was collected;
83
84 * ``collected`` is the total number of objects collected inside this
85 generation;
86
87 * ``uncollectable`` is the total number of objects which were found
88 to be uncollectable (and were therefore moved to the :data:`garbage`
89 list) inside this generation.
90
91 .. versionadded:: 3.4
92
93
Georg Brandl116aa622007-08-15 14:28:22 +000094.. function:: set_threshold(threshold0[, threshold1[, threshold2]])
95
96 Set the garbage collection thresholds (the collection frequency). Setting
97 *threshold0* to zero disables collection.
98
99 The GC classifies objects into three generations depending on how many
100 collection sweeps they have survived. New objects are placed in the youngest
101 generation (generation ``0``). If an object survives a collection it is moved
102 into the next older generation. Since generation ``2`` is the oldest
103 generation, objects in that generation remain there after a collection. In
104 order to decide when to run, the collector keeps track of the number object
105 allocations and deallocations since the last collection. When the number of
106 allocations minus the number of deallocations exceeds *threshold0*, collection
107 starts. Initially only generation ``0`` is examined. If generation ``0`` has
108 been examined more than *threshold1* times since generation ``1`` has been
Yaroslav Pankovych82ca8fa2020-08-08 21:48:21 +0300109 examined, then generation ``1`` is examined as well.
110 With the third generation, things are a bit more complicated,
111 see `Collecting the oldest generation <https://devguide.python.org/garbage_collector/#collecting-the-oldest-generation>`_ for more information.
Georg Brandl116aa622007-08-15 14:28:22 +0000112
113
114.. function:: get_count()
115
116 Return the current collection counts as a tuple of ``(count0, count1,
117 count2)``.
118
Georg Brandl116aa622007-08-15 14:28:22 +0000119
120.. function:: get_threshold()
121
122 Return the current collection thresholds as a tuple of ``(threshold0,
123 threshold1, threshold2)``.
124
125
126.. function:: get_referrers(*objs)
127
128 Return the list of objects that directly refer to any of objs. This function
129 will only locate those containers which support garbage collection; extension
130 types which do refer to other objects but do not support garbage collection will
131 not be found.
132
133 Note that objects which have already been dereferenced, but which live in cycles
134 and have not yet been collected by the garbage collector can be listed among the
135 resulting referrers. To get only currently live objects, call :func:`collect`
136 before calling :func:`get_referrers`.
137
138 Care must be taken when using objects returned by :func:`get_referrers` because
139 some of them could still be under construction and hence in a temporarily
140 invalid state. Avoid using :func:`get_referrers` for any purpose other than
141 debugging.
142
Georg Brandl116aa622007-08-15 14:28:22 +0000143
144.. function:: get_referents(*objs)
145
146 Return a list of objects directly referred to by any of the arguments. The
147 referents returned are those objects visited by the arguments' C-level
Antoine Pitrou39668f52013-08-01 21:12:45 +0200148 :c:member:`~PyTypeObject.tp_traverse` methods (if any), and may not be all objects actually
149 directly reachable. :c:member:`~PyTypeObject.tp_traverse` methods are supported only by objects
Georg Brandl116aa622007-08-15 14:28:22 +0000150 that support garbage collection, and are only required to visit objects that may
151 be involved in a cycle. So, for example, if an integer is directly reachable
152 from an argument, that integer object may or may not appear in the result list.
153
Georg Brandl116aa622007-08-15 14:28:22 +0000154
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000155.. function:: is_tracked(obj)
156
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200157 Returns ``True`` if the object is currently tracked by the garbage collector,
158 ``False`` otherwise. As a general rule, instances of atomic types aren't
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000159 tracked and instances of non-atomic types (containers, user-defined
160 objects...) are. However, some type-specific optimizations can be present
161 in order to suppress the garbage collector footprint of simple instances
162 (e.g. dicts containing only atomic keys and values)::
163
164 >>> gc.is_tracked(0)
165 False
166 >>> gc.is_tracked("a")
167 False
168 >>> gc.is_tracked([])
169 True
170 >>> gc.is_tracked({})
171 False
172 >>> gc.is_tracked({"a": 1})
173 False
174 >>> gc.is_tracked({"a": []})
175 True
176
Georg Brandl705d9d52009-05-05 09:29:50 +0000177 .. versionadded:: 3.1
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000178
179
Pablo Galindoa2ec3f02020-01-14 12:06:45 +0000180.. function:: is_finalized(obj)
181
182 Returns ``True`` if the given object has been finalized by the
183 garbage collector, ``False`` otherwise. ::
184
185 >>> x = None
186 >>> class Lazarus:
187 ... def __del__(self):
188 ... global x
189 ... x = self
190 ...
191 >>> lazarus = Lazarus()
192 >>> gc.is_finalized(lazarus)
193 False
194 >>> del lazarus
195 >>> gc.is_finalized(x)
196 True
197
198 .. versionadded:: 3.9
199
200
brainfvckc75edab2017-10-16 12:49:41 -0700201.. function:: freeze()
202
203 Freeze all the objects tracked by gc - move them to a permanent generation
204 and ignore all the future collections. This can be used before a POSIX
205 fork() call to make the gc copy-on-write friendly or to speed up collection.
206 Also collection before a POSIX fork() call may free pages for future
207 allocation which can cause copy-on-write too so it's advised to disable gc
Victor Stinner5e922652018-09-07 17:30:33 +0200208 in parent process and freeze before fork and enable gc in child process.
brainfvckc75edab2017-10-16 12:49:41 -0700209
210 .. versionadded:: 3.7
211
212
213.. function:: unfreeze()
214
215 Unfreeze the objects in the permanent generation, put them back into the
216 oldest generation.
217
218 .. versionadded:: 3.7
219
220
221.. function:: get_freeze_count()
222
223 Return the number of objects in the permanent generation.
224
225 .. versionadded:: 3.7
226
227
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +0000228The following variables are provided for read-only access (you can mutate the
229values but should not rebind them):
Georg Brandl116aa622007-08-15 14:28:22 +0000230
Georg Brandl116aa622007-08-15 14:28:22 +0000231.. data:: garbage
232
Antoine Pitrou796564c2013-07-30 19:59:21 +0200233 A list of objects which the collector found to be unreachable but could
234 not be freed (uncollectable objects). Starting with Python 3.4, this
235 list should be empty most of the time, except when using instances of
Serhiy Storchakae835b312019-10-30 21:37:16 +0200236 C extension types with a non-``NULL`` ``tp_del`` slot.
Georg Brandl116aa622007-08-15 14:28:22 +0000237
Antoine Pitrou796564c2013-07-30 19:59:21 +0200238 If :const:`DEBUG_SAVEALL` is set, then all unreachable objects will be
239 added to this list rather than freed.
Georg Brandl116aa622007-08-15 14:28:22 +0000240
Antoine Pitrou696e0352010-08-08 22:18:46 +0000241 .. versionchanged:: 3.2
Antoine Pitrou5db1bb82014-12-07 01:28:27 +0100242 If this list is non-empty at :term:`interpreter shutdown`, a
Georg Brandl08be72d2010-10-24 15:11:22 +0000243 :exc:`ResourceWarning` is emitted, which is silent by default. If
244 :const:`DEBUG_UNCOLLECTABLE` is set, in addition all uncollectable objects
245 are printed.
Antoine Pitrou696e0352010-08-08 22:18:46 +0000246
Antoine Pitrou796564c2013-07-30 19:59:21 +0200247 .. versionchanged:: 3.4
248 Following :pep:`442`, objects with a :meth:`__del__` method don't end
249 up in :attr:`gc.garbage` anymore.
250
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +0000251.. data:: callbacks
252
253 A list of callbacks that will be invoked by the garbage collector before and
254 after collection. The callbacks will be called with two arguments,
Brian Curtinc07bda02012-04-16 15:24:02 -0500255 *phase* and *info*.
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +0000256
Gregory P. Smith89bbe682012-09-30 10:36:07 -0700257 *phase* can be one of two values:
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +0000258
259 "start": The garbage collection is about to start.
260
261 "stop": The garbage collection has finished.
262
Gregory P. Smith89bbe682012-09-30 10:36:07 -0700263 *info* is a dict providing more information for the callback. The following
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +0000264 keys are currently defined:
265
266 "generation": The oldest generation being collected.
267
Brian Curtinc07bda02012-04-16 15:24:02 -0500268 "collected": When *phase* is "stop", the number of objects
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +0000269 successfully collected.
270
Gregory P. Smith89bbe682012-09-30 10:36:07 -0700271 "uncollectable": When *phase* is "stop", the number of objects
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +0000272 that could not be collected and were put in :data:`garbage`.
273
274 Applications can add their own callbacks to this list. The primary
275 use cases are:
276
277 Gathering statistics about garbage collection, such as how often
278 various generations are collected, and how long the collection
279 takes.
280
281 Allowing applications to identify and clear their own uncollectable
282 types when they appear in :data:`garbage`.
283
284 .. versionadded:: 3.3
285
Antoine Pitrou696e0352010-08-08 22:18:46 +0000286
Georg Brandl116aa622007-08-15 14:28:22 +0000287The following constants are provided for use with :func:`set_debug`:
288
289
290.. data:: DEBUG_STATS
291
292 Print statistics during collection. This information can be useful when tuning
293 the collection frequency.
294
295
296.. data:: DEBUG_COLLECTABLE
297
298 Print information on collectable objects found.
299
300
301.. data:: DEBUG_UNCOLLECTABLE
302
303 Print information of uncollectable objects found (objects which are not
Georg Brandl08be72d2010-10-24 15:11:22 +0000304 reachable but cannot be freed by the collector). These objects will be added
305 to the ``garbage`` list.
Georg Brandl116aa622007-08-15 14:28:22 +0000306
Antoine Pitrou696e0352010-08-08 22:18:46 +0000307 .. versionchanged:: 3.2
Antoine Pitrou5db1bb82014-12-07 01:28:27 +0100308 Also print the contents of the :data:`garbage` list at
309 :term:`interpreter shutdown`, if it isn't empty.
Georg Brandl116aa622007-08-15 14:28:22 +0000310
Georg Brandl116aa622007-08-15 14:28:22 +0000311.. data:: DEBUG_SAVEALL
312
313 When set, all unreachable objects found will be appended to *garbage* rather
314 than being freed. This can be useful for debugging a leaking program.
315
316
317.. data:: DEBUG_LEAK
318
319 The debugging flags necessary for the collector to print information about a
320 leaking program (equal to ``DEBUG_COLLECTABLE | DEBUG_UNCOLLECTABLE |
Amaury Forgeot d'Arcad8dcd52007-12-10 23:58:35 +0000321 DEBUG_SAVEALL``).