Server : Apache System : Linux server1.cgrithy.com 3.10.0-1160.95.1.el7.x86_64 #1 SMP Mon Jul 24 13:59:37 UTC 2023 x86_64 User : nobody ( 99) PHP Version : 8.1.23 Disable Function : NONE Directory : /usr/share/doc/python-docs-2.7.5/html/library/ |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>27.12. gc — Garbage Collector interface — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="27. Python Runtime Services" href="python.html" /> <link rel="next" title="27.13. inspect — Inspect live objects" href="inspect.html" /> <link rel="prev" title="27.11. __future__ — Future statement definitions" href="__future__.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="inspect.html" title="27.13. inspect — Inspect live objects" accesskey="N">next</a> |</li> <li class="right" > <a href="__future__.html" title="27.11. __future__ — Future statement definitions" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="python.html" accesskey="U">27. Python Runtime Services</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-gc"> <span id="gc-garbage-collector-interface"></span><h1>27.12. <a class="reference internal" href="#module-gc" title="gc: Interface to the cycle-detecting garbage collector."><tt class="xref py py-mod docutils literal"><span class="pre">gc</span></tt></a> — Garbage Collector interface<a class="headerlink" href="#module-gc" title="Permalink to this headline">¶</a></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 <tt class="docutils literal"><span class="pre">gc.disable()</span></tt>. To debug a leaking program call <tt class="docutils literal"><span class="pre">gc.set_debug(gc.DEBUG_LEAK)</span></tt>. Notice that this includes <tt class="docutils literal"><span class="pre">gc.DEBUG_SAVEALL</span></tt>, 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."><tt class="xref py py-mod docutils literal"><span class="pre">gc</span></tt></a> module provides the following functions:</p> <dl class="function"> <dt id="gc.enable"> <tt class="descclassname">gc.</tt><tt class="descname">enable</tt><big>(</big><big>)</big><a class="headerlink" href="#gc.enable" title="Permalink to this definition">¶</a></dt> <dd><p>Enable automatic garbage collection.</p> </dd></dl> <dl class="function"> <dt id="gc.disable"> <tt class="descclassname">gc.</tt><tt class="descname">disable</tt><big>(</big><big>)</big><a class="headerlink" href="#gc.disable" title="Permalink to this definition">¶</a></dt> <dd><p>Disable automatic garbage collection.</p> </dd></dl> <dl class="function"> <dt id="gc.isenabled"> <tt class="descclassname">gc.</tt><tt class="descname">isenabled</tt><big>(</big><big>)</big><a class="headerlink" href="#gc.isenabled" title="Permalink to this definition">¶</a></dt> <dd><p>Returns true if automatic collection is enabled.</p> </dd></dl> <dl class="function"> <dt id="gc.collect"> <tt class="descclassname">gc.</tt><tt class="descname">collect</tt><big>(</big><span class="optional">[</span><em>generation</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#gc.collect" title="Permalink to this definition">¶</a></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.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> is raised if the generation number is invalid. The number of unreachable objects found is returned.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.5: </span>The optional <em>generation</em> argument was added.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.6: </span>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.html#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a> and <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a>.</p> </dd></dl> <dl class="function"> <dt id="gc.set_debug"> <tt class="descclassname">gc.</tt><tt class="descname">set_debug</tt><big>(</big><em>flags</em><big>)</big><a class="headerlink" href="#gc.set_debug" title="Permalink to this definition">¶</a></dt> <dd><p>Set the garbage collection debugging flags. Debugging information will be written to <tt class="docutils literal"><span class="pre">sys.stderr</span></tt>. See below for a list of debugging flags which can be combined using bit operations to control debugging.</p> </dd></dl> <dl class="function"> <dt id="gc.get_debug"> <tt class="descclassname">gc.</tt><tt class="descname">get_debug</tt><big>(</big><big>)</big><a class="headerlink" href="#gc.get_debug" title="Permalink to this definition">¶</a></dt> <dd><p>Return the debugging flags currently set.</p> </dd></dl> <dl class="function"> <dt id="gc.get_objects"> <tt class="descclassname">gc.</tt><tt class="descname">get_objects</tt><big>(</big><big>)</big><a class="headerlink" href="#gc.get_objects" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a list of all objects tracked by the collector, excluding the list returned.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.2.</span></p> </dd></dl> <dl class="function"> <dt id="gc.set_threshold"> <tt class="descclassname">gc.</tt><tt class="descname">set_threshold</tt><big>(</big><em>threshold0</em><span class="optional">[</span>, <em>threshold1</em><span class="optional">[</span>, <em>threshold2</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#gc.set_threshold" title="Permalink to this definition">¶</a></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 <tt class="docutils literal"><span class="pre">0</span></tt>). If an object survives a collection it is moved into the next older generation. Since generation <tt class="docutils literal"><span class="pre">2</span></tt> 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 <tt class="docutils literal"><span class="pre">0</span></tt> is examined. If generation <tt class="docutils literal"><span class="pre">0</span></tt> has been examined more than <em>threshold1</em> times since generation <tt class="docutils literal"><span class="pre">1</span></tt> has been examined, then generation <tt class="docutils literal"><span class="pre">1</span></tt> is examined as well. Similarly, <em>threshold2</em> controls the number of collections of generation <tt class="docutils literal"><span class="pre">1</span></tt> before collecting generation <tt class="docutils literal"><span class="pre">2</span></tt>.</p> </dd></dl> <dl class="function"> <dt id="gc.get_count"> <tt class="descclassname">gc.</tt><tt class="descname">get_count</tt><big>(</big><big>)</big><a class="headerlink" href="#gc.get_count" title="Permalink to this definition">¶</a></dt> <dd><p>Return the current collection counts as a tuple of <tt class="docutils literal"><span class="pre">(count0,</span> <span class="pre">count1,</span> <span class="pre">count2)</span></tt>.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.5.</span></p> </dd></dl> <dl class="function"> <dt id="gc.get_threshold"> <tt class="descclassname">gc.</tt><tt class="descname">get_threshold</tt><big>(</big><big>)</big><a class="headerlink" href="#gc.get_threshold" title="Permalink to this definition">¶</a></dt> <dd><p>Return the current collection thresholds as a tuple of <tt class="docutils literal"><span class="pre">(threshold0,</span> <span class="pre">threshold1,</span> <span class="pre">threshold2)</span></tt>.</p> </dd></dl> <dl class="function"> <dt id="gc.get_referrers"> <tt class="descclassname">gc.</tt><tt class="descname">get_referrers</tt><big>(</big><em>*objs</em><big>)</big><a class="headerlink" href="#gc.get_referrers" title="Permalink to this definition">¶</a></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"><tt class="xref py py-func docutils literal"><span class="pre">collect()</span></tt></a> before calling <a class="reference internal" href="#gc.get_referrers" title="gc.get_referrers"><tt class="xref py py-func docutils literal"><span class="pre">get_referrers()</span></tt></a>.</p> <p>Care must be taken when using objects returned by <a class="reference internal" href="#gc.get_referrers" title="gc.get_referrers"><tt class="xref py py-func docutils literal"><span class="pre">get_referrers()</span></tt></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"><tt class="xref py py-func docutils literal"><span class="pre">get_referrers()</span></tt></a> for any purpose other than debugging.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.2.</span></p> </dd></dl> <dl class="function"> <dt id="gc.get_referents"> <tt class="descclassname">gc.</tt><tt class="descname">get_referents</tt><big>(</big><em>*objs</em><big>)</big><a class="headerlink" href="#gc.get_referents" title="Permalink to this definition">¶</a></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 <tt class="xref py py-attr docutils literal"><span class="pre">tp_traverse</span></tt> methods (if any), and may not be all objects actually directly reachable. <tt class="xref py py-attr docutils literal"><span class="pre">tp_traverse</span></tt> 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="versionadded"> <span class="versionmodified">New in version 2.3.</span></p> </dd></dl> <dl class="function"> <dt id="gc.is_tracked"> <tt class="descclassname">gc.</tt><tt class="descname">is_tracked</tt><big>(</big><em>obj</em><big>)</big><a class="headerlink" href="#gc.is_tracked" title="Permalink to this definition">¶</a></dt> <dd><p>Returns True if the object is currently tracked by the garbage collector, False 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> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">gc</span><span class="o">.</span><span class="n">is_tracked</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="go">False</span> <span class="gp">>>> </span><span class="n">gc</span><span class="o">.</span><span class="n">is_tracked</span><span class="p">(</span><span class="s">"a"</span><span class="p">)</span> <span class="go">False</span> <span class="gp">>>> </span><span class="n">gc</span><span class="o">.</span><span class="n">is_tracked</span><span class="p">([])</span> <span class="go">True</span> <span class="gp">>>> </span><span class="n">gc</span><span class="o">.</span><span class="n">is_tracked</span><span class="p">({})</span> <span class="go">False</span> <span class="gp">>>> </span><span class="n">gc</span><span class="o">.</span><span class="n">is_tracked</span><span class="p">({</span><span class="s">"a"</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span> <span class="go">False</span> <span class="gp">>>> </span><span class="n">gc</span><span class="o">.</span><span class="n">is_tracked</span><span class="p">({</span><span class="s">"a"</span><span class="p">:</span> <span class="p">[]})</span> <span class="go">True</span> </pre></div> </div> <p class="versionadded"> <span class="versionmodified">New in version 2.7.</span></p> </dd></dl> <p>The following variable is provided for read-only access (you can mutate its value but should not rebind it):</p> <dl class="data"> <dt id="gc.garbage"> <tt class="descclassname">gc.</tt><tt class="descname">garbage</tt><a class="headerlink" href="#gc.garbage" title="Permalink to this definition">¶</a></dt> <dd><p>A list of objects which the collector found to be unreachable but could not be freed (uncollectable objects). By default, this list contains only objects with <a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><tt class="xref py py-meth docutils literal"><span class="pre">__del__()</span></tt></a> methods. <a class="footnote-reference" href="#id2" id="id1">[1]</a> Objects that have <a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><tt class="xref py py-meth docutils literal"><span class="pre">__del__()</span></tt></a> methods and are part of a reference cycle cause the entire reference cycle to be uncollectable, including objects not necessarily in the cycle but reachable only from it. Python doesn’t collect such cycles automatically because, in general, it isn’t possible for Python to guess a safe order in which to run the <a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><tt class="xref py py-meth docutils literal"><span class="pre">__del__()</span></tt></a> methods. If you know a safe order, you can force the issue by examining the <em>garbage</em> list, and explicitly breaking cycles due to your objects within the list. Note that these objects are kept alive even so by virtue of being in the <em>garbage</em> list, so they should be removed from <em>garbage</em> too. For example, after breaking cycles, do <tt class="docutils literal"><span class="pre">del</span> <span class="pre">gc.garbage[:]</span></tt> to empty the list. It’s generally better to avoid the issue by not creating cycles containing objects with <a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><tt class="xref py py-meth docutils literal"><span class="pre">__del__()</span></tt></a> methods, and <em>garbage</em> can be examined in that case to verify that no such cycles are being created.</p> <p>If <a class="reference internal" href="#gc.DEBUG_SAVEALL" title="gc.DEBUG_SAVEALL"><tt class="xref py py-const docutils literal"><span class="pre">DEBUG_SAVEALL</span></tt></a> is set, then all unreachable objects will be added to this list rather than freed.</p> </dd></dl> <p>The following constants are provided for use with <a class="reference internal" href="#gc.set_debug" title="gc.set_debug"><tt class="xref py py-func docutils literal"><span class="pre">set_debug()</span></tt></a>:</p> <dl class="data"> <dt id="gc.DEBUG_STATS"> <tt class="descclassname">gc.</tt><tt class="descname">DEBUG_STATS</tt><a class="headerlink" href="#gc.DEBUG_STATS" title="Permalink to this definition">¶</a></dt> <dd><p>Print statistics during collection. This information can be useful when tuning the collection frequency.</p> </dd></dl> <dl class="data"> <dt id="gc.DEBUG_COLLECTABLE"> <tt class="descclassname">gc.</tt><tt class="descname">DEBUG_COLLECTABLE</tt><a class="headerlink" href="#gc.DEBUG_COLLECTABLE" title="Permalink to this definition">¶</a></dt> <dd><p>Print information on collectable objects found.</p> </dd></dl> <dl class="data"> <dt id="gc.DEBUG_UNCOLLECTABLE"> <tt class="descclassname">gc.</tt><tt class="descname">DEBUG_UNCOLLECTABLE</tt><a class="headerlink" href="#gc.DEBUG_UNCOLLECTABLE" title="Permalink to this definition">¶</a></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 <tt class="docutils literal"><span class="pre">garbage</span></tt> list.</p> </dd></dl> <dl class="data"> <dt id="gc.DEBUG_INSTANCES"> <tt class="descclassname">gc.</tt><tt class="descname">DEBUG_INSTANCES</tt><a class="headerlink" href="#gc.DEBUG_INSTANCES" title="Permalink to this definition">¶</a></dt> <dd><p>When <a class="reference internal" href="#gc.DEBUG_COLLECTABLE" title="gc.DEBUG_COLLECTABLE"><tt class="xref py py-const docutils literal"><span class="pre">DEBUG_COLLECTABLE</span></tt></a> or <a class="reference internal" href="#gc.DEBUG_UNCOLLECTABLE" title="gc.DEBUG_UNCOLLECTABLE"><tt class="xref py py-const docutils literal"><span class="pre">DEBUG_UNCOLLECTABLE</span></tt></a> is set, print information about instance objects found.</p> </dd></dl> <dl class="data"> <dt id="gc.DEBUG_OBJECTS"> <tt class="descclassname">gc.</tt><tt class="descname">DEBUG_OBJECTS</tt><a class="headerlink" href="#gc.DEBUG_OBJECTS" title="Permalink to this definition">¶</a></dt> <dd><p>When <a class="reference internal" href="#gc.DEBUG_COLLECTABLE" title="gc.DEBUG_COLLECTABLE"><tt class="xref py py-const docutils literal"><span class="pre">DEBUG_COLLECTABLE</span></tt></a> or <a class="reference internal" href="#gc.DEBUG_UNCOLLECTABLE" title="gc.DEBUG_UNCOLLECTABLE"><tt class="xref py py-const docutils literal"><span class="pre">DEBUG_UNCOLLECTABLE</span></tt></a> is set, print information about objects other than instance objects found.</p> </dd></dl> <dl class="data"> <dt id="gc.DEBUG_SAVEALL"> <tt class="descclassname">gc.</tt><tt class="descname">DEBUG_SAVEALL</tt><a class="headerlink" href="#gc.DEBUG_SAVEALL" title="Permalink to this definition">¶</a></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="data"> <dt id="gc.DEBUG_LEAK"> <tt class="descclassname">gc.</tt><tt class="descname">DEBUG_LEAK</tt><a class="headerlink" href="#gc.DEBUG_LEAK" title="Permalink to this definition">¶</a></dt> <dd><p>The debugging flags necessary for the collector to print information about a leaking program (equal to <tt class="docutils literal"><span class="pre">DEBUG_COLLECTABLE</span> <span class="pre">|</span> <span class="pre">DEBUG_UNCOLLECTABLE</span> <span class="pre">|</span> <span class="pre">DEBUG_INSTANCES</span> <span class="pre">|</span> <span class="pre">DEBUG_OBJECTS</span> <span class="pre">|</span> <span class="pre">DEBUG_SAVEALL</span></tt>).</p> </dd></dl> <p class="rubric">Footnotes</p> <table class="docutils footnote" frame="void" id="id2" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>Prior to Python 2.2, the list contained all instance objects in unreachable cycles, not only those with <a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><tt class="xref py py-meth docutils literal"><span class="pre">__del__()</span></tt></a> methods.</td></tr> </tbody> </table> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="__future__.html" title="previous chapter">27.11. <tt class="docutils literal"><span class="pre">__future__</span></tt> — Future statement definitions</a></p> <h4>Next topic</h4> <p class="topless"><a href="inspect.html" title="next chapter">27.13. <tt class="docutils literal"><span class="pre">inspect</span></tt> — Inspect live objects</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/gc.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="inspect.html" title="27.13. inspect — Inspect live objects" >next</a> |</li> <li class="right" > <a href="__future__.html" title="27.11. __future__ — Future statement definitions" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="python.html" >27. Python Runtime Services</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2020, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Oct 13, 2020. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>