222 lines
469 KiB
HTML
222 lines
469 KiB
HTML
|
<!DOCTYPE html>
|
|||
|
<html lang="zh"><head><title>垃圾回收</title><meta charset="utf-8"/><link rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&family=Noto Serif Simplified Chinese:wght@400;700&family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&display=swap"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta property="og:title" content="垃圾回收"/><meta property="og:description" content="python 垃圾回收机制."/><meta property="og:image" content="https://wiki.7wate.com/static/og-image.png"/><meta property="og:width" content="1200"/><meta property="og:height" content="675"/><link rel="icon" href="../../../../static/icon.png"/><meta name="description" content="python 垃圾回收机制."/><meta name="generator" content="Quartz"/><link href="../../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/katex.min.css" rel="stylesheet" type="text/css" spa-preserve/><script src="../../../../prescript.js" type="application/javascript" spa-preserve></script><script type="application/javascript" spa-preserve>const fetchData = fetch("../../../../static/contentIndex.json").then(data => data.json())</script></head><body data-slug="Technology/ProgrammingLanguage/Python/进阶/垃圾回收"><div id="quartz-root" class="page"><div id="quartz-body"><div class="left sidebar"><h2 class="page-title"><a href="../../../..">🪴 X·Eden</a></h2><div class="spacer mobile-only"></div><div class="search"><button class="search-button" id="search-button"><p>搜索</p><svg role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title>Search</title><g class="search-path" fill="none"><path stroke-linecap="square" d="M18.5 18.3l-5.4-5.4"></path><circle cx="8" cy="8" r="7"></circle></g></svg></button><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="搜索些什么" placeholder="搜索些什么"/><div id="search-layout" data-preview="true"></div></div></div></div><button class="darkmode" id="darkmode"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="dayIcon" x="0px" y="0px" viewBox="0 0 35 35" style="enable-background:new 0 0 35 35" xml:space="preserve" aria-label="暗色模式"><title>暗色模式</title><path d="M6,17.5C6,16.672,5.328,16,4.5,16h-3C0.672,16,0,16.672,0,17.5 S0.672,19,1.5,19h3C5.328,19,6,18.328,6,17.5z M7.5,26c-0.414,0-0.789,0.168-1.061,0.439l-2,2C4.168,28.711,4,29.086,4,29.5 C4,30.328,4.671,31,5.5,31c0.414,0,0.789-0.168,1.06-0.44l2-2C8.832,28.289,9,27.914,9,27.5C9,26.672,8.329,26,7.5,26z M17.5,6 C18.329,6,19,5.328,19,4.5v-3C19,0.672,18.329,0,17.5,0S16,0.672,16,1.5v3C16,5.328,16.671,6,17.5,6z M27.5,9 c0.414,0,0.789-0.168,1.06-0.439l2-2C30.832,6.289,31,5.914,31,5.5C31,4.672,30.329,4,29.5,4c-0.414,0-0.789,0.168-1.061,0.44 l-2,2C26.168,6.711,26,7.086,26,7.5C26,8.328,26.671,9,27.5,9z M6.439,8.561C6.711,8.832,7.086,9,7.5,9C8.328,9,9,8.328,9,7.5 c0-0.414-0.168-0.789-0.439-1.061l-2-2C6.289,4.168,5.914,4,5.5,4C4.672,4,4,4.672,4,5.5c0,0.414,0.168,0.789,0.439,1.06 L6.439,8.561z M33.5,16h-3c-0.828,0-1.5,0.672-1.5,1.5s0.672,1.5,1.5,1.5h3c0.828,0,1.5-0.672,1.5-1.5S34.328,16,33.5,16z M28.561,26.439C28.289,26.168,27.914,26,27.5,26c-0.828,0-1.5,0.672-1.5,1.5c0,0.414,0.168,0.789,0.439,1.06l2,2 C28.711,30.832,29.086,31,29.5,31c0.828,0,1.5-0.672,1.5-1.5c0-0.414-0.168-0.789-0.439-1.061L28.561,26.439z M17.5,29 c-0.829,0-1.5,0.672-1.5,1.5v3c0,0.828,0.671,1.5,1.5,1.5s1.5-0.672,1.5-1.5v-3C19,29.672,18.329,29,17.5,29z M17.5,7 C11.71,7,7,11.71,7,17.5S11.71,28,17.5,28S28,23.29,28,17.5S23.29,7,17.5,7z M17.5,25c-4.136,0-7.5-3.364-7.5-7.5 c0-4.136,3.364-7.5,7.5-7.5c4.136,0,7.5,3.364,7.5,7.5C25,21.636,21.636,25,17.5,25z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="nig
|
|||
|
<h3 id="python-中的引用概念">Python 中的引用概念<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#python-中的引用概念" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>**在 Python 中,所有的数据都是以对象的形式存在。**当我们创建一个变量并赋值时,实际上 Python 会为我们创建一个对象,然后变量会引用这个对象。这种关系,我们称之为引用。</p>
|
|||
|
<p>例如:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span></span></code></pre></figure>
|
|||
|
<p>这里,数字 <code>1</code> 是一个 <code>int</code> 类型的对象,变量 <code>a</code> 是对这个对象的引用。</p>
|
|||
|
<h3 id="引用计数的基本原理">引用计数的基本原理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#引用计数的基本原理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>**Python 使用一种叫做引用计数的方式来管理内存。**每一个对象都有一个引用计数,用来记录有多少个引用指向这个对象。当这个计数值变为 0 时, Python 就知道没有任何引用指向这个对象,这个对象就可以被安全地销毁了,它占用的内存就会被释放。</p>
|
|||
|
<p>例如:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 引用计数为1</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">b </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 引用计数为2</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> None</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 引用计数为1</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">b </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> None</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 引用计数为0,对象被销毁</span></span></code></pre></figure>
|
|||
|
<h3 id="引用计数的增减和对象的创建与销毁过程">引用计数的增减和对象的创建与销毁过程<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#引用计数的增减和对象的创建与销毁过程" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<pre><code class="mermaid">graph LR
|
|||
|
A[Python 对象] --> |创建| B(引用计数增加)
|
|||
|
B --> C{引用计数是否为0?}
|
|||
|
C -->|是| D[对象被立即回收]
|
|||
|
C -->|否| E[对象继续存在]
|
|||
|
E --> F{是否存在循环引用?}
|
|||
|
F --> |是| G[标记-清除处理]
|
|||
|
G --> H[分代回收处理]
|
|||
|
F --> |否| I[对象持续存在]
|
|||
|
H --> J{是否到达阈值?}
|
|||
|
J -->|是| K[触发垃圾回收]
|
|||
|
J -->|否| I
|
|||
|
K --> L[回收结束,对象被销毁]
|
|||
|
</code></pre>
|
|||
|
<p>每当我们创建一个新的引用(赋值操作),对象的引用计数就会增加 1。当我们删除一个引用(例如赋值为 <code>None</code> 或者使用 <code>del</code> 命令),对象的引用计数就会减少 1。当引用计数变为 0 时,Python 的垃圾回收器就会销毁这个对象并回收它所占用的内存。</p>
|
|||
|
<h2 id="循环引用和引用计数的限制">循环引用和引用计数的限制<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#循环引用和引用计数的限制" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<h3 id="循环引用的概念和问题">循环引用的概念和问题<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#循环引用的概念和问题" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>**循环引用是指两个或更多的对象互相引用,形成一种闭环。**在这种情况下,即使没有其他引用指向这些对象,它们的引用计数也永远不会变为 0,所以它们不会被 Python 的垃圾回收器销毁,导致内存泄漏。</p>
|
|||
|
<p>例如:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Node</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> def</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> __init__</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(self):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> self</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">.other </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> None</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">a </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Node()</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">b </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Node()</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">a.other </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># a 引用了 b</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">b.other </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># b 引用了 a,形成了循环引用</span></span></code></pre></figure>
|
|||
|
<h3 id="引用计数无法解决的循环引用问题">引用计数无法解决的循环引用问题<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#引用计数无法解决的循环引用问题" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>这正是引用计数法的一个主要弱点。尽管它简单易懂,但它不能处理循环引用的问题。在上面的例子中,即使我们删除了对 <code>a</code> 和 <code>b</code> 的引用,它们也不会被销毁,因为它们互相引用,它们的引用计数永远不会变为 0。</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">del</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">del</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> b</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 现在,a 和 b 形成的循环引用对象仍然存在,但我们无法访问它们</span></span></code></pre></figure>
|
|||
|
<h3 id="引用计数的弱点和限制">引用计数的弱点和限制<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#引用计数的弱点和限制" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>引用计数法的另一个弱点是它的开销相对较大。每次创建或删除引用时,Python 都需要更新引用计数。这可能在大量对象创建和销毁的情况下成为性能瓶颈。</p>
|
|||
|
<h2 id="垃圾回收算法">垃圾回收算法<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#垃圾回收算法" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<h3 id="垃圾回收算法的概述">垃圾回收算法的概述<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#垃圾回收算法的概述" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>为了解决引用计数法不能处理循环引用的问题,Python 引入了两种垃圾回收算法:<strong>标记 - 清除算法和分代回收算法。这两种算法都是为了检测和回收循环引用的对象。</strong></p>
|
|||
|
<h3 id="标记---清除算法的原理和流程">标记 - 清除算法的原理和流程<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#标记---清除算法的原理和流程" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>标记 - 清除算法是一种基础的垃圾回收算法。它的**基本原理是通过标记和清除两个步骤来回收垃圾对象。**在标记步骤中,从某些根对象(例如全局变量)出发,遍历所有可达的对象,将这些对象标记为活动。剩下的未被标记的对象(即不可达的对象)就被认为是垃圾。然后在“清除”步骤中,清除所有标记为“垃圾”的对象。</p>
|
|||
|
<p>在 Python 中,标记 - 清除算法主要用于检测和清除循环引用对象。它的工作流程是这样的:</p>
|
|||
|
<ol>
|
|||
|
<li>从所有的容器对象(例如列表、字典和类实例等)出发,找出所有可能形成循环引用的对象。</li>
|
|||
|
<li>对这些对象应用标记 - 清除算法,找出并清除真正的循环引用对象。</li>
|
|||
|
</ol>
|
|||
|
<h3 id="分代回收算法的原理和优化">分代回收算法的原理和优化<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#分代回收算法的原理和优化" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>分代回收是 Python 用来优化垃圾回收性能的一种方式。它的**基本思想是将所有的对象分为几代,每一代的对象有自己的生命周期和回收策略。**新创建的对象被放入第一代,当这一代的对象经历了一定次数的垃圾回收后仍然存活,就被移到下一代。每一代的垃圾回收频率都不同,通常,越年轻的代的垃圾回收频率越高。</p>
|
|||
|
<p>Python 的分代回收有三代。新创建的对象被放入第一代(<code>generation 0</code>),当它经历了一次垃圾回收后仍然存活,就被移到第二代(<code>generation 1</code>)。同样,第二代的对象在经历了一次垃圾回收后仍然存活,就被移到第三代(<code>generation 2</code>)。第三代的对象在经历了一次垃圾回收后仍然存活,就留在第三代。每一代的垃圾回收频率都不同,第一代的频率最高,第三代的频率最低。</p>
|
|||
|
<p>分代回收算法的优点是它可以减少垃圾回收的开销,因为经常产生垃圾的通常是生命周期短的对象(例如临时变量),而生命周期长的对象(例如全局变量)很少产生垃圾。这种方式可以让 Python 更加聚焦于可能产生垃圾的地方,从而提高垃圾回收的效率。</p>
|
|||
|
<h3 id="python-中的垃圾回收算法实现">Python 中的垃圾回收算法实现<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#python-中的垃圾回收算法实现" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<pre><code class="mermaid">sequenceDiagram
|
|||
|
participant O as Python对象
|
|||
|
participant RC as 引用计数
|
|||
|
participant GM as 垃圾回收机制
|
|||
|
participant GC as gc模块
|
|||
|
O->>RC: 创建对象,引用计数+1
|
|||
|
RC-->>O: 维持对象
|
|||
|
RC->>GM: 检查引用计数是否为0
|
|||
|
GM-->>RC: 不为0,继续维持
|
|||
|
RC->>GM: 引用计数变为0
|
|||
|
GM-->>O: 对象被立即回收
|
|||
|
GM->>GC: 检查是否存在循环引用
|
|||
|
GC-->>GM: 存在循环引用
|
|||
|
GM->>GC: 触发标记-清除
|
|||
|
GC-->>GM: 清除循环引用
|
|||
|
GM->>GC: 触发分代回收
|
|||
|
GC-->>GM: 分代回收完成
|
|||
|
GM->>GC: 检查是否达到阈值
|
|||
|
GC-->>GM: 达到阈值
|
|||
|
GM->>O: 触发垃圾回收,对象被销毁
|
|||
|
GM->>GC: 检查是否达到阈值
|
|||
|
GC-->>GM: 没有达到阈值
|
|||
|
GM-->>GC: 继续监视
|
|||
|
</code></pre>
|
|||
|
<p>Python 的垃圾回收机制是基于引用计数的,当对象的引用计数降到 0 时,该对象就会被立即回收。但是,对于循环引用的问题,Python 使用标记 - 清除和分代回收两种算法来解决。</p>
|
|||
|
<p>首先,Python 使用标记 - 清除算法来检测和清除循环引用的对象。然后,为了优化垃圾回收的性能,Python 会根据对象的存活时间将它们分成三代,并分别进行回收。这样,Python 就能有效地管理内存,同时尽可能地降低垃圾回收的开销。</p>
|
|||
|
<h2 id="gc-模块">Gc 模块<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#gc-模块" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<p>Python 通过 <code>gc</code> 模块提供了对垃圾回收机制的直接控制。<code>gc</code> 模块提供了一些函数,让我们可以手动触发垃圾回收,查询垃圾回收的状态,或者调整垃圾回收的参数。</p>
|
|||
|
<h3 id="基础功能">基础功能<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#基础功能" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>下面是一些常用的 <code>gc</code> 函数:</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="table-container"><table><thead><tr><th style="text-align:left;">函数</th><th style="text-align:left;">描述</th></tr></thead><tbody><tr><td style="text-align:left;"><code>gc.enable()</code></td><td style="text-align:left;">启用自动垃圾回收。</td></tr><tr><td style="text-align:left;"><code>gc.disable()</code></td><td style="text-align:left;">禁用自动垃圾回收。</td></tr><tr><td style="text-align:left;"><code>gc.isenabled()</code></td><td style="text-align:left;">查看自动垃圾回收是否被启用。</td></tr><tr><td style="text-align:left;"><code>gc.collect(generation=2)</code></td><td style="text-align:left;">立即进行一次垃圾回收。可以通过 <code>generation</code> 参数指定要收集的代的编号(0 代表最年轻的一代,2 代表所有代)。</td></tr><tr><td style="text-align:left;"><code>gc.set_threshold(threshold0, threshold1=None, threshold2=None)</code></td><td style="text-align:left;">设置垃圾回收的阈值。当某一代的垃圾数量超过对应的阈值时,就会触发垃圾回收。</td></tr><tr><td style="text-align:left;"><code>gc.get_threshold()</code></td><td style="text-align:left;">获取当前的垃圾回收阈值。</td></tr><tr><td style="text-align:left;"><code>gc.get_count()</code></td><td style="text-align:left;">获取当前每一代的垃圾数量。</td></tr><tr><td style="text-align:left;"><code>gc.get_objects()</code></td><td style="text-align:left;">返回一个列表,包含所有当前被监视的对象。</td></tr><tr><td style="text-align:left;"><code>gc.get_stats()</code></td><td style="text-align:left;">返回一个字典,包含垃圾回收的统计信息。</td></tr><tr><td style="text-align:left;"><code>gc.set_debug(flags)</code></td><td style="text-align:left;">设置垃圾回收的调试标志。</td></tr><tr><td style="text-align:left;"><code>gc.get_debug()</code></td><td style="text-align:left;">获取当前的垃圾回收调试标志。</td></tr><tr><td style="text-align:left;"><code>gc.get_referents(*objs)</code></td><td style="text-align:left;">返回一个列表,包含所有给定对象的直接引用对象。</td></tr><tr><td style="text-align:left;"><code>gc.get_referrers(*objs)</code></td><td style="text-align:left;">返回一个列表,包含所有直接引用给定对象的对象。</td></tr></tbody></table></div>
|
|||
|
<p>下面是一个简单的例子,演示了如何使用 <code>gc</code> 模块:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> gc</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 手动触发垃圾回收</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">gc.collect()</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取当前的计数器值</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">print</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(gc.get_count())</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取当前的阈值</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">print</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(gc.get_threshold())</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置新的阈值</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">gc.set_threshold(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">500</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取所有存在的对象</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">print</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">len</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(gc.get_objects()))</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 获取统计信息</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">print</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(gc.get_stats())</span></span></code></pre></figure>
|
|||
|
<h3 id="高级用法">高级用法<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#高级用法" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
|
|||
|
<p>除了上述的基本功能,<code>gc</code> 模块还提供了一些高级功能,例如你可以注册自己的回调函数,当垃圾回收发生时,这些回调函数就会被调用。这可以用来监控垃圾回收的过程,或者调试内存泄漏的问题。</p>
|
|||
|
<p>下面是一个例子,演示了如何注册回调函数:</p>
|
|||
|
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">import</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> gc</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> callback</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(phase, info):</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> print</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">f</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">{</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">phase</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">}</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">{</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">info</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">}</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 注册回调函数</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">gc.callbacks.append(callback)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 触发垃圾回收</span></span>
|
|||
|
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">gc.collect()</span></span></code></pre></figure>
|
|||
|
<p>在这个例子中,每次垃圾回收发生时,<code>callback</code> 函数都会被调用,它会打印出垃圾回收的阶段和一些信息。</p>
|
|||
|
<h2 id="总结">总结<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#总结" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
|
|||
|
<p>Python 的垃圾回收机制是基于引用计数的,它简单高效,但无法处理循环引用的问题。为了解决这个问题,Python 引入了标记 - 清除和分代回收两种垃圾回收算法。这两种算法可以有效地检测和清除循环引用的对象,同时优化垃圾回收的性能。</p>
|
|||
|
<p>Python 通过 <code>gc</code> 模块提供了对垃圾回收机制的直接控制。通过 <code>gc</code> 模块,我们可以手动触发垃圾回收,查询垃圾回收的状态,或者调整垃圾回收的参数。我们甚至可以注册自己的回调函数,以便在垃圾回收发生时获取通知。</p></article><hr/><div class="page-footer"></div></div><div class="right sidebar"><div class="graph"><h3>关系图谱</h3><div class="graph-outer"><div id="graph-container" data-cfg="{"drag":true,"zoom":true,"depth":1,"scale":1.1,"repelForce":0.5,"centerForce":0.3,"linkDistance":30,"fontSize":0.6,"opacityScale":1,"showTags":true,"removeTags":[],"focusOnHover":false}"></div><button id="global-graph-icon" aria-label="Global Graph"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xml:space="preserve"><path d="M49,0c-3.309,0-6,2.691-6,6c0,1.035,0.263,2.009,0.726,2.86l-9.829,9.829C32.542,17.634,30.846,17,29,17
|
|||
|
s-3.542,0.634-4.898,1.688l-7.669-7.669C16.785,10.424,17,9.74,17,9c0-2.206-1.794-4-4-4S9,6.794,9,9s1.794,4,4,4
|
|||
|
c0.74,0,1.424-0.215,2.019-0.567l7.669,7.669C21.634,21.458,21,23.154,21,25s0.634,3.542,1.688,4.897L10.024,42.562
|
|||
|
C8.958,41.595,7.549,41,6,41c-3.309,0-6,2.691-6,6s2.691,6,6,6s6-2.691,6-6c0-1.035-0.263-2.009-0.726-2.86l12.829-12.829
|
|||
|
c1.106,0.86,2.44,1.436,3.898,1.619v10.16c-2.833,0.478-5,2.942-5,5.91c0,3.309,2.691,6,6,6s6-2.691,6-6c0-2.967-2.167-5.431-5-5.91
|
|||
|
v-10.16c1.458-0.183,2.792-0.759,3.898-1.619l7.669,7.669C41.215,39.576,41,40.26,41,41c0,2.206,1.794,4,4,4s4-1.794,4-4
|
|||
|
s-1.794-4-4-4c-0.74,0-1.424,0.215-2.019,0.567l-7.669-7.669C36.366,28.542,37,26.846,37,25s-0.634-3.542-1.688-4.897l9.665-9.665
|
|||
|
C46.042,11.405,47.451,12,49,12c3.309,0,6-2.691,6-6S52.309,0,49,0z M11,9c0-1.103,0.897-2,2-2s2,0.897,2,2s-0.897,2-2,2
|
|||
|
S11,10.103,11,9z M6,51c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S8.206,51,6,51z M33,49c0,2.206-1.794,4-4,4s-4-1.794-4-4
|
|||
|
s1.794-4,4-4S33,46.794,33,49z M29,31c-3.309,0-6-2.691-6-6s2.691-6,6-6s6,2.691,6,6S32.309,31,29,31z M47,41c0,1.103-0.897,2-2,2
|
|||
|
s-2-0.897-2-2s0.897-2,2-2S47,39.897,47,41z M49,10c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S51.206,10,49,10z"></path></svg></button></div><div id="global-graph-outer"><div id="global-graph-container" data-cfg="{"drag":true,"zoom":true,"depth":-1,"scale":0.9,"repelForce":0.5,"centerForce":0.3,"linkDistance":30,"fontSize":0.6,"opacityScale":1,"showTags":true,"removeTags":[],"focusOnHover":true}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class aria-controls="toc-content" aria-expanded="true"><h3>目录</h3><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="fold"><polyline points="6 9 12 15 18 9"></polyline></svg></button><div id="toc-content" class><ul class="overflow"><li class="depth-0"><a href="#引用和引用计数" data-for="引用和引用计数">引用和引用计数</a></li><li class="depth-1"><a href="#python-中的引用概念" data-for="python-中的引用概念">Python 中的引用概念</a></li><li class="depth-1"><a href="#引用计数的基本原理" data-for="引用计数的基本原理">引用计数的基本原理</a></li><li class="depth-1"><a href="#引用计数的增减和对象的创建与销毁过程" data-for="引用计数的增减和对象的创建与销毁过程">引用计数的增减和对象的创建与销毁过程</a></li><li class="depth-0"><a href="#循环引用和引用计数的限制" data-for="循环引用和引用计数的限制">循环引用和引用计数的限制</a></li><li class="depth-1"><a href="#循环引用的概念和问题" data-for="循环引用的概念和问题">循环引用的概念和问题</a></li><li class="depth-1"><a href="#引用计数无法解决的循环引用问题" data-for="引用计数无法解决的循环引用问题">引用计数无法解决的循环引用问题</a></li><li class="depth-1"><a href="#引用计数的弱点和限制" data-for="引用计数的弱点和限制">引用计数的弱点和限制</a></li><li class="depth-0"><a href="#垃圾回收算法" data-for="垃圾回收算法">垃圾回收算法</a></li><li class="depth-1"><a href="#垃圾回收算法的概述" data-for="垃圾回收算法的概述">垃圾回收算法的概述</a></li><li class="depth-1"><a href="#标记---清除算法的原理和流程" data-for="标记---清除算法的原理和流程">标记 - 清除算法的原理和流程</a></li><li class="depth-1"><a href="#分代回收算法的原理和优化" data-for="分代回收算法的原理和优化">分代回收算法的原理和优化</a></li><li class="depth-1"><a href="#python-中的垃圾回收算法实现" data-for="python-中的垃圾回收算法实现">Python 中的垃圾回收算法实现</a></li><li class="depth-0"><a href="#gc-模块" data-for="gc-模块">Gc 模块</a></li><li class="depth-1"><a href="#基础功能" data-for="基础功能">基础功能</a></li><li class="depth-1"><a href="#高级用法" data-for="高级用法">高级用法</a></li><li class="depth-0"><a href="#总结" data-for="总结">总结</a></li></ul></div></div><div class="explorer mobile-only"><button type="button" id="explorer" data-behavior="collapse" data-collapsed="collapsed" data-savestate="true" data-tree="[{"path":"Personal","collapsed":true},{"path":"Personal/Blog","collapsed":true},{"path":"Personal/Blog/2018","collapsed":true},{"path":"Personal/Blog/2020","collapsed":true},{"path":"Personal/Blog/2021","collapsed":true},{"path":"Personal/Blog/2022","collapsed":true},{"path":"Personal/Blog/2023","collapsed":true},{"path":"Personal/Blog/2024","collapsed":true},{"path":"Personal/Book&qu
|
|||
|
</script><script type="module">
|
|||
|
let mermaidImport = undefined
|
|||
|
document.addEventListener('nav', async () => {
|
|||
|
if (document.querySelector("code.mermaid")) {
|
|||
|
mermaidImport ||= await import('https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.7.0/mermaid.esm.min.mjs')
|
|||
|
const mermaid = mermaidImport.default
|
|||
|
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
|
|||
|
mermaid.initialize({
|
|||
|
startOnLoad: false,
|
|||
|
securityLevel: 'loose',
|
|||
|
theme: darkMode ? 'dark' : 'default'
|
|||
|
})
|
|||
|
|
|||
|
await mermaid.run({
|
|||
|
querySelector: '.mermaid'
|
|||
|
})
|
|||
|
}
|
|||
|
});
|
|||
|
</script><script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../../postscript.js" type="module"></script></html>
|