gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

656 lines
44KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="gc — Garbage Collector interface" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/gc.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="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 acc..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="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 acc..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>gc — Garbage Collector interface &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="inspect — Inspect live objects" href="inspect.html" />
  33. <link rel="prev" title="__future__ — Future statement definitions" href="__future__.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/gc.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h4>Previous topic</h4>
  84. <p class="topless"><a href="__future__.html"
  85. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code> — Future statement definitions</a></p>
  86. </div>
  87. <div>
  88. <h4>Next topic</h4>
  89. <p class="topless"><a href="inspect.html"
  90. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code> — Inspect live objects</a></p>
  91. </div>
  92. <div role="note" aria-label="source link">
  93. <h3>This Page</h3>
  94. <ul class="this-page-menu">
  95. <li><a href="../bugs.html">Report a Bug</a></li>
  96. <li>
  97. <a href="https://github.com/python/cpython/blob/main/Doc/library/gc.rst"
  98. rel="nofollow">Show Source
  99. </a>
  100. </li>
  101. </ul>
  102. </div>
  103. </nav>
  104. </div>
  105. </div>
  106. <div class="related" role="navigation" aria-label="related navigation">
  107. <h3>Navigation</h3>
  108. <ul>
  109. <li class="right" style="margin-right: 10px">
  110. <a href="../genindex.html" title="General Index"
  111. accesskey="I">index</a></li>
  112. <li class="right" >
  113. <a href="../py-modindex.html" title="Python Module Index"
  114. >modules</a> |</li>
  115. <li class="right" >
  116. <a href="inspect.html" title="inspect — Inspect live objects"
  117. accesskey="N">next</a> |</li>
  118. <li class="right" >
  119. <a href="__future__.html" title="__future__ — Future statement definitions"
  120. accesskey="P">previous</a> |</li>
  121. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  122. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  123. <li class="switchers">
  124. <div class="language_switcher_placeholder"></div>
  125. <div class="version_switcher_placeholder"></div>
  126. </li>
  127. <li>
  128. </li>
  129. <li id="cpython-language-and-version">
  130. <a href="../index.html">3.12.3 Documentation</a> &#187;
  131. </li>
  132. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  133. <li class="nav-item nav-item-2"><a href="python.html" accesskey="U">Python Runtime Services</a> &#187;</li>
  134. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">gc</span></code> — Garbage Collector interface</a></li>
  135. <li class="right">
  136. <div class="inline-search" role="search">
  137. <form class="inline-search" action="../search.html" method="get">
  138. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  139. <input type="submit" value="Go" />
  140. </form>
  141. </div>
  142. |
  143. </li>
  144. <li class="right">
  145. <label class="theme-selector-label">
  146. Theme
  147. <select class="theme-selector" oninput="activateTheme(this.value)">
  148. <option value="auto" selected>Auto</option>
  149. <option value="light">Light</option>
  150. <option value="dark">Dark</option>
  151. </select>
  152. </label> |</li>
  153. </ul>
  154. </div>
  155. <div class="document">
  156. <div class="documentwrapper">
  157. <div class="bodywrapper">
  158. <div class="body" role="main">
  159. <section id="module-gc">
  160. <span id="gc-garbage-collector-interface"></span><h1><a class="reference internal" href="#module-gc" title="gc: Interface to the cycle-detecting garbage collector."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gc</span></code></a> — Garbage Collector interface<a class="headerlink" href="#module-gc" title="Link to this heading">¶</a></h1>
  161. <hr class="docutils" />
  162. <p>This module provides an interface to the optional garbage collector. It
  163. provides the ability to disable the collector, tune the collection frequency,
  164. and set debugging options. It also provides access to unreachable objects that
  165. the collector found but cannot free. Since the collector supplements the
  166. reference counting already used in Python, you can disable the collector if you
  167. are sure your program does not create reference cycles. Automatic collection
  168. can be disabled by calling <code class="docutils literal notranslate"><span class="pre">gc.disable()</span></code>. To debug a leaking program call
  169. <code class="docutils literal notranslate"><span class="pre">gc.set_debug(gc.DEBUG_LEAK)</span></code>. Notice that this includes
  170. <code class="docutils literal notranslate"><span class="pre">gc.DEBUG_SAVEALL</span></code>, causing garbage-collected objects to be saved in
  171. gc.garbage for inspection.</p>
  172. <p>The <a class="reference internal" href="#module-gc" title="gc: Interface to the cycle-detecting garbage collector."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gc</span></code></a> module provides the following functions:</p>
  173. <dl class="py function">
  174. <dt class="sig sig-object py" id="gc.enable">
  175. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">enable</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#gc.enable" title="Link to this definition">¶</a></dt>
  176. <dd><p>Enable automatic garbage collection.</p>
  177. </dd></dl>
  178. <dl class="py function">
  179. <dt class="sig sig-object py" id="gc.disable">
  180. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">disable</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#gc.disable" title="Link to this definition">¶</a></dt>
  181. <dd><p>Disable automatic garbage collection.</p>
  182. </dd></dl>
  183. <dl class="py function">
  184. <dt class="sig sig-object py" id="gc.isenabled">
  185. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">isenabled</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#gc.isenabled" title="Link to this definition">¶</a></dt>
  186. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if automatic collection is enabled.</p>
  187. </dd></dl>
  188. <dl class="py function">
  189. <dt class="sig sig-object py" id="gc.collect">
  190. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">collect</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">generation</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">2</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gc.collect" title="Link to this definition">¶</a></dt>
  191. <dd><p>With no arguments, run a full collection. The optional argument <em>generation</em>
  192. may be an integer specifying which generation to collect (from 0 to 2). A
  193. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised if the generation number is invalid. The number of
  194. unreachable objects found is returned.</p>
  195. <p>The free lists maintained for a number of built-in types are cleared
  196. whenever a full collection or collection of the highest generation (2)
  197. is run. Not all items in some free lists may be freed due to the
  198. particular implementation, in particular <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a>.</p>
  199. <p>The effect of calling <code class="docutils literal notranslate"><span class="pre">gc.collect()</span></code> while the interpreter is already
  200. performing a collection is undefined.</p>
  201. </dd></dl>
  202. <dl class="py function">
  203. <dt class="sig sig-object py" id="gc.set_debug">
  204. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">set_debug</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">flags</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gc.set_debug" title="Link to this definition">¶</a></dt>
  205. <dd><p>Set the garbage collection debugging flags. Debugging information will be
  206. written to <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code>. See below for a list of debugging flags which can be
  207. combined using bit operations to control debugging.</p>
  208. </dd></dl>
  209. <dl class="py function">
  210. <dt class="sig sig-object py" id="gc.get_debug">
  211. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">get_debug</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#gc.get_debug" title="Link to this definition">¶</a></dt>
  212. <dd><p>Return the debugging flags currently set.</p>
  213. </dd></dl>
  214. <dl class="py function">
  215. <dt class="sig sig-object py" id="gc.get_objects">
  216. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">get_objects</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">generation</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gc.get_objects" title="Link to this definition">¶</a></dt>
  217. <dd><p>Returns a list of all objects tracked by the collector, excluding the list
  218. returned. If <em>generation</em> is not None, return only the objects tracked by
  219. the collector that are in that generation.</p>
  220. <div class="versionchanged">
  221. <p><span class="versionmodified changed">Changed in version 3.8: </span>New <em>generation</em> parameter.</p>
  222. </div>
  223. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">gc.get_objects</span></code> with argument <code class="docutils literal notranslate"><span class="pre">generation</span></code>.</p>
  224. </dd></dl>
  225. <dl class="py function">
  226. <dt class="sig sig-object py" id="gc.get_stats">
  227. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">get_stats</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#gc.get_stats" title="Link to this definition">¶</a></dt>
  228. <dd><p>Return a list of three per-generation dictionaries containing collection
  229. statistics since interpreter start. The number of keys may change
  230. in the future, but currently each dictionary will contain the following
  231. items:</p>
  232. <ul class="simple">
  233. <li><p><code class="docutils literal notranslate"><span class="pre">collections</span></code> is the number of times this generation was collected;</p></li>
  234. <li><p><code class="docutils literal notranslate"><span class="pre">collected</span></code> is the total number of objects collected inside this
  235. generation;</p></li>
  236. <li><p><code class="docutils literal notranslate"><span class="pre">uncollectable</span></code> is the total number of objects which were found
  237. to be uncollectable (and were therefore moved to the <a class="reference internal" href="#gc.garbage" title="gc.garbage"><code class="xref py py-data docutils literal notranslate"><span class="pre">garbage</span></code></a>
  238. list) inside this generation.</p></li>
  239. </ul>
  240. <div class="versionadded">
  241. <p><span class="versionmodified added">New in version 3.4.</span></p>
  242. </div>
  243. </dd></dl>
  244. <dl class="py function">
  245. <dt class="sig sig-object py" id="gc.set_threshold">
  246. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">set_threshold</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">threshold0</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">threshold1</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">threshold2</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#gc.set_threshold" title="Link to this definition">¶</a></dt>
  247. <dd><p>Set the garbage collection thresholds (the collection frequency). Setting
  248. <em>threshold0</em> to zero disables collection.</p>
  249. <p>The GC classifies objects into three generations depending on how many
  250. collection sweeps they have survived. New objects are placed in the youngest
  251. generation (generation <code class="docutils literal notranslate"><span class="pre">0</span></code>). If an object survives a collection it is moved
  252. into the next older generation. Since generation <code class="docutils literal notranslate"><span class="pre">2</span></code> is the oldest
  253. generation, objects in that generation remain there after a collection. In
  254. order to decide when to run, the collector keeps track of the number object
  255. allocations and deallocations since the last collection. When the number of
  256. allocations minus the number of deallocations exceeds <em>threshold0</em>, collection
  257. starts. Initially only generation <code class="docutils literal notranslate"><span class="pre">0</span></code> is examined. If generation <code class="docutils literal notranslate"><span class="pre">0</span></code> has
  258. been examined more than <em>threshold1</em> times since generation <code class="docutils literal notranslate"><span class="pre">1</span></code> has been
  259. examined, then generation <code class="docutils literal notranslate"><span class="pre">1</span></code> is examined as well.
  260. With the third generation, things are a bit more complicated,
  261. 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>
  262. </dd></dl>
  263. <dl class="py function">
  264. <dt class="sig sig-object py" id="gc.get_count">
  265. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">get_count</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#gc.get_count" title="Link to this definition">¶</a></dt>
  266. <dd><p>Return the current collection counts as a tuple of <code class="docutils literal notranslate"><span class="pre">(count0,</span> <span class="pre">count1,</span>
  267. <span class="pre">count2)</span></code>.</p>
  268. </dd></dl>
  269. <dl class="py function">
  270. <dt class="sig sig-object py" id="gc.get_threshold">
  271. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">get_threshold</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#gc.get_threshold" title="Link to this definition">¶</a></dt>
  272. <dd><p>Return the current collection thresholds as a tuple of <code class="docutils literal notranslate"><span class="pre">(threshold0,</span>
  273. <span class="pre">threshold1,</span> <span class="pre">threshold2)</span></code>.</p>
  274. </dd></dl>
  275. <dl class="py function">
  276. <dt class="sig sig-object py" id="gc.get_referrers">
  277. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">get_referrers</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">objs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gc.get_referrers" title="Link to this definition">¶</a></dt>
  278. <dd><p>Return the list of objects that directly refer to any of objs. This function
  279. will only locate those containers which support garbage collection; extension
  280. types which do refer to other objects but do not support garbage collection will
  281. not be found.</p>
  282. <p>Note that objects which have already been dereferenced, but which live in cycles
  283. and have not yet been collected by the garbage collector can be listed among the
  284. resulting referrers. To get only currently live objects, call <a class="reference internal" href="#gc.collect" title="gc.collect"><code class="xref py py-func docutils literal notranslate"><span class="pre">collect()</span></code></a>
  285. before calling <a class="reference internal" href="#gc.get_referrers" title="gc.get_referrers"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_referrers()</span></code></a>.</p>
  286. <div class="admonition warning">
  287. <p class="admonition-title">Warning</p>
  288. <p>Care must be taken when using objects returned by <a class="reference internal" href="#gc.get_referrers" title="gc.get_referrers"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_referrers()</span></code></a> because
  289. some of them could still be under construction and hence in a temporarily
  290. invalid state. Avoid using <a class="reference internal" href="#gc.get_referrers" title="gc.get_referrers"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_referrers()</span></code></a> for any purpose other than
  291. debugging.</p>
  292. </div>
  293. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">gc.get_referrers</span></code> with argument <code class="docutils literal notranslate"><span class="pre">objs</span></code>.</p>
  294. </dd></dl>
  295. <dl class="py function">
  296. <dt class="sig sig-object py" id="gc.get_referents">
  297. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">get_referents</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">objs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gc.get_referents" title="Link to this definition">¶</a></dt>
  298. <dd><p>Return a list of objects directly referred to by any of the arguments. The
  299. referents returned are those objects visited by the arguments’ C-level
  300. <a class="reference internal" href="../c-api/typeobj.html#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_traverse</span></code></a> methods (if any), and may not be all objects actually
  301. directly reachable. <a class="reference internal" href="../c-api/typeobj.html#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_traverse</span></code></a> methods are supported only by objects
  302. that support garbage collection, and are only required to visit objects that may
  303. be involved in a cycle. So, for example, if an integer is directly reachable
  304. from an argument, that integer object may or may not appear in the result list.</p>
  305. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">gc.get_referents</span></code> with argument <code class="docutils literal notranslate"><span class="pre">objs</span></code>.</p>
  306. </dd></dl>
  307. <dl class="py function">
  308. <dt class="sig sig-object py" id="gc.is_tracked">
  309. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">is_tracked</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gc.is_tracked" title="Link to this definition">¶</a></dt>
  310. <dd><p>Returns <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is currently tracked by the garbage collector,
  311. <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise. As a general rule, instances of atomic types aren’t
  312. tracked and instances of non-atomic types (containers, user-defined
  313. objects…) are. However, some type-specific optimizations can be present
  314. in order to suppress the garbage collector footprint of simple instances
  315. (e.g. dicts containing only atomic keys and values):</p>
  316. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </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>
  317. <span class="go">False</span>
  318. <span class="gp">&gt;&gt;&gt; </span><span class="n">gc</span><span class="o">.</span><span class="n">is_tracked</span><span class="p">(</span><span class="s2">&quot;a&quot;</span><span class="p">)</span>
  319. <span class="go">False</span>
  320. <span class="gp">&gt;&gt;&gt; </span><span class="n">gc</span><span class="o">.</span><span class="n">is_tracked</span><span class="p">([])</span>
  321. <span class="go">True</span>
  322. <span class="gp">&gt;&gt;&gt; </span><span class="n">gc</span><span class="o">.</span><span class="n">is_tracked</span><span class="p">({})</span>
  323. <span class="go">False</span>
  324. <span class="gp">&gt;&gt;&gt; </span><span class="n">gc</span><span class="o">.</span><span class="n">is_tracked</span><span class="p">({</span><span class="s2">&quot;a&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span>
  325. <span class="go">False</span>
  326. <span class="gp">&gt;&gt;&gt; </span><span class="n">gc</span><span class="o">.</span><span class="n">is_tracked</span><span class="p">({</span><span class="s2">&quot;a&quot;</span><span class="p">:</span> <span class="p">[]})</span>
  327. <span class="go">True</span>
  328. </pre></div>
  329. </div>
  330. <div class="versionadded">
  331. <p><span class="versionmodified added">New in version 3.1.</span></p>
  332. </div>
  333. </dd></dl>
  334. <dl class="py function">
  335. <dt class="sig sig-object py" id="gc.is_finalized">
  336. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">is_finalized</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#gc.is_finalized" title="Link to this definition">¶</a></dt>
  337. <dd><p>Returns <code class="docutils literal notranslate"><span class="pre">True</span></code> if the given object has been finalized by the
  338. garbage collector, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
  339. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="kc">None</span>
  340. <span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Lazarus</span><span class="p">:</span>
  341. <span class="gp">... </span> <span class="k">def</span> <span class="fm">__del__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  342. <span class="gp">... </span> <span class="k">global</span> <span class="n">x</span>
  343. <span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="bp">self</span>
  344. <span class="gp">...</span>
  345. <span class="gp">&gt;&gt;&gt; </span><span class="n">lazarus</span> <span class="o">=</span> <span class="n">Lazarus</span><span class="p">()</span>
  346. <span class="gp">&gt;&gt;&gt; </span><span class="n">gc</span><span class="o">.</span><span class="n">is_finalized</span><span class="p">(</span><span class="n">lazarus</span><span class="p">)</span>
  347. <span class="go">False</span>
  348. <span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">lazarus</span>
  349. <span class="gp">&gt;&gt;&gt; </span><span class="n">gc</span><span class="o">.</span><span class="n">is_finalized</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
  350. <span class="go">True</span>
  351. </pre></div>
  352. </div>
  353. <div class="versionadded">
  354. <p><span class="versionmodified added">New in version 3.9.</span></p>
  355. </div>
  356. </dd></dl>
  357. <dl class="py function">
  358. <dt class="sig sig-object py" id="gc.freeze">
  359. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">freeze</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#gc.freeze" title="Link to this definition">¶</a></dt>
  360. <dd><p>Freeze all the objects tracked by the garbage collector; move them to a
  361. permanent generation and ignore them in all the future collections.</p>
  362. <p>If a process will <code class="docutils literal notranslate"><span class="pre">fork()</span></code> without <code class="docutils literal notranslate"><span class="pre">exec()</span></code>, avoiding unnecessary
  363. copy-on-write in child processes will maximize memory sharing and reduce
  364. overall memory usage. This requires both avoiding creation of freed “holes”
  365. in memory pages in the parent process and ensuring that GC collections in
  366. child processes won’t touch the <code class="docutils literal notranslate"><span class="pre">gc_refs</span></code> counter of long-lived objects
  367. originating in the parent process. To accomplish both, call <code class="docutils literal notranslate"><span class="pre">gc.disable()</span></code>
  368. early in the parent process, <code class="docutils literal notranslate"><span class="pre">gc.freeze()</span></code> right before <code class="docutils literal notranslate"><span class="pre">fork()</span></code>, and
  369. <code class="docutils literal notranslate"><span class="pre">gc.enable()</span></code> early in child processes.</p>
  370. <div class="versionadded">
  371. <p><span class="versionmodified added">New in version 3.7.</span></p>
  372. </div>
  373. </dd></dl>
  374. <dl class="py function">
  375. <dt class="sig sig-object py" id="gc.unfreeze">
  376. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">unfreeze</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#gc.unfreeze" title="Link to this definition">¶</a></dt>
  377. <dd><p>Unfreeze the objects in the permanent generation, put them back into the
  378. oldest generation.</p>
  379. <div class="versionadded">
  380. <p><span class="versionmodified added">New in version 3.7.</span></p>
  381. </div>
  382. </dd></dl>
  383. <dl class="py function">
  384. <dt class="sig sig-object py" id="gc.get_freeze_count">
  385. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">get_freeze_count</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#gc.get_freeze_count" title="Link to this definition">¶</a></dt>
  386. <dd><p>Return the number of objects in the permanent generation.</p>
  387. <div class="versionadded">
  388. <p><span class="versionmodified added">New in version 3.7.</span></p>
  389. </div>
  390. </dd></dl>
  391. <p>The following variables are provided for read-only access (you can mutate the
  392. values but should not rebind them):</p>
  393. <dl class="py data">
  394. <dt class="sig sig-object py" id="gc.garbage">
  395. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">garbage</span></span><a class="headerlink" href="#gc.garbage" title="Link to this definition">¶</a></dt>
  396. <dd><p>A list of objects which the collector found to be unreachable but could
  397. not be freed (uncollectable objects). Starting with Python 3.4, this
  398. list should be empty most of the time, except when using instances of
  399. C extension types with a non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code> <code class="docutils literal notranslate"><span class="pre">tp_del</span></code> slot.</p>
  400. <p>If <a class="reference internal" href="#gc.DEBUG_SAVEALL" title="gc.DEBUG_SAVEALL"><code class="xref py py-const docutils literal notranslate"><span class="pre">DEBUG_SAVEALL</span></code></a> is set, then all unreachable objects will be
  401. added to this list rather than freed.</p>
  402. <div class="versionchanged">
  403. <p><span class="versionmodified changed">Changed in version 3.2: </span>If this list is non-empty at <a class="reference internal" href="../glossary.html#term-interpreter-shutdown"><span class="xref std std-term">interpreter shutdown</span></a>, a
  404. <a class="reference internal" href="exceptions.html#ResourceWarning" title="ResourceWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ResourceWarning</span></code></a> is emitted, which is silent by default. If
  405. <a class="reference internal" href="#gc.DEBUG_UNCOLLECTABLE" title="gc.DEBUG_UNCOLLECTABLE"><code class="xref py py-const docutils literal notranslate"><span class="pre">DEBUG_UNCOLLECTABLE</span></code></a> is set, in addition all uncollectable objects
  406. are printed.</p>
  407. </div>
  408. <div class="versionchanged">
  409. <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.html#object.__del__" title="object.__del__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__del__()</span></code></a> method don’t end
  410. up in <a class="reference internal" href="#gc.garbage" title="gc.garbage"><code class="xref py py-data docutils literal notranslate"><span class="pre">gc.garbage</span></code></a> anymore.</p>
  411. </div>
  412. </dd></dl>
  413. <dl class="py data">
  414. <dt class="sig sig-object py" id="gc.callbacks">
  415. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">callbacks</span></span><a class="headerlink" href="#gc.callbacks" title="Link to this definition">¶</a></dt>
  416. <dd><p>A list of callbacks that will be invoked by the garbage collector before and
  417. after collection. The callbacks will be called with two arguments,
  418. <em>phase</em> and <em>info</em>.</p>
  419. <p><em>phase</em> can be one of two values:</p>
  420. <blockquote>
  421. <div><p>“start”: The garbage collection is about to start.</p>
  422. <p>“stop”: The garbage collection has finished.</p>
  423. </div></blockquote>
  424. <p><em>info</em> is a dict providing more information for the callback. The following
  425. keys are currently defined:</p>
  426. <blockquote>
  427. <div><p>“generation”: The oldest generation being collected.</p>
  428. <p>“collected”: When <em>phase</em> is “stop”, the number of objects
  429. successfully collected.</p>
  430. <p>“uncollectable”: When <em>phase</em> is “stop”, the number of objects
  431. that could not be collected and were put in <a class="reference internal" href="#gc.garbage" title="gc.garbage"><code class="xref py py-data docutils literal notranslate"><span class="pre">garbage</span></code></a>.</p>
  432. </div></blockquote>
  433. <p>Applications can add their own callbacks to this list. The primary
  434. use cases are:</p>
  435. <blockquote>
  436. <div><p>Gathering statistics about garbage collection, such as how often
  437. various generations are collected, and how long the collection
  438. takes.</p>
  439. <p>Allowing applications to identify and clear their own uncollectable
  440. types when they appear in <a class="reference internal" href="#gc.garbage" title="gc.garbage"><code class="xref py py-data docutils literal notranslate"><span class="pre">garbage</span></code></a>.</p>
  441. </div></blockquote>
  442. <div class="versionadded">
  443. <p><span class="versionmodified added">New in version 3.3.</span></p>
  444. </div>
  445. </dd></dl>
  446. <p>The following constants are provided for use with <a class="reference internal" href="#gc.set_debug" title="gc.set_debug"><code class="xref py py-func docutils literal notranslate"><span class="pre">set_debug()</span></code></a>:</p>
  447. <dl class="py data">
  448. <dt class="sig sig-object py" id="gc.DEBUG_STATS">
  449. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">DEBUG_STATS</span></span><a class="headerlink" href="#gc.DEBUG_STATS" title="Link to this definition">¶</a></dt>
  450. <dd><p>Print statistics during collection. This information can be useful when tuning
  451. the collection frequency.</p>
  452. </dd></dl>
  453. <dl class="py data">
  454. <dt class="sig sig-object py" id="gc.DEBUG_COLLECTABLE">
  455. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">DEBUG_COLLECTABLE</span></span><a class="headerlink" href="#gc.DEBUG_COLLECTABLE" title="Link to this definition">¶</a></dt>
  456. <dd><p>Print information on collectable objects found.</p>
  457. </dd></dl>
  458. <dl class="py data">
  459. <dt class="sig sig-object py" id="gc.DEBUG_UNCOLLECTABLE">
  460. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">DEBUG_UNCOLLECTABLE</span></span><a class="headerlink" href="#gc.DEBUG_UNCOLLECTABLE" title="Link to this definition">¶</a></dt>
  461. <dd><p>Print information of uncollectable objects found (objects which are not
  462. reachable but cannot be freed by the collector). These objects will be added
  463. to the <code class="docutils literal notranslate"><span class="pre">garbage</span></code> list.</p>
  464. <div class="versionchanged">
  465. <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 class="xref py py-data docutils literal notranslate"><span class="pre">garbage</span></code></a> list at
  466. <a class="reference internal" href="../glossary.html#term-interpreter-shutdown"><span class="xref std std-term">interpreter shutdown</span></a>, if it isn’t empty.</p>
  467. </div>
  468. </dd></dl>
  469. <dl class="py data">
  470. <dt class="sig sig-object py" id="gc.DEBUG_SAVEALL">
  471. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">DEBUG_SAVEALL</span></span><a class="headerlink" href="#gc.DEBUG_SAVEALL" title="Link to this definition">¶</a></dt>
  472. <dd><p>When set, all unreachable objects found will be appended to <em>garbage</em> rather
  473. than being freed. This can be useful for debugging a leaking program.</p>
  474. </dd></dl>
  475. <dl class="py data">
  476. <dt class="sig sig-object py" id="gc.DEBUG_LEAK">
  477. <span class="sig-prename descclassname"><span class="pre">gc.</span></span><span class="sig-name descname"><span class="pre">DEBUG_LEAK</span></span><a class="headerlink" href="#gc.DEBUG_LEAK" title="Link to this definition">¶</a></dt>
  478. <dd><p>The debugging flags necessary for the collector to print information about a
  479. leaking program (equal to <code class="docutils literal notranslate"><span class="pre">DEBUG_COLLECTABLE</span> <span class="pre">|</span> <span class="pre">DEBUG_UNCOLLECTABLE</span> <span class="pre">|</span>
  480. <span class="pre">DEBUG_SAVEALL</span></code>).</p>
  481. </dd></dl>
  482. </section>
  483. <div class="clearer"></div>
  484. </div>
  485. </div>
  486. </div>
  487. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  488. <div class="sphinxsidebarwrapper">
  489. <div>
  490. <h4>Previous topic</h4>
  491. <p class="topless"><a href="__future__.html"
  492. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code> — Future statement definitions</a></p>
  493. </div>
  494. <div>
  495. <h4>Next topic</h4>
  496. <p class="topless"><a href="inspect.html"
  497. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code> — Inspect live objects</a></p>
  498. </div>
  499. <div role="note" aria-label="source link">
  500. <h3>This Page</h3>
  501. <ul class="this-page-menu">
  502. <li><a href="../bugs.html">Report a Bug</a></li>
  503. <li>
  504. <a href="https://github.com/python/cpython/blob/main/Doc/library/gc.rst"
  505. rel="nofollow">Show Source
  506. </a>
  507. </li>
  508. </ul>
  509. </div>
  510. </div>
  511. <div id="sidebarbutton" title="Collapse sidebar">
  512. <span>«</span>
  513. </div>
  514. </div>
  515. <div class="clearer"></div>
  516. </div>
  517. <div class="related" role="navigation" aria-label="related navigation">
  518. <h3>Navigation</h3>
  519. <ul>
  520. <li class="right" style="margin-right: 10px">
  521. <a href="../genindex.html" title="General Index"
  522. >index</a></li>
  523. <li class="right" >
  524. <a href="../py-modindex.html" title="Python Module Index"
  525. >modules</a> |</li>
  526. <li class="right" >
  527. <a href="inspect.html" title="inspect — Inspect live objects"
  528. >next</a> |</li>
  529. <li class="right" >
  530. <a href="__future__.html" title="__future__ — Future statement definitions"
  531. >previous</a> |</li>
  532. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  533. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  534. <li class="switchers">
  535. <div class="language_switcher_placeholder"></div>
  536. <div class="version_switcher_placeholder"></div>
  537. </li>
  538. <li>
  539. </li>
  540. <li id="cpython-language-and-version">
  541. <a href="../index.html">3.12.3 Documentation</a> &#187;
  542. </li>
  543. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  544. <li class="nav-item nav-item-2"><a href="python.html" >Python Runtime Services</a> &#187;</li>
  545. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">gc</span></code> — Garbage Collector interface</a></li>
  546. <li class="right">
  547. <div class="inline-search" role="search">
  548. <form class="inline-search" action="../search.html" method="get">
  549. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  550. <input type="submit" value="Go" />
  551. </form>
  552. </div>
  553. |
  554. </li>
  555. <li class="right">
  556. <label class="theme-selector-label">
  557. Theme
  558. <select class="theme-selector" oninput="activateTheme(this.value)">
  559. <option value="auto" selected>Auto</option>
  560. <option value="light">Light</option>
  561. <option value="dark">Dark</option>
  562. </select>
  563. </label> |</li>
  564. </ul>
  565. </div>
  566. <div class="footer">
  567. &copy;
  568. <a href="../copyright.html">
  569. Copyright
  570. </a>
  571. 2001-2024, Python Software Foundation.
  572. <br />
  573. This page is licensed under the Python Software Foundation License Version 2.
  574. <br />
  575. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  576. <br />
  577. See <a href="/license.html">History and License</a> for more information.<br />
  578. <br />
  579. The Python Software Foundation is a non-profit corporation.
  580. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  581. <br />
  582. <br />
  583. Last updated on Apr 09, 2024 (13:47 UTC).
  584. <a href="/bugs.html">Found a bug</a>?
  585. <br />
  586. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  587. </div>
  588. </body>
  589. </html>
上海开阖软件有限公司 沪ICP备12045867号-1