Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 1 | \section{\module{gc} --- |
| 2 | Garbage Collector interface} |
| 3 | |
| 4 | \declaremodule{extension}{gc} |
Fred Drake | e544191 | 2000-09-09 03:26:51 +0000 | [diff] [blame] | 5 | \modulesynopsis{Interface to the cycle-detecting garbage collector.} |
Neil Schemenauer | b2c2c9e | 2000-10-04 16:34:09 +0000 | [diff] [blame] | 6 | \moduleauthor{Neil Schemenauer}{nas@arctrix.com} |
| 7 | \sectionauthor{Neil Schemenauer}{nas@arctrix.com} |
Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 8 | |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 9 | This module provides an interface to the optional garbage collector. It |
| 10 | provides the ability to disable the collector, tune the collection |
Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 11 | frequency, and set debugging options. It also provides access to |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 12 | unreachable objects that the collector found but cannot free. Since the |
| 13 | collector supplements the reference counting already used in Python, you |
| 14 | can disable the collector if you are sure your program does not create |
| 15 | reference cycles. Automatic collection can be disabled by calling |
| 16 | \code{gc.disable()}. To debug a leaking program call |
Martin v. Löwis | 0f9679d | 2005-03-28 15:29:28 +0000 | [diff] [blame] | 17 | \code{gc.set_debug(gc.DEBUG_LEAK)}. Notice that this includes |
| 18 | \code{gc.DEBUG_SAVEALL}, causing garbage-collected objects to be |
| 19 | saved in gc.garbage for inspection. |
Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 20 | |
| 21 | The \module{gc} module provides the following functions: |
| 22 | |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 23 | \begin{funcdesc}{enable}{} |
| 24 | Enable automatic garbage collection. |
| 25 | \end{funcdesc} |
| 26 | |
| 27 | \begin{funcdesc}{disable}{} |
| 28 | Disable automatic garbage collection. |
| 29 | \end{funcdesc} |
| 30 | |
| 31 | \begin{funcdesc}{isenabled}{} |
| 32 | Returns true if automatic collection is enabled. |
| 33 | \end{funcdesc} |
| 34 | |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 35 | \begin{funcdesc}{collect}{\optional{generation}} |
| 36 | With no arguments, run a full collection. The optional argument |
| 37 | \var{generation} may be an integer specifying which generation to collect |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 38 | (from 0 to 2). A \exception{ValueError} is raised if the generation number |
| 39 | is invalid. |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 40 | The number of unreachable objects found is returned. |
| 41 | |
| 42 | \versionchanged[The optional \var{generation} argument was added]{2.5} |
Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 43 | \end{funcdesc} |
| 44 | |
| 45 | \begin{funcdesc}{set_debug}{flags} |
| 46 | Set the garbage collection debugging flags. |
| 47 | Debugging information will be written to \code{sys.stderr}. See below |
| 48 | for a list of debugging flags which can be combined using bit |
| 49 | operations to control debugging. |
| 50 | \end{funcdesc} |
| 51 | |
| 52 | \begin{funcdesc}{get_debug}{} |
| 53 | Return the debugging flags currently set. |
| 54 | \end{funcdesc} |
| 55 | |
Fred Drake | e4523c4 | 2002-07-10 19:21:07 +0000 | [diff] [blame] | 56 | \begin{funcdesc}{get_objects}{} |
| 57 | Returns a list of all objects tracked by the collector, excluding the |
| 58 | list returned. |
| 59 | \versionadded{2.2} |
| 60 | \end{funcdesc} |
| 61 | |
Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 62 | \begin{funcdesc}{set_threshold}{threshold0\optional{, |
| 63 | threshold1\optional{, threshold2}}} |
| 64 | Set the garbage collection thresholds (the collection frequency). |
| 65 | Setting \var{threshold0} to zero disables collection. |
| 66 | |
| 67 | The GC classifies objects into three generations depending on how many |
| 68 | collection sweeps they have survived. New objects are placed in the |
| 69 | youngest generation (generation \code{0}). If an object survives a |
| 70 | collection it is moved into the next older generation. Since |
| 71 | generation \code{2} is the oldest generation, objects in that |
| 72 | generation remain there after a collection. In order to decide when |
| 73 | to run, the collector keeps track of the number object allocations and |
| 74 | deallocations since the last collection. When the number of |
| 75 | allocations minus the number of deallocations exceeds |
| 76 | \var{threshold0}, collection starts. Initially only generation |
| 77 | \code{0} is examined. If generation \code{0} has been examined more |
| 78 | than \var{threshold1} times since generation \code{1} has been |
| 79 | examined, then generation \code{1} is examined as well. Similarly, |
| 80 | \var{threshold2} controls the number of collections of generation |
| 81 | \code{1} before collecting generation \code{2}. |
| 82 | \end{funcdesc} |
| 83 | |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 84 | \begin{funcdesc}{get_count}{} |
| 85 | Return the current collection counts as a tuple of |
| 86 | \code{(\var{count0}, \var{count1}, \var{count2})}. |
| 87 | \versionadded{2.5} |
| 88 | \end{funcdesc} |
| 89 | |
Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 90 | \begin{funcdesc}{get_threshold}{} |
| 91 | Return the current collection thresholds as a tuple of |
| 92 | \code{(\var{threshold0}, \var{threshold1}, \var{threshold2})}. |
| 93 | \end{funcdesc} |
| 94 | |
Martin v. Löwis | 560da62 | 2001-11-24 09:24:51 +0000 | [diff] [blame] | 95 | \begin{funcdesc}{get_referrers}{*objs} |
| 96 | Return the list of objects that directly refer to any of objs. This |
| 97 | function will only locate those containers which support garbage |
| 98 | collection; extension types which do refer to other objects but do not |
| 99 | support garbage collection will not be found. |
Martin v. Löwis | ef180dc | 2002-01-26 20:11:50 +0000 | [diff] [blame] | 100 | |
| 101 | Note that objects which have already been dereferenced, but which live |
| 102 | in cycles and have not yet been collected by the garbage collector can |
| 103 | be listed among the resulting referrers. To get only currently live |
| 104 | objects, call \function{collect()} before calling |
| 105 | \function{get_referrers()}. |
| 106 | |
Armin Rigo | 3be6d5d | 2003-10-28 12:10:38 +0000 | [diff] [blame] | 107 | Care must be taken when using objects returned by |
| 108 | \function{get_referrers()} because some of them could still be under |
| 109 | construction and hence in a temporarily invalid state. Avoid using |
| 110 | \function{get_referrers()} for any purpose other than debugging. |
| 111 | |
Martin v. Löwis | 560da62 | 2001-11-24 09:24:51 +0000 | [diff] [blame] | 112 | \versionadded{2.2} |
| 113 | \end{funcdesc} |
Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 114 | |
Tim Peters | 730f553 | 2003-04-08 17:17:17 +0000 | [diff] [blame] | 115 | \begin{funcdesc}{get_referents}{*objs} |
Tim Peters | 0f81ab6 | 2003-04-08 16:39:48 +0000 | [diff] [blame] | 116 | Return a list of objects directly referred to by any of the arguments. |
Tim Peters | 730f553 | 2003-04-08 17:17:17 +0000 | [diff] [blame] | 117 | The referents returned are those objects visited by the arguments' |
Fred Drake | de7ad2c | 2003-04-08 17:37:47 +0000 | [diff] [blame] | 118 | C-level \member{tp_traverse} methods (if any), and may not be all |
| 119 | objects actually directly reachable. \member{tp_traverse} methods |
Tim Peters | 0f81ab6 | 2003-04-08 16:39:48 +0000 | [diff] [blame] | 120 | are supported only by objects that support garbage collection, and are |
| 121 | only required to visit objects that may be involved in a cycle. So, |
| 122 | for example, if an integer is directly reachable from an argument, that |
| 123 | integer object may or may not appear in the result list. |
| 124 | |
| 125 | \versionadded{2.3} |
| 126 | \end{funcdesc} |
| 127 | |
Tim Peters | af0a883 | 2001-11-18 04:51:17 +0000 | [diff] [blame] | 128 | The following variable is provided for read-only access (you can |
| 129 | mutate its value but should not rebind it): |
Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 130 | |
| 131 | \begin{datadesc}{garbage} |
| 132 | A list of objects which the collector found to be unreachable |
Tim Peters | 169ded0 | 2001-11-03 19:57:21 +0000 | [diff] [blame] | 133 | but could not be freed (uncollectable objects). By default, this list |
| 134 | contains only objects with \method{__del__()} methods.\footnote{Prior to |
| 135 | Python 2.2, the list contained all instance objects in unreachable |
| 136 | cycles, not only those with \method{__del__()} methods.} |
| 137 | Objects that have |
Tim Peters | af0a883 | 2001-11-18 04:51:17 +0000 | [diff] [blame] | 138 | \method{__del__()} methods and are part of a reference cycle cause |
| 139 | the entire reference cycle to be uncollectable, including objects |
| 140 | not necessarily in the cycle but reachable only from it. Python doesn't |
| 141 | collect such cycles automatically because, in general, it isn't possible |
| 142 | for Python to guess a safe order in which to run the \method{__del__()} |
| 143 | methods. If you know a safe order, you can force the issue by examining |
| 144 | the \var{garbage} list, and explicitly breaking cycles due to your |
| 145 | objects within the list. Note that these objects are kept alive even |
| 146 | so by virtue of being in the \var{garbage} list, so they should be |
| 147 | removed from \var{garbage} too. For example, after breaking cycles, do |
| 148 | \code{del gc.garbage[:]} to empty the list. It's generally better |
| 149 | to avoid the issue by not creating cycles containing objects with |
| 150 | \method{__del__()} methods, and \var{garbage} can be examined in that |
Fred Drake | dda7dcb | 2001-12-14 21:19:08 +0000 | [diff] [blame] | 151 | case to verify that no such cycles are being created. |
Tim Peters | af0a883 | 2001-11-18 04:51:17 +0000 | [diff] [blame] | 152 | |
| 153 | If \constant{DEBUG_SAVEALL} is set, then all unreachable objects will |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 154 | be added to this list rather than freed. |
Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 155 | \end{datadesc} |
| 156 | |
| 157 | |
| 158 | The following constants are provided for use with |
| 159 | \function{set_debug()}: |
| 160 | |
| 161 | \begin{datadesc}{DEBUG_STATS} |
| 162 | Print statistics during collection. This information can |
| 163 | be useful when tuning the collection frequency. |
| 164 | \end{datadesc} |
| 165 | |
| 166 | \begin{datadesc}{DEBUG_COLLECTABLE} |
| 167 | Print information on collectable objects found. |
| 168 | \end{datadesc} |
| 169 | |
| 170 | \begin{datadesc}{DEBUG_UNCOLLECTABLE} |
| 171 | Print information of uncollectable objects found (objects which are |
| 172 | not reachable but cannot be freed by the collector). These objects |
| 173 | will be added to the \code{garbage} list. |
| 174 | \end{datadesc} |
| 175 | |
| 176 | \begin{datadesc}{DEBUG_INSTANCES} |
| 177 | When \constant{DEBUG_COLLECTABLE} or \constant{DEBUG_UNCOLLECTABLE} is |
| 178 | set, print information about instance objects found. |
| 179 | \end{datadesc} |
| 180 | |
| 181 | \begin{datadesc}{DEBUG_OBJECTS} |
| 182 | When \constant{DEBUG_COLLECTABLE} or \constant{DEBUG_UNCOLLECTABLE} is |
| 183 | set, print information about objects other than instance objects found. |
| 184 | \end{datadesc} |
| 185 | |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 186 | \begin{datadesc}{DEBUG_SAVEALL} |
| 187 | When set, all unreachable objects found will be appended to |
| 188 | \var{garbage} rather than being freed. This can be useful for debugging |
| 189 | a leaking program. |
| 190 | \end{datadesc} |
| 191 | |
Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 192 | \begin{datadesc}{DEBUG_LEAK} |
| 193 | The debugging flags necessary for the collector to print |
| 194 | information about a leaking program (equal to \code{DEBUG_COLLECTABLE | |
Tim Peters | af0a883 | 2001-11-18 04:51:17 +0000 | [diff] [blame] | 195 | DEBUG_UNCOLLECTABLE | DEBUG_INSTANCES | DEBUG_OBJECTS | DEBUG_SAVEALL}). |
Fred Drake | bdcf91f | 2000-07-12 04:22:53 +0000 | [diff] [blame] | 196 | \end{datadesc} |