Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`gc` --- Garbage Collector interface |
| 2 | ========================================= |
| 3 | |
| 4 | .. module:: gc |
| 5 | :synopsis: Interface to the cycle-detecting garbage collector. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 7 | .. moduleauthor:: Neil Schemenauer <nas@arctrix.com> |
| 8 | .. sectionauthor:: Neil Schemenauer <nas@arctrix.com> |
| 9 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 10 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | |
| 12 | This module provides an interface to the optional garbage collector. It |
| 13 | provides the ability to disable the collector, tune the collection frequency, |
| 14 | and set debugging options. It also provides access to unreachable objects that |
| 15 | the collector found but cannot free. Since the collector supplements the |
| 16 | reference counting already used in Python, you can disable the collector if you |
| 17 | are sure your program does not create reference cycles. Automatic collection |
| 18 | can 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 |
| 21 | gc.garbage for inspection. |
| 22 | |
| 23 | The :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 | |
Pablo Galindo | 72a0d21 | 2018-01-29 20:37:09 +0000 | [diff] [blame] | 36 | .. class:: ensure_disabled() |
| 37 | |
| 38 | Return a context manager object that disables the garbage collector and reenables the previous |
| 39 | state upon completion of the block. This is basically equivalent to:: |
| 40 | |
| 41 | from gc import enable, disable, isenabled |
| 42 | |
| 43 | @contextmanager |
| 44 | def ensure_disabled(): |
| 45 | was_enabled_previously = isenabled() |
| 46 | gc.disable() |
| 47 | yield |
| 48 | if was_enabled_previously: |
| 49 | gc.enable() |
| 50 | |
| 51 | And lets you write code like this:: |
| 52 | |
| 53 | with ensure_disabled(): |
| 54 | run_some_timing() |
| 55 | |
| 56 | with ensure_disabled(): |
| 57 | # do_something_that_has_real_time_guarantees |
| 58 | # such as a pair trade, robotic braking, etc |
| 59 | |
| 60 | without needing to explicitly enable and disable the garbage collector yourself. |
| 61 | This context manager is implemented in C to assure atomicity, thread safety and speed. |
| 62 | |
| 63 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 64 | .. function:: isenabled() |
| 65 | |
| 66 | Returns true if automatic collection is enabled. |
| 67 | |
| 68 | |
Benjamin Peterson | bc51a8a | 2016-10-24 23:00:03 -0700 | [diff] [blame] | 69 | .. function:: collect(generation=2) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 70 | |
| 71 | With no arguments, run a full collection. The optional argument *generation* |
| 72 | may be an integer specifying which generation to collect (from 0 to 2). A |
| 73 | :exc:`ValueError` is raised if the generation number is invalid. The number of |
| 74 | unreachable objects found is returned. |
| 75 | |
Georg Brandl | c4a55fc | 2010-02-06 18:46:57 +0000 | [diff] [blame] | 76 | The free lists maintained for a number of built-in types are cleared |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 77 | whenever a full collection or collection of the highest generation (2) |
| 78 | is run. Not all items in some free lists may be freed due to the |
| 79 | particular implementation, in particular :class:`float`. |
| 80 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
| 82 | .. function:: set_debug(flags) |
| 83 | |
| 84 | Set the garbage collection debugging flags. Debugging information will be |
| 85 | written to ``sys.stderr``. See below for a list of debugging flags which can be |
| 86 | combined using bit operations to control debugging. |
| 87 | |
| 88 | |
| 89 | .. function:: get_debug() |
| 90 | |
| 91 | Return the debugging flags currently set. |
| 92 | |
| 93 | |
| 94 | .. function:: get_objects() |
| 95 | |
| 96 | Returns a list of all objects tracked by the collector, excluding the list |
| 97 | returned. |
| 98 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 99 | |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 100 | .. function:: get_stats() |
| 101 | |
R David Murray | 0e81463 | 2013-12-26 15:11:28 -0500 | [diff] [blame] | 102 | Return a list of three per-generation dictionaries containing collection |
| 103 | statistics since interpreter start. The number of keys may change |
| 104 | in the future, but currently each dictionary will contain the following |
| 105 | items: |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 106 | |
| 107 | * ``collections`` is the number of times this generation was collected; |
| 108 | |
| 109 | * ``collected`` is the total number of objects collected inside this |
| 110 | generation; |
| 111 | |
| 112 | * ``uncollectable`` is the total number of objects which were found |
| 113 | to be uncollectable (and were therefore moved to the :data:`garbage` |
| 114 | list) inside this generation. |
| 115 | |
| 116 | .. versionadded:: 3.4 |
| 117 | |
| 118 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 119 | .. function:: set_threshold(threshold0[, threshold1[, threshold2]]) |
| 120 | |
| 121 | Set the garbage collection thresholds (the collection frequency). Setting |
| 122 | *threshold0* to zero disables collection. |
| 123 | |
| 124 | The GC classifies objects into three generations depending on how many |
| 125 | collection sweeps they have survived. New objects are placed in the youngest |
| 126 | generation (generation ``0``). If an object survives a collection it is moved |
| 127 | into the next older generation. Since generation ``2`` is the oldest |
| 128 | generation, objects in that generation remain there after a collection. In |
| 129 | order to decide when to run, the collector keeps track of the number object |
| 130 | allocations and deallocations since the last collection. When the number of |
| 131 | allocations minus the number of deallocations exceeds *threshold0*, collection |
| 132 | starts. Initially only generation ``0`` is examined. If generation ``0`` has |
| 133 | been examined more than *threshold1* times since generation ``1`` has been |
| 134 | examined, then generation ``1`` is examined as well. Similarly, *threshold2* |
| 135 | controls the number of collections of generation ``1`` before collecting |
| 136 | generation ``2``. |
| 137 | |
| 138 | |
| 139 | .. function:: get_count() |
| 140 | |
| 141 | Return the current collection counts as a tuple of ``(count0, count1, |
| 142 | count2)``. |
| 143 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 144 | |
| 145 | .. function:: get_threshold() |
| 146 | |
| 147 | Return the current collection thresholds as a tuple of ``(threshold0, |
| 148 | threshold1, threshold2)``. |
| 149 | |
| 150 | |
| 151 | .. function:: get_referrers(*objs) |
| 152 | |
| 153 | Return the list of objects that directly refer to any of objs. This function |
| 154 | will only locate those containers which support garbage collection; extension |
| 155 | types which do refer to other objects but do not support garbage collection will |
| 156 | not be found. |
| 157 | |
| 158 | Note that objects which have already been dereferenced, but which live in cycles |
| 159 | and have not yet been collected by the garbage collector can be listed among the |
| 160 | resulting referrers. To get only currently live objects, call :func:`collect` |
| 161 | before calling :func:`get_referrers`. |
| 162 | |
| 163 | Care must be taken when using objects returned by :func:`get_referrers` because |
| 164 | some of them could still be under construction and hence in a temporarily |
| 165 | invalid state. Avoid using :func:`get_referrers` for any purpose other than |
| 166 | debugging. |
| 167 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 168 | |
| 169 | .. function:: get_referents(*objs) |
| 170 | |
| 171 | Return a list of objects directly referred to by any of the arguments. The |
| 172 | referents returned are those objects visited by the arguments' C-level |
Antoine Pitrou | 39668f5 | 2013-08-01 21:12:45 +0200 | [diff] [blame] | 173 | :c:member:`~PyTypeObject.tp_traverse` methods (if any), and may not be all objects actually |
| 174 | directly reachable. :c:member:`~PyTypeObject.tp_traverse` methods are supported only by objects |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 175 | that support garbage collection, and are only required to visit objects that may |
| 176 | be involved in a cycle. So, for example, if an integer is directly reachable |
| 177 | from an argument, that integer object may or may not appear in the result list. |
| 178 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 179 | |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 180 | .. function:: is_tracked(obj) |
| 181 | |
Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 182 | Returns ``True`` if the object is currently tracked by the garbage collector, |
| 183 | ``False`` otherwise. As a general rule, instances of atomic types aren't |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 184 | tracked and instances of non-atomic types (containers, user-defined |
| 185 | objects...) are. However, some type-specific optimizations can be present |
| 186 | in order to suppress the garbage collector footprint of simple instances |
| 187 | (e.g. dicts containing only atomic keys and values):: |
| 188 | |
| 189 | >>> gc.is_tracked(0) |
| 190 | False |
| 191 | >>> gc.is_tracked("a") |
| 192 | False |
| 193 | >>> gc.is_tracked([]) |
| 194 | True |
| 195 | >>> gc.is_tracked({}) |
| 196 | False |
| 197 | >>> gc.is_tracked({"a": 1}) |
| 198 | False |
| 199 | >>> gc.is_tracked({"a": []}) |
| 200 | True |
| 201 | |
Georg Brandl | 705d9d5 | 2009-05-05 09:29:50 +0000 | [diff] [blame] | 202 | .. versionadded:: 3.1 |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 203 | |
| 204 | |
brainfvck | c75edab | 2017-10-16 12:49:41 -0700 | [diff] [blame] | 205 | .. function:: freeze() |
| 206 | |
| 207 | Freeze all the objects tracked by gc - move them to a permanent generation |
| 208 | and ignore all the future collections. This can be used before a POSIX |
| 209 | fork() call to make the gc copy-on-write friendly or to speed up collection. |
| 210 | Also collection before a POSIX fork() call may free pages for future |
| 211 | allocation which can cause copy-on-write too so it's advised to disable gc |
| 212 | in master process and freeze before fork and enable gc in child process. |
| 213 | |
| 214 | .. versionadded:: 3.7 |
| 215 | |
| 216 | |
| 217 | .. function:: unfreeze() |
| 218 | |
| 219 | Unfreeze the objects in the permanent generation, put them back into the |
| 220 | oldest generation. |
| 221 | |
| 222 | .. versionadded:: 3.7 |
| 223 | |
| 224 | |
| 225 | .. function:: get_freeze_count() |
| 226 | |
| 227 | Return the number of objects in the permanent generation. |
| 228 | |
| 229 | .. versionadded:: 3.7 |
| 230 | |
| 231 | |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 232 | The following variables are provided for read-only access (you can mutate the |
| 233 | values but should not rebind them): |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 234 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 235 | .. data:: garbage |
| 236 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 237 | A list of objects which the collector found to be unreachable but could |
| 238 | not be freed (uncollectable objects). Starting with Python 3.4, this |
| 239 | list should be empty most of the time, except when using instances of |
| 240 | C extension types with a non-NULL ``tp_del`` slot. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 241 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 242 | If :const:`DEBUG_SAVEALL` is set, then all unreachable objects will be |
| 243 | added to this list rather than freed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 244 | |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 245 | .. versionchanged:: 3.2 |
Antoine Pitrou | 5db1bb8 | 2014-12-07 01:28:27 +0100 | [diff] [blame] | 246 | If this list is non-empty at :term:`interpreter shutdown`, a |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 247 | :exc:`ResourceWarning` is emitted, which is silent by default. If |
| 248 | :const:`DEBUG_UNCOLLECTABLE` is set, in addition all uncollectable objects |
| 249 | are printed. |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 250 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 251 | .. versionchanged:: 3.4 |
| 252 | Following :pep:`442`, objects with a :meth:`__del__` method don't end |
| 253 | up in :attr:`gc.garbage` anymore. |
| 254 | |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 255 | .. data:: callbacks |
| 256 | |
| 257 | A list of callbacks that will be invoked by the garbage collector before and |
| 258 | after collection. The callbacks will be called with two arguments, |
Brian Curtin | c07bda0 | 2012-04-16 15:24:02 -0500 | [diff] [blame] | 259 | *phase* and *info*. |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 260 | |
Gregory P. Smith | 89bbe68 | 2012-09-30 10:36:07 -0700 | [diff] [blame] | 261 | *phase* can be one of two values: |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 262 | |
| 263 | "start": The garbage collection is about to start. |
| 264 | |
| 265 | "stop": The garbage collection has finished. |
| 266 | |
Gregory P. Smith | 89bbe68 | 2012-09-30 10:36:07 -0700 | [diff] [blame] | 267 | *info* is a dict providing more information for the callback. The following |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 268 | keys are currently defined: |
| 269 | |
| 270 | "generation": The oldest generation being collected. |
| 271 | |
Brian Curtin | c07bda0 | 2012-04-16 15:24:02 -0500 | [diff] [blame] | 272 | "collected": When *phase* is "stop", the number of objects |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 273 | successfully collected. |
| 274 | |
Gregory P. Smith | 89bbe68 | 2012-09-30 10:36:07 -0700 | [diff] [blame] | 275 | "uncollectable": When *phase* is "stop", the number of objects |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 276 | that could not be collected and were put in :data:`garbage`. |
| 277 | |
| 278 | Applications can add their own callbacks to this list. The primary |
| 279 | use cases are: |
| 280 | |
| 281 | Gathering statistics about garbage collection, such as how often |
| 282 | various generations are collected, and how long the collection |
| 283 | takes. |
| 284 | |
| 285 | Allowing applications to identify and clear their own uncollectable |
| 286 | types when they appear in :data:`garbage`. |
| 287 | |
| 288 | .. versionadded:: 3.3 |
| 289 | |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 290 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 291 | The following constants are provided for use with :func:`set_debug`: |
| 292 | |
| 293 | |
| 294 | .. data:: DEBUG_STATS |
| 295 | |
| 296 | Print statistics during collection. This information can be useful when tuning |
| 297 | the collection frequency. |
| 298 | |
| 299 | |
| 300 | .. data:: DEBUG_COLLECTABLE |
| 301 | |
| 302 | Print information on collectable objects found. |
| 303 | |
| 304 | |
| 305 | .. data:: DEBUG_UNCOLLECTABLE |
| 306 | |
| 307 | Print information of uncollectable objects found (objects which are not |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 308 | reachable but cannot be freed by the collector). These objects will be added |
| 309 | to the ``garbage`` list. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 310 | |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 311 | .. versionchanged:: 3.2 |
Antoine Pitrou | 5db1bb8 | 2014-12-07 01:28:27 +0100 | [diff] [blame] | 312 | Also print the contents of the :data:`garbage` list at |
| 313 | :term:`interpreter shutdown`, if it isn't empty. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 314 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 315 | .. data:: DEBUG_SAVEALL |
| 316 | |
| 317 | When set, all unreachable objects found will be appended to *garbage* rather |
| 318 | than being freed. This can be useful for debugging a leaking program. |
| 319 | |
| 320 | |
| 321 | .. data:: DEBUG_LEAK |
| 322 | |
| 323 | The debugging flags necessary for the collector to print information about a |
| 324 | leaking program (equal to ``DEBUG_COLLECTABLE | DEBUG_UNCOLLECTABLE | |
Amaury Forgeot d'Arc | ad8dcd5 | 2007-12-10 23:58:35 +0000 | [diff] [blame] | 325 | DEBUG_SAVEALL``). |