blob: 7c425e3ec62a8d01bb41c13cb95edca4aee27724 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`gc` --- Garbage Collector interface
3=========================================
4
5.. module:: gc
6 :synopsis: Interface to the cycle-detecting garbage collector.
7.. moduleauthor:: Neil Schemenauer <nas@arctrix.com>
8.. sectionauthor:: Neil Schemenauer <nas@arctrix.com>
9
10
11This module provides an interface to the optional garbage collector. It
12provides the ability to disable the collector, tune the collection frequency,
13and set debugging options. It also provides access to unreachable objects that
14the collector found but cannot free. Since the collector supplements the
15reference counting already used in Python, you can disable the collector if you
16are sure your program does not create reference cycles. Automatic collection
17can be disabled by calling ``gc.disable()``. To debug a leaking program call
18``gc.set_debug(gc.DEBUG_LEAK)``. Notice that this includes
19``gc.DEBUG_SAVEALL``, causing garbage-collected objects to be saved in
20gc.garbage for inspection.
21
22The :mod:`gc` module provides the following functions:
23
24
25.. function:: enable()
26
27 Enable automatic garbage collection.
28
29
30.. function:: disable()
31
32 Disable automatic garbage collection.
33
34
35.. function:: isenabled()
36
37 Returns true if automatic collection is enabled.
38
39
40.. function:: collect([generation])
41
42 With no arguments, run a full collection. The optional argument *generation*
43 may be an integer specifying which generation to collect (from 0 to 2). A
44 :exc:`ValueError` is raised if the generation number is invalid. The number of
45 unreachable objects found is returned.
46
Georg Brandl2ee470f2008-07-16 12:55:28 +000047 The free lists maintained for a number of builtin types are cleared
48 whenever a full collection or collection of the highest generation (2)
49 is run. Not all items in some free lists may be freed due to the
50 particular implementation, in particular :class:`float`.
51
Georg Brandl116aa622007-08-15 14:28:22 +000052
53.. function:: set_debug(flags)
54
55 Set the garbage collection debugging flags. Debugging information will be
56 written to ``sys.stderr``. See below for a list of debugging flags which can be
57 combined using bit operations to control debugging.
58
59
60.. function:: get_debug()
61
62 Return the debugging flags currently set.
63
64
65.. function:: get_objects()
66
67 Returns a list of all objects tracked by the collector, excluding the list
68 returned.
69
Georg Brandl116aa622007-08-15 14:28:22 +000070
71.. function:: set_threshold(threshold0[, threshold1[, threshold2]])
72
73 Set the garbage collection thresholds (the collection frequency). Setting
74 *threshold0* to zero disables collection.
75
76 The GC classifies objects into three generations depending on how many
77 collection sweeps they have survived. New objects are placed in the youngest
78 generation (generation ``0``). If an object survives a collection it is moved
79 into the next older generation. Since generation ``2`` is the oldest
80 generation, objects in that generation remain there after a collection. In
81 order to decide when to run, the collector keeps track of the number object
82 allocations and deallocations since the last collection. When the number of
83 allocations minus the number of deallocations exceeds *threshold0*, collection
84 starts. Initially only generation ``0`` is examined. If generation ``0`` has
85 been examined more than *threshold1* times since generation ``1`` has been
86 examined, then generation ``1`` is examined as well. Similarly, *threshold2*
87 controls the number of collections of generation ``1`` before collecting
88 generation ``2``.
89
90
91.. function:: get_count()
92
93 Return the current collection counts as a tuple of ``(count0, count1,
94 count2)``.
95
Georg Brandl116aa622007-08-15 14:28:22 +000096
97.. function:: get_threshold()
98
99 Return the current collection thresholds as a tuple of ``(threshold0,
100 threshold1, threshold2)``.
101
102
103.. function:: get_referrers(*objs)
104
105 Return the list of objects that directly refer to any of objs. This function
106 will only locate those containers which support garbage collection; extension
107 types which do refer to other objects but do not support garbage collection will
108 not be found.
109
110 Note that objects which have already been dereferenced, but which live in cycles
111 and have not yet been collected by the garbage collector can be listed among the
112 resulting referrers. To get only currently live objects, call :func:`collect`
113 before calling :func:`get_referrers`.
114
115 Care must be taken when using objects returned by :func:`get_referrers` because
116 some of them could still be under construction and hence in a temporarily
117 invalid state. Avoid using :func:`get_referrers` for any purpose other than
118 debugging.
119
Georg Brandl116aa622007-08-15 14:28:22 +0000120
121.. function:: get_referents(*objs)
122
123 Return a list of objects directly referred to by any of the arguments. The
124 referents returned are those objects visited by the arguments' C-level
125 :attr:`tp_traverse` methods (if any), and may not be all objects actually
126 directly reachable. :attr:`tp_traverse` methods are supported only by objects
127 that support garbage collection, and are only required to visit objects that may
128 be involved in a cycle. So, for example, if an integer is directly reachable
129 from an argument, that integer object may or may not appear in the result list.
130
Georg Brandl116aa622007-08-15 14:28:22 +0000131
132The following variable is provided for read-only access (you can mutate its
133value but should not rebind it):
134
Georg Brandl116aa622007-08-15 14:28:22 +0000135.. data:: garbage
136
137 A list of objects which the collector found to be unreachable but could not be
138 freed (uncollectable objects). By default, this list contains only objects with
Amaury Forgeot d'Arcad8dcd52007-12-10 23:58:35 +0000139 :meth:`__del__` methods. Objects that have :meth:`__del__` methods and are
Georg Brandl116aa622007-08-15 14:28:22 +0000140 part of a reference cycle cause the entire reference cycle to be uncollectable,
141 including objects not necessarily in the cycle but reachable only from it.
142 Python doesn't collect such cycles automatically because, in general, it isn't
143 possible for Python to guess a safe order in which to run the :meth:`__del__`
144 methods. If you know a safe order, you can force the issue by examining the
145 *garbage* list, and explicitly breaking cycles due to your objects within the
146 list. Note that these objects are kept alive even so by virtue of being in the
147 *garbage* list, so they should be removed from *garbage* too. For example,
148 after breaking cycles, do ``del gc.garbage[:]`` to empty the list. It's
149 generally better to avoid the issue by not creating cycles containing objects
150 with :meth:`__del__` methods, and *garbage* can be examined in that case to
151 verify that no such cycles are being created.
152
153 If :const:`DEBUG_SAVEALL` is set, then all unreachable objects will be added to
154 this list rather than freed.
155
156The following constants are provided for use with :func:`set_debug`:
157
158
159.. data:: DEBUG_STATS
160
161 Print statistics during collection. This information can be useful when tuning
162 the collection frequency.
163
164
165.. data:: DEBUG_COLLECTABLE
166
167 Print information on collectable objects found.
168
169
170.. data:: DEBUG_UNCOLLECTABLE
171
172 Print information of uncollectable objects found (objects which are not
173 reachable but cannot be freed by the collector). These objects will be added to
174 the ``garbage`` list.
175
176
Georg Brandl116aa622007-08-15 14:28:22 +0000177.. data:: DEBUG_SAVEALL
178
179 When set, all unreachable objects found will be appended to *garbage* rather
180 than being freed. This can be useful for debugging a leaking program.
181
182
183.. data:: DEBUG_LEAK
184
185 The debugging flags necessary for the collector to print information about a
186 leaking program (equal to ``DEBUG_COLLECTABLE | DEBUG_UNCOLLECTABLE |
Amaury Forgeot d'Arcad8dcd52007-12-10 23:58:35 +0000187 DEBUG_SAVEALL``).
Georg Brandl116aa622007-08-15 14:28:22 +0000188
189.. rubric:: Footnotes