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