summaryrefslogtreecommitdiff
path: root/devdocs/python~3.12/library%2Fgc.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/python~3.12/library%2Fgc.html')
-rw-r--r--devdocs/python~3.12/library%2Fgc.html111
1 files changed, 111 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Fgc.html b/devdocs/python~3.12/library%2Fgc.html
new file mode 100644
index 00000000..17d4c135
--- /dev/null
+++ b/devdocs/python~3.12/library%2Fgc.html
@@ -0,0 +1,111 @@
+ <span id="gc-garbage-collector-interface"></span><h1>gc — Garbage Collector interface</h1> <p>This module provides an interface to the optional garbage collector. It provides the ability to disable the collector, tune the collection frequency, and set debugging options. It also provides access to unreachable objects that the collector found but cannot free. Since the collector supplements the reference counting already used in Python, you can disable the collector if you are sure your program does not create reference cycles. Automatic collection can be disabled by calling <code>gc.disable()</code>. To debug a leaking program call <code>gc.set_debug(gc.DEBUG_LEAK)</code>. Notice that this includes <code>gc.DEBUG_SAVEALL</code>, causing garbage-collected objects to be saved in gc.garbage for inspection.</p> <p>The <a class="reference internal" href="#module-gc" title="gc: Interface to the cycle-detecting garbage collector."><code>gc</code></a> module provides the following functions:</p> <dl class="py function"> <dt class="sig sig-object py" id="gc.enable">
+<code>gc.enable()</code> </dt> <dd>
+<p>Enable automatic garbage collection.</p> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="gc.disable">
+<code>gc.disable()</code> </dt> <dd>
+<p>Disable automatic garbage collection.</p> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="gc.isenabled">
+<code>gc.isenabled()</code> </dt> <dd>
+<p>Return <code>True</code> if automatic collection is enabled.</p> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="gc.collect">
+<code>gc.collect(generation=2)</code> </dt> <dd>
+<p>With no arguments, run a full collection. The optional argument <em>generation</em> may be an integer specifying which generation to collect (from 0 to 2). A <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> is raised if the generation number is invalid. The number of unreachable objects found is returned.</p> <p>The free lists maintained for a number of built-in types are cleared whenever a full collection or collection of the highest generation (2) is run. Not all items in some free lists may be freed due to the particular implementation, in particular <a class="reference internal" href="functions#float" title="float"><code>float</code></a>.</p> <p>The effect of calling <code>gc.collect()</code> while the interpreter is already performing a collection is undefined.</p> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="gc.set_debug">
+<code>gc.set_debug(flags)</code> </dt> <dd>
+<p>Set the garbage collection debugging flags. Debugging information will be written to <code>sys.stderr</code>. See below for a list of debugging flags which can be combined using bit operations to control debugging.</p> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="gc.get_debug">
+<code>gc.get_debug()</code> </dt> <dd>
+<p>Return the debugging flags currently set.</p> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="gc.get_objects">
+<code>gc.get_objects(generation=None)</code> </dt> <dd>
+<p>Returns a list of all objects tracked by the collector, excluding the list returned. If <em>generation</em> is not None, return only the objects tracked by the collector that are in that generation.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>New <em>generation</em> parameter.</p> </div> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>gc.get_objects</code> with argument <code>generation</code>.</p> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="gc.get_stats">
+<code>gc.get_stats()</code> </dt> <dd>
+<p>Return a list of three per-generation dictionaries containing collection statistics since interpreter start. The number of keys may change in the future, but currently each dictionary will contain the following items:</p> <ul class="simple"> <li>
+<code>collections</code> is the number of times this generation was collected;</li> <li>
+<code>collected</code> is the total number of objects collected inside this generation;</li> <li>
+<code>uncollectable</code> is the total number of objects which were found to be uncollectable (and were therefore moved to the <a class="reference internal" href="#gc.garbage" title="gc.garbage"><code>garbage</code></a> list) inside this generation.</li> </ul> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.4.</span></p> </div> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="gc.set_threshold">
+<code>gc.set_threshold(threshold0[, threshold1[, threshold2]])</code> </dt> <dd>
+<p>Set the garbage collection thresholds (the collection frequency). Setting <em>threshold0</em> to zero disables collection.</p> <p>The GC classifies objects into three generations depending on how many collection sweeps they have survived. New objects are placed in the youngest generation (generation <code>0</code>). If an object survives a collection it is moved into the next older generation. Since generation <code>2</code> is the oldest generation, objects in that generation remain there after a collection. In order to decide when to run, the collector keeps track of the number object allocations and deallocations since the last collection. When the number of allocations minus the number of deallocations exceeds <em>threshold0</em>, collection starts. Initially only generation <code>0</code> is examined. If generation <code>0</code> has been examined more than <em>threshold1</em> times since generation <code>1</code> has been examined, then generation <code>1</code> is examined as well. With the third generation, things are a bit more complicated, see <a class="reference external" href="https://devguide.python.org/garbage_collector/#collecting-the-oldest-generation">Collecting the oldest generation</a> for more information.</p> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="gc.get_count">
+<code>gc.get_count()</code> </dt> <dd>
+<p>Return the current collection counts as a tuple of <code>(count0, count1,
+count2)</code>.</p> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="gc.get_threshold">
+<code>gc.get_threshold()</code> </dt> <dd>
+<p>Return the current collection thresholds as a tuple of <code>(threshold0,
+threshold1, threshold2)</code>.</p> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="gc.get_referrers">
+<code>gc.get_referrers(*objs)</code> </dt> <dd>
+<p>Return the list of objects that directly refer to any of objs. This function will only locate those containers which support garbage collection; extension types which do refer to other objects but do not support garbage collection will not be found.</p> <p>Note that objects which have already been dereferenced, but which live in cycles and have not yet been collected by the garbage collector can be listed among the resulting referrers. To get only currently live objects, call <a class="reference internal" href="#gc.collect" title="gc.collect"><code>collect()</code></a> before calling <a class="reference internal" href="#gc.get_referrers" title="gc.get_referrers"><code>get_referrers()</code></a>.</p> <div class="admonition warning"> <p class="admonition-title">Warning</p> <p>Care must be taken when using objects returned by <a class="reference internal" href="#gc.get_referrers" title="gc.get_referrers"><code>get_referrers()</code></a> because some of them could still be under construction and hence in a temporarily invalid state. Avoid using <a class="reference internal" href="#gc.get_referrers" title="gc.get_referrers"><code>get_referrers()</code></a> for any purpose other than debugging.</p> </div> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>gc.get_referrers</code> with argument <code>objs</code>.</p> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="gc.get_referents">
+<code>gc.get_referents(*objs)</code> </dt> <dd>
+<p>Return a list of objects directly referred to by any of the arguments. The referents returned are those objects visited by the arguments’ C-level <a class="reference internal" href="../c-api/typeobj#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code>tp_traverse</code></a> methods (if any), and may not be all objects actually directly reachable. <a class="reference internal" href="../c-api/typeobj#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code>tp_traverse</code></a> methods are supported only by objects that support garbage collection, and are only required to visit objects that may be involved in a cycle. So, for example, if an integer is directly reachable from an argument, that integer object may or may not appear in the result list.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>gc.get_referents</code> with argument <code>objs</code>.</p> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="gc.is_tracked">
+<code>gc.is_tracked(obj)</code> </dt> <dd>
+<p>Returns <code>True</code> if the object is currently tracked by the garbage collector, <code>False</code> otherwise. As a general rule, instances of atomic types aren’t tracked and instances of non-atomic types (containers, user-defined objects…) are. However, some type-specific optimizations can be present in order to suppress the garbage collector footprint of simple instances (e.g. dicts containing only atomic keys and values):</p> <pre data-language="python">&gt;&gt;&gt; gc.is_tracked(0)
+False
+&gt;&gt;&gt; gc.is_tracked("a")
+False
+&gt;&gt;&gt; gc.is_tracked([])
+True
+&gt;&gt;&gt; gc.is_tracked({})
+False
+&gt;&gt;&gt; gc.is_tracked({"a": 1})
+False
+&gt;&gt;&gt; gc.is_tracked({"a": []})
+True
+</pre> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.1.</span></p> </div> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="gc.is_finalized">
+<code>gc.is_finalized(obj)</code> </dt> <dd>
+<p>Returns <code>True</code> if the given object has been finalized by the garbage collector, <code>False</code> otherwise.</p> <pre data-language="python">&gt;&gt;&gt; x = None
+&gt;&gt;&gt; class Lazarus:
+... def __del__(self):
+... global x
+... x = self
+...
+&gt;&gt;&gt; lazarus = Lazarus()
+&gt;&gt;&gt; gc.is_finalized(lazarus)
+False
+&gt;&gt;&gt; del lazarus
+&gt;&gt;&gt; gc.is_finalized(x)
+True
+</pre> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.9.</span></p> </div> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="gc.freeze">
+<code>gc.freeze()</code> </dt> <dd>
+<p>Freeze all the objects tracked by the garbage collector; move them to a permanent generation and ignore them in all the future collections.</p> <p>If a process will <code>fork()</code> without <code>exec()</code>, avoiding unnecessary copy-on-write in child processes will maximize memory sharing and reduce overall memory usage. This requires both avoiding creation of freed “holes” in memory pages in the parent process and ensuring that GC collections in child processes won’t touch the <code>gc_refs</code> counter of long-lived objects originating in the parent process. To accomplish both, call <code>gc.disable()</code> early in the parent process, <code>gc.freeze()</code> right before <code>fork()</code>, and <code>gc.enable()</code> early in child processes.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="gc.unfreeze">
+<code>gc.unfreeze()</code> </dt> <dd>
+<p>Unfreeze the objects in the permanent generation, put them back into the oldest generation.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="gc.get_freeze_count">
+<code>gc.get_freeze_count()</code> </dt> <dd>
+<p>Return the number of objects in the permanent generation.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
+</dl> <p>The following variables are provided for read-only access (you can mutate the values but should not rebind them):</p> <dl class="py data"> <dt class="sig sig-object py" id="gc.garbage">
+<code>gc.garbage</code> </dt> <dd>
+<p>A list of objects which the collector found to be unreachable but could not be freed (uncollectable objects). Starting with Python 3.4, this list should be empty most of the time, except when using instances of C extension types with a non-<code>NULL</code> <code>tp_del</code> slot.</p> <p>If <a class="reference internal" href="#gc.DEBUG_SAVEALL" title="gc.DEBUG_SAVEALL"><code>DEBUG_SAVEALL</code></a> is set, then all unreachable objects will be added to this list rather than freed.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>If this list is non-empty at <a class="reference internal" href="../glossary#term-interpreter-shutdown"><span class="xref std std-term">interpreter shutdown</span></a>, a <a class="reference internal" href="exceptions#ResourceWarning" title="ResourceWarning"><code>ResourceWarning</code></a> is emitted, which is silent by default. If <a class="reference internal" href="#gc.DEBUG_UNCOLLECTABLE" title="gc.DEBUG_UNCOLLECTABLE"><code>DEBUG_UNCOLLECTABLE</code></a> is set, in addition all uncollectable objects are printed.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>Following <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0442/"><strong>PEP 442</strong></a>, objects with a <a class="reference internal" href="../reference/datamodel#object.__del__" title="object.__del__"><code>__del__()</code></a> method don’t end up in <a class="reference internal" href="#gc.garbage" title="gc.garbage"><code>gc.garbage</code></a> anymore.</p> </div> </dd>
+</dl> <dl class="py data"> <dt class="sig sig-object py" id="gc.callbacks">
+<code>gc.callbacks</code> </dt> <dd>
+<p>A list of callbacks that will be invoked by the garbage collector before and after collection. The callbacks will be called with two arguments, <em>phase</em> and <em>info</em>.</p> <p><em>phase</em> can be one of two values:</p> <p>“start”: The garbage collection is about to start.</p> <p>“stop”: The garbage collection has finished.</p> <p><em>info</em> is a dict providing more information for the callback. The following keys are currently defined:</p> <p>“generation”: The oldest generation being collected.</p> <p>“collected”: When <em>phase</em> is “stop”, the number of objects successfully collected.</p> <p>“uncollectable”: When <em>phase</em> is “stop”, the number of objects that could not be collected and were put in <a class="reference internal" href="#gc.garbage" title="gc.garbage"><code>garbage</code></a>.</p> <p>Applications can add their own callbacks to this list. The primary use cases are:</p> <p>Gathering statistics about garbage collection, such as how often various generations are collected, and how long the collection takes.</p> <p>Allowing applications to identify and clear their own uncollectable types when they appear in <a class="reference internal" href="#gc.garbage" title="gc.garbage"><code>garbage</code></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
+</dl> <p>The following constants are provided for use with <a class="reference internal" href="#gc.set_debug" title="gc.set_debug"><code>set_debug()</code></a>:</p> <dl class="py data"> <dt class="sig sig-object py" id="gc.DEBUG_STATS">
+<code>gc.DEBUG_STATS</code> </dt> <dd>
+<p>Print statistics during collection. This information can be useful when tuning the collection frequency.</p> </dd>
+</dl> <dl class="py data"> <dt class="sig sig-object py" id="gc.DEBUG_COLLECTABLE">
+<code>gc.DEBUG_COLLECTABLE</code> </dt> <dd>
+<p>Print information on collectable objects found.</p> </dd>
+</dl> <dl class="py data"> <dt class="sig sig-object py" id="gc.DEBUG_UNCOLLECTABLE">
+<code>gc.DEBUG_UNCOLLECTABLE</code> </dt> <dd>
+<p>Print information of uncollectable objects found (objects which are not reachable but cannot be freed by the collector). These objects will be added to the <code>garbage</code> list.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>Also print the contents of the <a class="reference internal" href="#gc.garbage" title="gc.garbage"><code>garbage</code></a> list at <a class="reference internal" href="../glossary#term-interpreter-shutdown"><span class="xref std std-term">interpreter shutdown</span></a>, if it isn’t empty.</p> </div> </dd>
+</dl> <dl class="py data"> <dt class="sig sig-object py" id="gc.DEBUG_SAVEALL">
+<code>gc.DEBUG_SAVEALL</code> </dt> <dd>
+<p>When set, all unreachable objects found will be appended to <em>garbage</em> rather than being freed. This can be useful for debugging a leaking program.</p> </dd>
+</dl> <dl class="py data"> <dt class="sig sig-object py" id="gc.DEBUG_LEAK">
+<code>gc.DEBUG_LEAK</code> </dt> <dd>
+<p>The debugging flags necessary for the collector to print information about a leaking program (equal to <code>DEBUG_COLLECTABLE | DEBUG_UNCOLLECTABLE |
+DEBUG_SAVEALL</code>).</p> </dd>
+</dl> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; 2001&ndash;2023 Python Software Foundation<br>Licensed under the PSF License.<br>
+ <a href="https://docs.python.org/3.12/library/gc.html" class="_attribution-link">https://docs.python.org/3.12/library/gc.html</a>
+ </p>
+</div>