{"id":10021224,"date":"2026-02-01T02:39:21","date_gmt":"2026-01-31T18:39:21","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021224"},"modified":"2026-02-01T02:39:21","modified_gmt":"2026-01-31T18:39:21","slug":"python%e4%b8%ad%e7%9a%84%e5%86%85%e5%ad%98%e7%ae%a1%e7%90%86%e4%b8%8e%e5%9e%83%e5%9c%be%e5%9b%9e%e6%94%b6%e7%ae%97%e6%b3%95%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021224","title":{"rendered":"Python\u4e2d\u7684\u5185\u5b58\u7ba1\u7406\u4e0e\u5783\u573e\u56de\u6536\u7b97\u6cd5\u5206\u6790"},"content":{"rendered":"<h2>Python\u4e2d\u7684\u5185\u5b58\u7ba1\u7406\u4e0e\u5783\u573e\u56de\u6536\u7b97\u6cd5\u5206\u6790<\/h2>\n<h3>1. \u5185\u5b58\u7ba1\u7406\u6982\u8ff0<\/h3>\n<h4>1.1 Python\u5185\u5b58\u7ba1\u7406\u7684\u5c42\u6b21<\/h4>\n<p>Python\u7684\u5185\u5b58\u7ba1\u7406\u5206\u4e3a\u4e09\u4e2a\u5c42\u6b21\uff1a<\/p>\n<ol>\n<li><strong>\u5e95\u5c42\u5185\u5b58\u5206\u914d<\/strong>\uff1a\u7531C\u6807\u51c6\u5e93\u7684<code>malloc<\/code>\/<code>free<\/code>\u7ba1\u7406<\/li>\n<li><strong>\u5185\u5b58\u6c60<\/strong>\uff1aPython\u7684\u5185\u5b58\u6c60\u673a\u5236\uff0c\u7ba1\u7406\u5c0f\u5185\u5b58\u5206\u914d<\/li>\n<li><strong>\u5bf9\u8c61\u7ba1\u7406<\/strong>\uff1aPython\u5bf9\u8c61\u7684\u521b\u5efa\u3001\u4f7f\u7528\u548c\u9500\u6bc1<\/li>\n<\/ol>\n<h4>1.2 \u5185\u5b58\u5206\u914d\u7b56\u7565<\/h4>\n<ul>\n<li><strong>\u5c0f\u5bf9\u8c61<\/strong>\uff1a\u4f7f\u7528\u5185\u5b58\u6c60\u5206\u914d\uff08&lt; 256KB\uff09<\/li>\n<li><strong>\u5927\u5bf9\u8c61<\/strong>\uff1a\u76f4\u63a5\u4f7f\u7528C\u6807\u51c6\u5e93\u5206\u914d\uff08\u2265 256KB\uff09<\/li>\n<li><strong>\u5b57\u7b26\u4e32\u548c\u6574\u6570<\/strong>\uff1a\u4f7f\u7528\u5bf9\u8c61\u6c60\u7f13\u5b58<\/li>\n<\/ul>\n<h4>1.3 \u5185\u5b58\u7ba1\u7406\u7684\u91cd\u8981\u6027<\/h4>\n<ul>\n<li>\u63d0\u9ad8\u7a0b\u5e8f\u6027\u80fd<\/li>\n<li>\u51cf\u5c11\u5185\u5b58\u6cc4\u6f0f<\/li>\n<li>\u4f18\u5316\u5185\u5b58\u4f7f\u7528<\/li>\n<li>\u907f\u514d\u5185\u5b58\u788e\u7247<\/li>\n<\/ul>\n<h3>2. \u5f15\u7528\u8ba1\u6570\u673a\u5236<\/h3>\n<h4>2.1 \u5f15\u7528\u8ba1\u6570\u7684\u57fa\u672c\u539f\u7406<\/h4>\n<p>\u5f15\u7528\u8ba1\u6570\u662fPython\u6700\u57fa\u672c\u7684\u5783\u573e\u56de\u6536\u673a\u5236\uff0c\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u5f15\u7528\u8ba1\u6570\u5668\uff0c\u5f53\u5f15\u7528\u8ba1\u6570\u4e3a0\u65f6\uff0c\u5bf9\u8c61\u88ab\u9500\u6bc1\u3002<\/p>\n<pre><code class=\"python\">import sys\n\n# \u67e5\u770b\u5f15\u7528\u8ba1\u6570\na = &quot;hello&quot;\nprint(sys.getrefcount(a))  # \u8f93\u51fa: 2 (\u56e0\u4e3agetrefcount\u672c\u8eab\u4e5f\u4f1a\u589e\u52a0\u4e00\u6b21\u5f15\u7528)\n\nb = a\nprint(sys.getrefcount(a))  # \u8f93\u51fa: 3\n\ndel b\nprint(sys.getrefcount(a))  # \u8f93\u51fa: 2<\/code><\/pre>\n<h4>2.2 \u5f15\u7528\u8ba1\u6570\u7684\u589e\u51cf<\/h4>\n<p><strong>\u5f15\u7528\u8ba1\u6570\u589e\u52a0\u7684\u60c5\u51b5<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5bf9\u8c61\u88ab\u521b\u5efa\uff1a<code>a = 10<\/code><\/li>\n<li>\u5bf9\u8c61\u88ab\u8d4b\u503c\u7ed9\u5176\u4ed6\u53d8\u91cf\uff1a<code>b = a<\/code><\/li>\n<li>\u5bf9\u8c61\u88ab\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff1a<code>func(a)<\/code><\/li>\n<li>\u5bf9\u8c61\u88ab\u6dfb\u52a0\u5230\u5bb9\u5668\u4e2d\uff1a<code>list.append(a)<\/code><\/li>\n<\/ul>\n<p><strong>\u5f15\u7528\u8ba1\u6570\u51cf\u5c11\u7684\u60c5\u51b5<\/strong>\uff1a<\/p>\n<ul>\n<li>\u53d8\u91cf\u88ab\u5220\u9664\uff1a<code>del a<\/code><\/li>\n<li>\u53d8\u91cf\u88ab\u8d4b\u503c\u7ed9\u5176\u4ed6\u5bf9\u8c61\uff1a<code>a = None<\/code><\/li>\n<li>\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\uff0c\u5c40\u90e8\u53d8\u91cf\u88ab\u9500\u6bc1<\/li>\n<li>\u5bf9\u8c61\u4ece\u5bb9\u5668\u4e2d\u79fb\u9664\uff1a<code>list.remove(a)<\/code><\/li>\n<li>\u5bb9\u5668\u672c\u8eab\u88ab\u9500\u6bc1<\/li>\n<\/ul>\n<h4>2.3 \u5f15\u7528\u8ba1\u6570\u7684\u4f18\u7f3a\u70b9<\/h4>\n<p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5b9e\u65f6\u6027\uff1a\u5bf9\u8c61\u4e00\u65e6\u6ca1\u6709\u5f15\u7528\u5c31\u7acb\u5373\u88ab\u56de\u6536<\/li>\n<li>\u5b9e\u73b0\u7b80\u5355<\/li>\n<li>\u5185\u5b58\u56de\u6536\u7684\u5f00\u9500\u5206\u6563\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d<\/li>\n<\/ul>\n<p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<ul>\n<li>\u65e0\u6cd5\u5904\u7406\u5faa\u73af\u5f15\u7528<\/li>\n<li>\u5f15\u7528\u8ba1\u6570\u64cd\u4f5c\u672c\u8eab\u6709\u5f00\u9500<\/li>\n<li>\u5bf9\u4e8e\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u7684\u5bf9\u8c61\u6548\u7387\u8f83\u4f4e<\/li>\n<\/ul>\n<h4>2.4 \u5faa\u73af\u5f15\u7528\u95ee\u9898<\/h4>\n<pre><code class=\"python\"># \u5faa\u73af\u5f15\u7528\u793a\u4f8b\nclass Node:\n    def __init__(self):\n        self.next = None\n\na = Node()\nb = Node()\na.next = b\nb.next = a\n\n# \u6b64\u65f6\u5373\u4f7f\u5220\u9664a\u548cb\uff0c\u5b83\u4eec\u7684\u5f15\u7528\u8ba1\u6570\u4ecd\u4e3a1\n# \u56e0\u4e3a\u5b83\u4eec\u4e92\u76f8\u5f15\u7528\ndel a\ndel b\n# \u8fd9\u91cc\u4f1a\u4ea7\u751f\u5185\u5b58\u6cc4\u6f0f\uff0c\u76f4\u5230\u5783\u573e\u56de\u6536\u5668\u8fd0\u884c<\/code><\/pre>\n<h3>3. \u5783\u573e\u56de\u6536\u7b97\u6cd5<\/h3>\n<h4>3.1 \u6807\u8bb0-\u6e05\u9664\u7b97\u6cd5<\/h4>\n<p><strong>\u6807\u8bb0-\u6e05\u9664<\/strong>\uff08Mark and Sweep\uff09\u662fPython\u7528\u4e8e\u5904\u7406\u5faa\u73af\u5f15\u7528\u7684\u4e3b\u8981\u7b97\u6cd5\uff1a<\/p>\n<ol>\n<li><strong>\u6807\u8bb0\u9636\u6bb5<\/strong>\uff1a\u4ece\u6839\u5bf9\u8c61\uff08\u5982\u5168\u5c40\u53d8\u91cf\u3001\u6808\u4e2d\u7684\u53d8\u91cf\uff09\u51fa\u53d1\uff0c\u6807\u8bb0\u6240\u6709\u53ef\u8fbe\u7684\u5bf9\u8c61<\/li>\n<li><strong>\u6e05\u9664\u9636\u6bb5<\/strong>\uff1a\u56de\u6536\u6240\u6709\u672a\u88ab\u6807\u8bb0\u7684\u5bf9\u8c61<\/li>\n<\/ol>\n<p><strong>\u6839\u5bf9\u8c61<\/strong>\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u5168\u5c40\u53d8\u91cf<\/li>\n<li>\u6808\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf<\/li>\n<li>\u5bc4\u5b58\u5668\u4e2d\u7684\u53d8\u91cf<\/li>\n<\/ul>\n<h4>3.2 \u5206\u4ee3\u56de\u6536\u673a\u5236<\/h4>\n<p>Python\u91c7\u7528<strong>\u5206\u4ee3\u56de\u6536<\/strong>\uff08Generational Garbage Collection\uff09\u7b56\u7565\uff0c\u5c06\u5bf9\u8c61\u5206\u4e3a\u4e09\u4e2a\u4ee3\uff1a<\/p>\n<ul>\n<li><strong>0\u4ee3<\/strong>\uff1a\u65b0\u521b\u5efa\u7684\u5bf9\u8c61<\/li>\n<li><strong>1\u4ee3<\/strong>\uff1a\u7ecf\u8fc7\u4e00\u6b21\u5783\u573e\u56de\u6536\u540e\u4ecd\u7136\u5b58\u5728\u7684\u5bf9\u8c61<\/li>\n<li><strong>2\u4ee3<\/strong>\uff1a\u7ecf\u8fc7\u591a\u6b21\u5783\u573e\u56de\u6536\u540e\u4ecd\u7136\u5b58\u5728\u7684\u5bf9\u8c61<\/li>\n<\/ul>\n<p><strong>\u56de\u6536\u9891\u7387<\/strong>\uff1a<\/p>\n<ul>\n<li>0\u4ee3\uff1a\u6700\u9891\u7e41\uff08\u5f53\u5bf9\u8c61\u6570\u91cf\u8fbe\u5230\u9608\u503c\u65f6\uff09<\/li>\n<li>1\u4ee3\uff1a\u5f530\u4ee3\u56de\u6536\u4e00\u5b9a\u6b21\u6570\u540e<\/li>\n<li>2\u4ee3\uff1a\u5f531\u4ee3\u56de\u6536\u4e00\u5b9a\u6b21\u6570\u540e<\/li>\n<\/ul>\n<h4>3.3 \u5783\u573e\u56de\u6536\u7684\u89e6\u53d1\u6761\u4ef6<\/h4>\n<pre><code class=\"python\">import gc\n\n# \u624b\u52a8\u89e6\u53d1\u5783\u573e\u56de\u6536\ngc.collect()\n\n# \u67e5\u770b\u5f53\u524d\u5404\u4ee3\u5bf9\u8c61\u6570\u91cf\nprint(gc.get_count())  # \u8fd4\u56de (generation0, generation1, generation2)\n\n# \u8bbe\u7f6e\u56de\u6536\u9608\u503c\ngc.set_threshold(700, 10, 10)  # (threshold0, threshold1, threshold2)<\/code><\/pre>\n<h4>3.4 \u5783\u573e\u56de\u6536\u7684\u4f18\u5316<\/h4>\n<ul>\n<li><strong>\u589e\u91cf\u56de\u6536<\/strong>\uff1a\u5c06\u6807\u8bb0-\u6e05\u9664\u8fc7\u7a0b\u5206\u6210\u591a\u4e2a\u5c0f\u6b65\u9aa4\uff0c\u907f\u514d\u957f\u65f6\u95f4\u963b\u585e<\/li>\n<li><strong>\u4e09\u8272\u6807\u8bb0<\/strong>\uff1a\u4f7f\u7528\u767d\u8272\u3001\u7070\u8272\u3001\u9ed1\u8272\u6807\u8bb0\u5bf9\u8c61\u72b6\u6001\uff0c\u63d0\u9ad8\u6807\u8bb0\u6548\u7387<\/li>\n<li><strong>\u5199\u5c4f\u969c<\/strong>\uff1a\u5728\u5bf9\u8c61\u5f15\u7528\u53d8\u5316\u65f6\u8bb0\u5f55\uff0c\u51cf\u5c11\u91cd\u590d\u626b\u63cf<\/li>\n<\/ul>\n<h3>4. \u5185\u5b58\u5206\u914d\u673a\u5236<\/h3>\n<h4>4.1 \u5185\u5b58\u6c60\u5b9e\u73b0<\/h4>\n<p>Python\u7684\u5185\u5b58\u6c60\u7531<code>pymalloc<\/code>\u5b9e\u73b0\uff0c\u5206\u4e3a\u591a\u4e2a\u5c42\u6b21\uff1a<\/p>\n<ol>\n<li><strong>arena<\/strong>\uff1a\u6700\u5927\u7684\u5185\u5b58\u5757\uff08\u7ea6256KB\uff09<\/li>\n<li><strong>pool<\/strong>\uff1aarena\u4e2d\u7684\u5185\u5b58\u5757\uff084KB\uff09<\/li>\n<li><strong>block<\/strong>\uff1a\u6700\u5c0f\u7684\u5185\u5b58\u5206\u914d\u5355\u4f4d\uff088\u5b57\u8282\u7684\u500d\u6570\uff09<\/li>\n<\/ol>\n<h4>4.2 \u5c0f\u5bf9\u8c61\u5206\u914d<\/h4>\n<p>\u5bf9\u4e8e\u5c0f\u5bf9\u8c61\uff08&lt; 256KB\uff09\uff0cPython\u4f7f\u7528\u5185\u5b58\u6c60\u5206\u914d\uff1a<\/p>\n<ul>\n<li>8\u5b57\u8282\uff1a\u7528\u4e8e<code>int<\/code>\u3001<code>float<\/code>\u7b49<\/li>\n<li>16\u5b57\u8282\uff1a\u7528\u4e8e<code>str<\/code>\u3001<code>list<\/code>\u7b49<\/li>\n<li>24\u5b57\u8282\uff1a\u7528\u4e8e<code>dict<\/code>\u7b49<\/li>\n<\/ul>\n<h4>4.3 \u5927\u5bf9\u8c61\u5206\u914d<\/h4>\n<p>\u5bf9\u4e8e\u5927\u5bf9\u8c61\uff08\u2265 256KB\uff09\uff0cPython\u76f4\u63a5\u4f7f\u7528C\u6807\u51c6\u5e93\u7684<code>malloc<\/code>\u5206\u914d\uff0c\u907f\u514d\u5360\u7528\u5185\u5b58\u6c60\u7a7a\u95f4\u3002<\/p>\n<h4>4.4 \u5185\u5b58\u788e\u7247\u7ba1\u7406<\/h4>\n<ul>\n<li><strong>\u5185\u5b58\u6c60<\/strong>\uff1a\u51cf\u5c11\u5c0f\u5bf9\u8c61\u5206\u914d\u7684\u788e\u7247<\/li>\n<li><strong>arena\u590d\u7528<\/strong>\uff1a\u56de\u6536\u548c\u91cd\u7528\u5185\u5b58\u5757<\/li>\n<li><strong>\u5927\u5bf9\u8c61\u76f4\u63a5\u5206\u914d<\/strong>\uff1a\u907f\u514d\u5927\u5bf9\u8c61\u5bf9\u5185\u5b58\u6c60\u7684\u5f71\u54cd<\/li>\n<\/ul>\n<h3>5. \u5185\u5b58\u7ba1\u7406\u7684\u5b9e\u9645\u5e94\u7528<\/h3>\n<h4>5.1 \u5185\u5b58\u6cc4\u6f0f\u68c0\u6d4b<\/h4>\n<pre><code class=\"python\">import objgraph\n\n# \u67e5\u770b\u6700\u5e38\u89c1\u7684\u5bf9\u8c61\nobjgraph.show_most_common_types()\n\n# \u67e5\u627e\u7279\u5b9a\u7c7b\u578b\u7684\u5bf9\u8c61\nobjgraph.count('Node')\n\n# \u7ed8\u5236\u5f15\u7528\u5173\u7cfb\u56fe\nobjgraph.show_backrefs([problematic_object], filename='backrefs.png')<\/code><\/pre>\n<h4>5.2 \u5185\u5b58\u4f7f\u7528\u5206\u6790<\/h4>\n<pre><code class=\"python\">import psutil\nimport os\n\n# \u83b7\u53d6\u5f53\u524d\u8fdb\u7a0b\nprocess = psutil.Process(os.getpid())\n\n# \u67e5\u770b\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\nprint(f&quot;RSS: {process.memory_info().rss \/ 1024 \/ 1024:.2f} MB&quot;)\nprint(f&quot;VMS: {process.memory_info().vms \/ 1024 \/ 1024:.2f} MB&quot;)<\/code><\/pre>\n<h4>5.3 \u5185\u5b58\u4f18\u5316\u6280\u5de7<\/h4>\n<h5>5.3.1 \u4f7f\u7528\u751f\u6210\u5668<\/h5>\n<pre><code class=\"python\"># \u4e0d\u597d\u7684\u505a\u6cd5\uff1a\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\n\ndef load_all_data():\n    data = []\n    for i in range(1000000):\n        data.append(i)\n    return data\n\n# \u597d\u7684\u505a\u6cd5\uff1a\u4f7f\u7528\u751f\u6210\u5668\ndef generate_data():\n    for i in range(1000000):\n        yield i<\/code><\/pre>\n<h5>5.3.2 \u907f\u514d\u5faa\u73af\u5f15\u7528<\/h5>\n<pre><code class=\"python\"># \u4e0d\u597d\u7684\u505a\u6cd5\uff1a\u5faa\u73af\u5f15\u7528\nclass Node:\n    def __init__(self):\n        self.children = []\n        self.parent = None\n    \n    def add_child(self, child):\n        self.children.append(child)\n        child.parent = self\n\n# \u597d\u7684\u505a\u6cd5\uff1a\u4f7f\u7528\u5f31\u5f15\u7528\nimport weakref\n\nclass Node:\n    def __init__(self):\n        self.children = []\n        self.parent = None\n    \n    def add_child(self, child):\n        self.children.append(child)\n        child.parent = weakref.ref(self)<\/code><\/pre>\n<h5>5.3.3 \u53ca\u65f6\u91ca\u653e\u8d44\u6e90<\/h5>\n<pre><code class=\"python\"># \u4e0d\u597d\u7684\u505a\u6cd5\uff1a\u8d44\u6e90\u4e0d\u53ca\u65f6\u91ca\u653e\nfile = open('large_file.txt', 'r')\ndata = file.read()\n# \u5904\u7406\u6570\u636e...\n# \u5fd8\u8bb0\u5173\u95ed\u6587\u4ef6\n\n# \u597d\u7684\u505a\u6cd5\uff1a\u4f7f\u7528with\u8bed\u53e5\nwith open('large_file.txt', 'r') as file:\n    data = file.read()\n    # \u5904\u7406\u6570\u636e...\n# \u6587\u4ef6\u81ea\u52a8\u5173\u95ed<\/code><\/pre>\n<h3>6. \u5185\u5b58\u7ba1\u7406\u7684\u9ad8\u7ea7\u8bdd\u9898<\/h3>\n<h4>6.1 \u5f31\u5f15\u7528<\/h4>\n<p><strong>\u5f31\u5f15\u7528<\/strong>\uff08Weak Reference\uff09\u5141\u8bb8\u5f15\u7528\u5bf9\u8c61\u800c\u4e0d\u589e\u52a0\u5176\u5f15\u7528\u8ba1\u6570\uff0c\u9002\u7528\u4e8e\u7f13\u5b58\u3001\u89c2\u5bdf\u8005\u6a21\u5f0f\u7b49\u573a\u666f\uff1a<\/p>\n<pre><code class=\"python\">import weakref\n\nclass MyClass:\n    def __init__(self, name):\n        self.name = name\n    \n    def __del__(self):\n        print(f&quot;{self.name} is being deleted&quot;)\n\n# \u521b\u5efa\u5bf9\u8c61\nobj = MyClass(&quot;Test&quot;)\n\n# \u521b\u5efa\u5f31\u5f15\u7528\nweak_ref = weakref.ref(obj)\nprint(weak_ref())  # \u8f93\u51fa: &lt;__main__.MyClass object at 0x...&gt;\n\n# \u5220\u9664\u5bf9\u8c61\ndel obj\nprint(weak_ref())  # \u8f93\u51fa: None<\/code><\/pre>\n<h4>6.2 \u5185\u5b58\u89c6\u56fe<\/h4>\n<p><strong>\u5185\u5b58\u89c6\u56fe<\/strong>\uff08Memory View\uff09\u5141\u8bb8\u5728\u4e0d\u590d\u5236\u6570\u636e\u7684\u60c5\u51b5\u4e0b\u8bbf\u95ee\u5bf9\u8c61\u7684\u5185\u90e8\u7f13\u51b2\u533a\uff1a<\/p>\n<pre><code class=\"python\"># \u521b\u5efa\u5b57\u8282\u6570\u7ec4\ndata = bytearray(b'Hello, World!')\n\n# \u521b\u5efa\u5185\u5b58\u89c6\u56fe\nmv = memoryview(data)\n\n# \u4fee\u6539\u5185\u5b58\u89c6\u56fe\uff0c\u4f1a\u76f4\u63a5\u4fee\u6539\u539f\u59cb\u6570\u636e\nmv[0] = ord('h')\nprint(data)  # \u8f93\u51fa: bytearray(b'hello, World!')<\/code><\/pre>\n<h4>6.3 \u7f13\u51b2\u534f\u8bae<\/h4>\n<p><strong>\u7f13\u51b2\u534f\u8bae<\/strong>\uff08Buffer Protocol\uff09\u5141\u8bb8\u5bf9\u8c61\u66b4\u9732\u5176\u5185\u90e8\u7f13\u51b2\u533a\uff0c\u4f9b\u5176\u4ed6\u5bf9\u8c61\u76f4\u63a5\u8bbf\u95ee\uff0c\u907f\u514d\u6570\u636e\u590d\u5236\uff1a<\/p>\n<ul>\n<li>\u5b9e\u73b0\u4e86<code>__buffer__<\/code>\u65b9\u6cd5\u7684\u5bf9\u8c61\u652f\u6301\u7f13\u51b2\u534f\u8bae<\/li>\n<li>\u5982<code>bytes<\/code>\u3001<code>bytearray<\/code>\u3001<code>array.array<\/code>\u7b49<\/li>\n<\/ul>\n<h4>6.4 \u5185\u5b58\u6620\u5c04<\/h4>\n<p><strong>\u5185\u5b58\u6620\u5c04<\/strong>\uff08Memory Mapping\uff09\u5141\u8bb8\u5c06\u6587\u4ef6\u76f4\u63a5\u6620\u5c04\u5230\u5185\u5b58\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"python\">import mmap\n\nwith open('large_file.txt', 'r+b') as f:\n    # \u521b\u5efa\u5185\u5b58\u6620\u5c04\n    mm = mmap.mmap(f.fileno(), length=0, access=mmap.ACCESS_WRITE)\n    \n    # \u76f4\u63a5\u64cd\u4f5c\u5185\u5b58\n    mm[0:5] = b'Hello'\n    \n    # \u5173\u95ed\u5185\u5b58\u6620\u5c04\n    mm.close()<\/code><\/pre>\n<h3>7. \u5185\u5b58\u6cc4\u6f0f\u7684\u539f\u56e0\u4e0e\u89e3\u51b3\u65b9\u6848<\/h3>\n<h4>7.1 \u5e38\u89c1\u7684\u5185\u5b58\u6cc4\u6f0f\u539f\u56e0<\/h4>\n<ol>\n<li><strong>\u5faa\u73af\u5f15\u7528<\/strong>\uff1a\u5bf9\u8c61\u4e4b\u95f4\u4e92\u76f8\u5f15\u7528<\/li>\n<li><strong>\u5168\u5c40\u53d8\u91cf<\/strong>\uff1a\u672a\u53ca\u65f6\u6e05\u7406\u7684\u5168\u5c40\u53d8\u91cf<\/li>\n<li><strong>\u7f13\u5b58<\/strong>\uff1a\u65e0\u9650\u589e\u957f\u7684\u7f13\u5b58<\/li>\n<li><strong>\u95ed\u5305<\/strong>\uff1a\u95ed\u5305\u4e2d\u5f15\u7528\u7684\u53d8\u91cf<\/li>\n<li><strong>\u7b2c\u4e09\u65b9\u5e93<\/strong>\uff1a\u4f7f\u7528\u4e0d\u5f53\u7684\u7b2c\u4e09\u65b9\u5e93<\/li>\n<\/ol>\n<h4>7.2 \u5185\u5b58\u6cc4\u6f0f\u7684\u68c0\u6d4b\u5de5\u5177<\/h4>\n<ul>\n<li><strong>objgraph<\/strong>\uff1a\u67e5\u770b\u5bf9\u8c61\u5f15\u7528\u5173\u7cfb<\/li>\n<li><strong>memory_profiler<\/strong>\uff1a\u9010\u884c\u5206\u6790\u5185\u5b58\u4f7f\u7528<\/li>\n<li><strong>tracemalloc<\/strong>\uff1a\u8ddf\u8e2a\u5185\u5b58\u5206\u914d<\/li>\n<li><strong>psutil<\/strong>\uff1a\u76d1\u63a7\u8fdb\u7a0b\u5185\u5b58\u4f7f\u7528<\/li>\n<\/ul>\n<h4>7.3 \u5185\u5b58\u6cc4\u6f0f\u7684\u89e3\u51b3\u65b9\u6848<\/h4>\n<ol>\n<li><strong>\u4f7f\u7528\u5f31\u5f15\u7528<\/strong>\uff1a\u907f\u514d\u5faa\u73af\u5f15\u7528<\/li>\n<li><strong>\u53ca\u65f6\u6e05\u7406<\/strong>\uff1a\u4f7f\u7528<code>del<\/code>\u5220\u9664\u4e0d\u9700\u8981\u7684\u5bf9\u8c61<\/li>\n<li><strong>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/strong>\uff1a\u81ea\u52a8\u91ca\u653e\u8d44\u6e90<\/li>\n<li><strong>\u8bbe\u7f6e\u7f13\u5b58\u5927\u5c0f\u9650\u5236<\/strong>\uff1a\u907f\u514d\u7f13\u5b58\u65e0\u9650\u589e\u957f<\/li>\n<li><strong>\u5b9a\u671f\u68c0\u6d4b<\/strong>\uff1a\u4f7f\u7528\u5185\u5b58\u5206\u6790\u5de5\u5177\u5b9a\u671f\u68c0\u67e5<\/li>\n<\/ol>\n<h3>8. \u6027\u80fd\u4f18\u5316\u6848\u4f8b<\/h3>\n<h4>8.1 \u5217\u8868\u4e0e\u751f\u6210\u5668\u5bf9\u6bd4<\/h4>\n<pre><code class=\"python\">import sys\n\n# \u5217\u8868\u5360\u7528\u7684\u5185\u5b58\na = [i for i in range(1000000)]\nprint(f&quot;List size: {sys.getsizeof(a) \/ 1024 \/ 1024:.2f} MB&quot;)\n\n# \u751f\u6210\u5668\u5360\u7528\u7684\u5185\u5b58\nb = (i for i in range(1000000))\nprint(f&quot;Generator size: {sys.getsizeof(b) \/ 1024 \/ 1024:.2f} MB&quot;)<\/code><\/pre>\n<h4>8.2 \u5b57\u5178\u4f18\u5316<\/h4>\n<pre><code class=\"python\"># \u4e0d\u597d\u7684\u505a\u6cd5\uff1a\u4f7f\u7528\u666e\u901a\u5b57\u5178\nlarge_dict = {}\nfor i in range(1000000):\n    large_dict[i] = i\n\n# \u597d\u7684\u505a\u6cd5\uff1a\u4f7f\u7528__slots__\u6216dataclasses\nfrom dataclasses import dataclass\n\n@dataclass\nclass Data:\n    value: int\n\n# \u6216\u8005\u4f7f\u7528__slots__\nclass DataWithSlots:\n    __slots__ = ['value']\n    def __init__(self, value):\n        self.value = value<\/code><\/pre>\n<h4>8.3 \u5b57\u7b26\u4e32\u62fc\u63a5\u4f18\u5316<\/h4>\n<pre><code class=\"python\"># \u4e0d\u597d\u7684\u505a\u6cd5\uff1a\u4f7f\u7528+\u62fc\u63a5\u5b57\u7b26\u4e32\nresult = &quot;&quot;\nfor i in range(10000):\n    result += str(i)\n\n# \u597d\u7684\u505a\u6cd5\uff1a\u4f7f\u7528join\nparts = []\nfor i in range(10000):\n    parts.append(str(i))\nresult = &quot;&quot;.join(parts)<\/code><\/pre>\n<h3>9. Python\u5185\u5b58\u7ba1\u7406\u7684\u672a\u6765\u53d1\u5c55<\/h3>\n<h4>9.1 PyPy\u7684\u5185\u5b58\u7ba1\u7406<\/h4>\n<p>PyPy\u4f7f\u7528<strong>\u5206\u4ee3\u5783\u573e\u56de\u6536<\/strong>\u548c<strong>\u5373\u65f6\u7f16\u8bd1<\/strong>\uff0c\u5185\u5b58\u7ba1\u7406\u6548\u7387\u66f4\u9ad8\uff1a<\/p>\n<ul>\n<li>\u66f4\u9ad8\u6548\u7684\u5783\u573e\u56de\u6536\u7b97\u6cd5<\/li>\n<li>\u51cf\u5c11\u5185\u5b58\u4f7f\u7528<\/li>\n<li>\u63d0\u9ad8\u6267\u884c\u901f\u5ea6<\/li>\n<\/ul>\n<h4>9.2 Python 3.10+\u7684\u5185\u5b58\u4f18\u5316<\/h4>\n<ul>\n<li><strong>PEP 634<\/strong>\uff1a\u7ed3\u6784\u5316\u6a21\u5f0f\u5339\u914d\uff0c\u51cf\u5c11\u5185\u5b58\u4f7f\u7528<\/li>\n<li><strong>PEP 644<\/strong>\uff1a\u5220\u9664Py_UNICODE\u7f16\u7801\uff0c\u7edf\u4e00\u5b57\u7b26\u4e32\u8868\u793a<\/li>\n<li><strong>PEP 654<\/strong>\uff1a\u5f02\u5e38\u7ec4\u548cexcept*\uff0c\u6539\u8fdb\u5f02\u5e38\u5904\u7406\u7684\u5185\u5b58\u4f7f\u7528<\/li>\n<\/ul>\n<h4>9.3 \u5185\u5b58\u7ba1\u7406\u7684\u7814\u7a76\u65b9\u5411<\/h4>\n<ul>\n<li><strong>\u5e76\u53d1\u5783\u573e\u56de\u6536<\/strong>\uff1a\u51cf\u5c11\u5783\u573e\u56de\u6536\u5bf9\u7a0b\u5e8f\u6267\u884c\u7684\u5f71\u54cd<\/li>\n<li><strong>\u81ea\u52a8\u5185\u5b58\u7ba1\u7406\u4f18\u5316<\/strong>\uff1a\u6839\u636e\u7a0b\u5e8f\u884c\u4e3a\u81ea\u52a8\u8c03\u6574\u5185\u5b58\u7ba1\u7406\u7b56\u7565<\/li>\n<li><strong>\u5185\u5b58\u4f7f\u7528\u9884\u6d4b<\/strong>\uff1a\u9884\u6d4b\u7a0b\u5e8f\u7684\u5185\u5b58\u4f7f\u7528\u6a21\u5f0f\uff0c\u63d0\u524d\u5206\u914d\u5185\u5b58<\/li>\n<\/ul>\n<h3>10. \u603b\u7ed3<\/h3>\n<p>Python\u7684\u5185\u5b58\u7ba1\u7406\u662f\u4e00\u4e2a\u590d\u6742\u800c\u7cbe\u5de7\u7684\u7cfb\u7edf\uff0c\u7ed3\u5408\u4e86\u5f15\u7528\u8ba1\u6570\u3001\u6807\u8bb0-\u6e05\u9664\u548c\u5206\u4ee3\u56de\u6536\u7b49\u591a\u79cd\u673a\u5236\u3002\u901a\u8fc7\u7406\u89e3Python\u7684\u5185\u5b58\u7ba1\u7406\u539f\u7406\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\uff1a<\/p>\n<ol>\n<li><strong>\u7f16\u5199\u66f4\u9ad8\u6548\u7684\u4ee3\u7801<\/strong>\uff1a\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u6027\u80fd<\/li>\n<li><strong>\u907f\u514d\u5185\u5b58\u6cc4\u6f0f<\/strong>\uff1a\u53ca\u65f6\u91ca\u653e\u4e0d\u9700\u8981\u7684\u8d44\u6e90<\/li>\n<li><strong>\u4f18\u5316\u5185\u5b58\u4f7f\u7528<\/strong>\uff1a\u6839\u636e\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5<\/li>\n<li><strong>\u8c03\u8bd5\u5185\u5b58\u95ee\u9898<\/strong>\uff1a\u4f7f\u7528\u5185\u5b58\u5206\u6790\u5de5\u5177\u5b9a\u4f4d\u548c\u89e3\u51b3\u5185\u5b58\u95ee\u9898<\/li>\n<\/ol>\n<h4>\u5173\u952e\u8981\u70b9<\/h4>\n<ul>\n<li><strong>\u5f15\u7528\u8ba1\u6570<\/strong>\uff1aPython\u7684\u57fa\u672c\u5783\u573e\u56de\u6536\u673a\u5236\uff0c\u5904\u7406\u5927\u591a\u6570\u5185\u5b58\u56de\u6536<\/li>\n<li><strong>\u6807\u8bb0-\u6e05\u9664<\/strong>\uff1a\u5904\u7406\u5faa\u73af\u5f15\u7528\u7684\u4e3b\u8981\u7b97\u6cd5<\/li>\n<li><strong>\u5206\u4ee3\u56de\u6536<\/strong>\uff1a\u63d0\u9ad8\u5783\u573e\u56de\u6536\u6548\u7387\u7684\u7b56\u7565<\/li>\n<li><strong>\u5185\u5b58\u6c60<\/strong>\uff1a\u4f18\u5316\u5c0f\u5bf9\u8c61\u5206\u914d\uff0c\u51cf\u5c11\u5185\u5b58\u788e\u7247<\/li>\n<li><strong>\u5f31\u5f15\u7528<\/strong>\uff1a\u907f\u514d\u5faa\u73af\u5f15\u7528\u7684\u6709\u6548\u5de5\u5177<\/li>\n<li><strong>\u5185\u5b58\u5206\u6790<\/strong>\uff1a\u4f7f\u7528\u5de5\u5177\u68c0\u6d4b\u548c\u89e3\u51b3\u5185\u5b58\u95ee\u9898<\/li>\n<\/ul>\n<h4>\u6700\u4f73\u5b9e\u8df5<\/h4>\n<ul>\n<li><strong>\u4f7f\u7528\u751f\u6210\u5668<\/strong>\uff1a\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u51cf\u5c11\u5185\u5b58\u4f7f\u7528<\/li>\n<li><strong>\u907f\u514d\u5faa\u73af\u5f15\u7528<\/strong>\uff1a\u4f7f\u7528\u5f31\u5f15\u7528\u6216\u5408\u7406\u8bbe\u8ba1\u5bf9\u8c61\u5173\u7cfb<\/li>\n<li><strong>\u53ca\u65f6\u91ca\u653e\u8d44\u6e90<\/strong>\uff1a\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u548c<code>del<\/code>\u8bed\u53e5<\/li>\n<li><strong>\u4f18\u5316\u6570\u636e\u7ed3\u6784<\/strong>\uff1a\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u51cf\u5c11\u5185\u5b58\u5360\u7528<\/li>\n<li><strong>\u5b9a\u671f\u68c0\u6d4b<\/strong>\uff1a\u4f7f\u7528\u5185\u5b58\u5206\u6790\u5de5\u5177\u76d1\u63a7\u5185\u5b58\u4f7f\u7528\u60c5\u51b5<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u638c\u63e1Python\u7684\u5185\u5b58\u7ba1\u7406\u77e5\u8bc6\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u7f16\u5199\u51fa\u66f4\u9ad8\u6548\u3001\u66f4\u53ef\u9760\u7684Python\u7a0b\u5e8f\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u6216\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u670d\u52a1\u65f6\uff0c\u826f\u597d\u7684\u5185\u5b58\u7ba1\u7406\u7b56\u7565\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047585279\">https:\/\/segmentfault.com\/a\/1190000047585279<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u7684\u5185\u5b58\u7ba1\u7406\u4e0e\u5783\u573e\u56de\u6536\u7b97\u6cd5\u5206\u6790 1. \u5185\u5b58\u7ba1\u7406\u6982\u8ff0 1.1 Python\u5185\u5b58\u7ba1\u7406\u7684\u5c42\u6b21 Pytho [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021224","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021224"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021224\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}