{"id":1156217,"date":"2025-01-13T18:13:14","date_gmt":"2025-01-13T10:13:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1156217.html"},"modified":"2025-01-13T18:13:17","modified_gmt":"2025-01-13T10:13:17","slug":"%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8bpython%e5%86%85%e5%ad%98%e4%bd%bf%e7%94%a8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1156217.html","title":{"rendered":"\u5982\u4f55\u67e5\u770bPython\u5185\u5b58\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25194715\/d4f3beb7-5833-47f3-ba3c-8611ff714ef4.webp\" alt=\"\u5982\u4f55\u67e5\u770bPython\u5185\u5b58\u4f7f\u7528\" \/><\/p>\n<p><p> <strong>\u67e5\u770bPython\u5185\u5b58\u4f7f\u7528\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5404\u79cd\u5de5\u5177\u548c\u5e93\uff0c\u5982<code>psutil<\/code>\u3001<code>memory_profiler<\/code>\u3001<code>objgraph<\/code>\u7b49\u3001\u76d1\u63a7\u8fdb\u7a0b\u5185\u5b58\u3001\u5206\u6790\u5bf9\u8c61\u5927\u5c0f\u3001\u4f7f\u7528\u5185\u7f6e\u7684<code>sys<\/code>\u6a21\u5757\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528<code>psutil<\/code>\u5e93\u8fdb\u884c\u8fdb\u7a0b\u5185\u5b58\u76d1\u63a7\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u5f3a\u5927\u7684\u65b9\u6cd5\u3002<code>psutil<\/code>\u5e93\u63d0\u4f9b\u4e86\u5bf9\u7cfb\u7edf\u548c\u8fdb\u7a0b\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5c24\u5176\u9002\u5408\u4e8e\u5b9e\u65f6\u76d1\u63a7\u7a0b\u5e8f\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528<code>psutil<\/code>\u5e93\u8fdb\u884c\u5185\u5b58\u4f7f\u7528\u76d1\u63a7\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528psutil\u5e93\u76d1\u63a7\u8fdb\u7a0b\u5185\u5b58<\/h3>\n<\/p>\n<p><p><code>psutil<\/code>\uff08Python system and process utilities\uff09\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u5bf9\u7cfb\u7edf\u548c\u8fdb\u7a0b\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u76d1\u63a7\u5185\u5b58\uff0c\u8fd8\u53ef\u4ee5\u76d1\u63a7CPU\u3001\u78c1\u76d8\u3001\u7f51\u7edc\u7b49\u8d44\u6e90\u7684\u4f7f\u7528\u60c5\u51b5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>psutil<\/code>\u5e93\u6765\u76d1\u63a7Python\u8fdb\u7a0b\u5185\u5b58\u4f7f\u7528\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5psutil\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>psutil<\/code>\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install psutil<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u83b7\u53d6\u5f53\u524d\u8fdb\u7a0b\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u6765\u83b7\u53d6\u5f53\u524dPython\u8fdb\u7a0b\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import psutil<\/p>\n<p>import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0b\u7684PID<\/strong><\/h2>\n<p>pid = os.getpid()<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0b\u5bf9\u8c61<\/strong><\/h2>\n<p>current_process = psutil.Process(pid)<\/p>\n<h2><strong>\u83b7\u53d6\u5185\u5b58\u4fe1\u606f<\/strong><\/h2>\n<p>memory_info = current_process.memory_info()<\/p>\n<p>print(f&quot;RSS: {memory_info.rss \/ (1024  2):.2f} MB&quot;)<\/p>\n<p>print(f&quot;VMS: {memory_info.vms \/ (1024  2):.2f} MB&quot;)<\/p>\n<p>print(f&quot;Shared: {memory_info.shared \/ (1024  2):.2f} MB&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>psutil.Process<\/code>\u5bf9\u8c61\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0b\u7684\u5185\u5b58\u4fe1\u606f\u3002<code>memory_info()<\/code>\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u547d\u540d\u5143\u7ec4\uff0c\u5305\u542bRSS\uff08Resident Set Size\uff09\u3001VMS\uff08Virtual Memory Size\uff09\u7b49\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528memory_profiler\u5e93\u5206\u6790\u51fd\u6570\u5185\u5b58\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><code>memory_profiler<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u76d1\u63a7Python\u4ee3\u7801\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u7684\u5e93\uff0c\u7279\u522b\u9002\u5408\u5206\u6790\u51fd\u6570\u6216\u4ee3\u7801\u5757\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002\u5b83\u53ef\u4ee5\u901a\u8fc7\u88c5\u9970\u5668\u6216\u8005\u547d\u4ee4\u884c\u5de5\u5177\u6765\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5memory_profiler\u5e93<\/h4>\n<\/p>\n<p><p>\u540c\u6837\u5730\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u6765\u5b89\u88c5<code>memory_profiler<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install memory_profiler<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u88c5\u9970\u5668\u5206\u6790\u51fd\u6570\u5185\u5b58\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>@profile<\/code>\u88c5\u9970\u5668\u6765\u5206\u6790\u7279\u5b9a\u51fd\u6570\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from memory_profiler import profile<\/p>\n<p>@profile<\/p>\n<p>def my_function():<\/p>\n<p>    a = [1] * (10  6)<\/p>\n<p>    b = [2] * (2 * 10  7)<\/p>\n<p>    del b<\/p>\n<p>    return a<\/p>\n<p>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;:<\/p>\n<p>    my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8fd9\u4e2a\u811a\u672c\u65f6\uff0c\u4f1a\u6253\u5370\u51fa<code>my_function<\/code>\u51fd\u6570\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528objgraph\u5e93\u5206\u6790\u5bf9\u8c61\u5185\u5b58\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><code>objgraph<\/code>\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5206\u6790Python\u5bf9\u8c61\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff0c\u7279\u522b\u662f\u5f53\u6000\u7591\u5b58\u5728\u5185\u5b58\u6cc4\u6f0f\u65f6\u975e\u5e38\u6709\u7528\u3002\u5b83\u53ef\u4ee5\u751f\u6210\u5bf9\u8c61\u5173\u7cfb\u56fe\uff0c\u663e\u793a\u5bf9\u8c61\u4e4b\u95f4\u7684\u5f15\u7528\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5objgraph\u5e93<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528pip\u5b89\u88c5<code>objgraph<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install objgraph<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5206\u6790\u5bf9\u8c61\u5f15\u7528\u5173\u7cfb<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>objgraph<\/code>\u5e93\u6765\u5206\u6790\u5bf9\u8c61\u7684\u5f15\u7528\u5173\u7cfb\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import objgraph<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e9b\u5bf9\u8c61<\/strong><\/h2>\n<p>a = [1, 2, 3]<\/p>\n<p>b = [a, a]<\/p>\n<p>c = {&#39;a&#39;: a, &#39;b&#39;: b}<\/p>\n<h2><strong>\u6253\u5370\u6700\u5e38\u89c1\u7684\u5bf9\u8c61\u7c7b\u578b<\/strong><\/h2>\n<p>objgraph.show_most_common_types()<\/p>\n<h2><strong>\u6253\u5370\u67d0\u4e2a\u7c7b\u578b\u7684\u5bf9\u8c61\u5f15\u7528\u5173\u7cfb<\/strong><\/h2>\n<p>objgraph.show_refs([a], filename=&#39;refs.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528sys\u6a21\u5757\u83b7\u53d6\u5bf9\u8c61\u5927\u5c0f<\/h3>\n<\/p>\n<p><p><code>sys<\/code>\u6a21\u5757\u7684<code>getsizeof<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u5355\u4e2a\u5bf9\u8c61\u7684\u5185\u5b58\u5927\u5c0f\u3002\u867d\u7136\u5b83\u4e0d\u80fd\u76d1\u63a7\u6574\u4e2a\u8fdb\u7a0b\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff0c\u4f46\u5728\u9700\u8981\u77e5\u9053\u67d0\u4e2a\u7279\u5b9a\u5bf9\u8c61\u7684\u5185\u5b58\u5360\u7528\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>a = [1, 2, 3]<\/p>\n<p>print(f&quot;Size of list a: {sys.getsizeof(a)} bytes&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u5de5\u5177\u8fdb\u884c\u5168\u9762\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u5168\u9762\u5730\u4e86\u89e3Python\u7a0b\u5e8f\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff0c\u901a\u5e38\u9700\u8981\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u5de5\u5177\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>psutil<\/code>\u76d1\u63a7\u6574\u4f53\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff0c<code>memory_profiler<\/code>\u5206\u6790\u51fd\u6570\u5185\u5b58\u4f7f\u7528\uff0c<code>objgraph<\/code>\u67e5\u627e\u5185\u5b58\u6cc4\u6f0f\uff0c<code>sys.getsizeof<\/code>\u83b7\u53d6\u5355\u4e2a\u5bf9\u8c61\u7684\u5185\u5b58\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><h4>1. \u76d1\u63a7\u6574\u4f53\u5185\u5b58\u4f7f\u7528\u60c5\u51b5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>psutil<\/code>\u5e93\u76d1\u63a7Python\u8fdb\u7a0b\u7684\u6574\u4f53\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u5185\u5b58\u4f7f\u7528\u9ad8\u5cf0\u548c\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5b9a\u671f\u8bb0\u5f55\u8fdb\u7a0b\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import psutil<\/p>\n<p>import os<\/p>\n<p>import time<\/p>\n<p>def log_memory_usage(interval=1, duration=60):<\/p>\n<p>    pid = os.getpid()<\/p>\n<p>    process = psutil.Process(pid)<\/p>\n<p>    for _ in range(duration):<\/p>\n<p>        memory_info = process.memory_info()<\/p>\n<p>        rss = memory_info.rss \/ (1024  2)<\/p>\n<p>        vms = memory_info.vms \/ (1024  2)<\/p>\n<p>        print(f&quot;RSS: {rss:.2f} MB, VMS: {vms:.2f} MB&quot;)<\/p>\n<p>        time.sleep(interval)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    log_memory_usage()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5206\u6790\u51fd\u6570\u5185\u5b58\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>memory_profiler<\/code>\u5e93\u5206\u6790\u7279\u5b9a\u51fd\u6570\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u548c\u4f18\u5316\u5185\u5b58\u5bc6\u96c6\u578b\u4ee3\u7801\u6bb5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>@profile<\/code>\u88c5\u9970\u5668\u5206\u6790\u5173\u952e\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from memory_profiler import profile<\/p>\n<p>@profile<\/p>\n<p>def memory_intensive_function():<\/p>\n<p>    a = [i for i in range(1000000)]<\/p>\n<p>    b = {i: i for i in range(1000000)}<\/p>\n<p>    return a, b<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    memory_intensive_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u67e5\u627e\u5185\u5b58\u6cc4\u6f0f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>objgraph<\/code>\u5e93\u67e5\u627e\u5185\u5b58\u6cc4\u6f0f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u672a\u6b63\u786e\u91ca\u653e\u7684\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u751f\u6210\u5bf9\u8c61\u5173\u7cfb\u56fe\uff0c\u67e5\u770b\u54ea\u4e9b\u5bf9\u8c61\u6ca1\u6709\u88ab\u6b63\u786e\u9500\u6bc1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import objgraph<\/p>\n<p>def create_leak():<\/p>\n<p>    a = [1] * (10  6)<\/p>\n<p>    b = [a]<\/p>\n<p>    return b<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    create_leak()<\/p>\n<p>    objgraph.show_most_common_types()<\/p>\n<p>    objgraph.show_refs([b], filename=&#39;leak_refs.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u83b7\u53d6\u5355\u4e2a\u5bf9\u8c61\u5185\u5b58\u5927\u5c0f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>sys<\/code>\u6a21\u5757\u7684<code>getsizeof<\/code>\u51fd\u6570\u83b7\u53d6\u5355\u4e2a\u5bf9\u8c61\u7684\u5185\u5b58\u5927\u5c0f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u5360\u7528\u5185\u5b58\u8f83\u5927\u7684\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>a = [i for i in range(1000000)]<\/p>\n<p>print(f&quot;Size of list a: {sys.getsizeof(a)} bytes&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u76d1\u63a7\u548c\u5206\u6790\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff0c\u4f18\u5316Python\u7a0b\u5e8f\u7684\u5185\u5b58\u4f7f\u7528\u4e5f\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u7684\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<\/p>\n<p><h4>1. \u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5927\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u548c\u4f7f\u7528\u5927\u5bf9\u8c61\u662f\u5bfc\u81f4\u5185\u5b58\u4f7f\u7528\u9ad8\u5cf0\u7684\u5e38\u89c1\u539f\u56e0\u3002\u5c3d\u91cf\u907f\u514d\u5728\u5185\u5b58\u4e2d\u540c\u65f6\u5b58\u5728\u591a\u4e2a\u5927\u5bf9\u8c61\uff0c\u53ef\u4ee5\u901a\u8fc7\u5206\u6279\u5904\u7406\u6570\u636e\u3001\u4f7f\u7528\u751f\u6210\u5668\u7b49\u65b9\u6cd5\u6765\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def process_data_in_batches(data, batch_size):<\/p>\n<p>    for i in range(0, len(data), batch_size):<\/p>\n<p>        batch = data[i:i + batch_size]<\/p>\n<p>        # \u5904\u7406\u6279\u6b21\u6570\u636e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u751f\u6210\u5668<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u60f0\u6027\u6c42\u503c\u7684\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u751f\u6210\u6570\u636e\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u5c06\u6240\u6709\u6570\u636e\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002\u4f7f\u7528\u751f\u6210\u5668\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def data_generator(size):<\/p>\n<p>    for i in range(size):<\/p>\n<p>        yield i<\/p>\n<p>for item in data_generator(1000000):<\/p>\n<p>    # \u5904\u7406\u6bcf\u4e2a\u6570\u636e\u9879<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u91ca\u653e\u4e0d\u518d\u9700\u8981\u7684\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u53ca\u65f6\u91ca\u653e\u4e0d\u518d\u9700\u8981\u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u901a\u8fc7\u663e\u5f0f\u8c03\u7528<code>del<\/code>\u8bed\u53e5\u6765\u5220\u9664\u5bf9\u8c61\u5f15\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = [i for i in range(1000000)]<\/p>\n<h2><strong>\u4f7f\u7528a<\/strong><\/h2>\n<p>del a  # \u91ca\u653ea<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u4f7f\u7528\u5185\u5b58\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>array<\/code>\u6a21\u5757\u6216<code>numpy<\/code>\u5e93\u6765\u5b58\u50a8\u5927\u89c4\u6a21\u6570\u503c\u6570\u636e\uff0c\u53ef\u4ee5\u6bd4\u4f7f\u7528\u5217\u8868\u66f4\u9ad8\u6548\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import array<\/p>\n<p>a = array.array(&#39;i&#39;, range(1000000))<\/p>\n<p>print(f&quot;Size of array a: {sys.getsizeof(a)} bytes&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u907f\u514d\u5faa\u73af\u5f15\u7528<\/h4>\n<\/p>\n<p><p>\u5faa\u73af\u5f15\u7528\u662f\u6307\u5bf9\u8c61\u4e4b\u95f4\u76f8\u4e92\u5f15\u7528\uff0c\u5bfc\u81f4\u5783\u573e\u56de\u6536\u5668\u65e0\u6cd5\u6b63\u786e\u56de\u6536\u5185\u5b58\u3002\u53ef\u4ee5\u901a\u8fc7\u663e\u5f0f\u6253\u7834\u5faa\u73af\u5f15\u7528\u6765\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Node:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<p>        self.next = None<\/p>\n<p>a = Node(1)<\/p>\n<p>b = Node(2)<\/p>\n<p>a.next = b<\/p>\n<p>b.next = a  # \u5faa\u73af\u5f15\u7528<\/p>\n<h2><strong>\u663e\u5f0f\u6253\u7834\u5faa\u73af\u5f15\u7528<\/strong><\/h2>\n<p>a.next = None<\/p>\n<p>b.next = None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u67e5\u770bPython\u5185\u5b58\u4f7f\u7528\u662f\u4f18\u5316\u7a0b\u5e8f\u6027\u80fd\u548c\u8bca\u65ad\u5185\u5b58\u95ee\u9898\u7684\u91cd\u8981\u73af\u8282\u3002\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528<code>psutil<\/code>\u3001<code>memory_profiler<\/code>\u3001<code>objgraph<\/code>\u3001<code>sys<\/code>\u6a21\u5757\u7b49\u5de5\u5177\uff0c\u53ef\u4ee5\u5168\u9762\u76d1\u63a7\u548c\u5206\u6790Python\u7a0b\u5e8f\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002<strong>\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u7684\u6700\u4f73\u5b9e\u8df5<\/strong>\uff0c\u5982\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5927\u5bf9\u8c61\u3001\u4f7f\u7528\u751f\u6210\u5668\u3001\u53ca\u65f6\u91ca\u653e\u5bf9\u8c61\u3001\u9009\u62e9\u5185\u5b58\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u3001\u907f\u514d\u5faa\u73af\u5f15\u7528\u7b49\uff0c\u53ef\u4ee5\u5e2e\u52a9\u663e\u8457\u51cf\u5c11\u5185\u5b58\u5360\u7528\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u5b9a\u671f\u76d1\u63a7\u548c\u5206\u6790\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff0c\u53ca\u65f6\u53d1\u73b0\u548c\u89e3\u51b3\u5185\u5b58\u95ee\u9898\u3002\u901a\u8fc7\u4e0d\u65ad\u4f18\u5316\u5185\u5b58\u4f7f\u7528\uff0c\u53ef\u4ee5\u63d0\u5347Python\u7a0b\u5e8f\u7684\u6548\u7387\u548c\u53ef\u9760\u6027\uff0c\u4e3a\u7528\u6237\u63d0\u4f9b\u66f4\u597d\u7684\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u76d1\u6d4bPython\u7a0b\u5e8f\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u76d1\u6d4bPython\u7a0b\u5e8f\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684<code>resource<\/code>\u6a21\u5757\uff0c\u6216\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982<code>psutil<\/code>\u548c<code>memory_profiler<\/code>\u3002<code>resource<\/code>\u6a21\u5757\u53ef\u4ee5\u63d0\u4f9b\u8fdb\u7a0b\u7684\u5185\u5b58\u9650\u5236\u548c\u4f7f\u7528\u60c5\u51b5\uff0c\u800c<code>psutil<\/code>\u5219\u53ef\u4ee5\u63d0\u4f9b\u66f4\u8be6\u7ec6\u7684\u7cfb\u7edf\u548c\u8fdb\u7a0b\u4fe1\u606f\uff0c\u5305\u62ec\u5185\u5b58\u4f7f\u7528\u7684\u5b9e\u65f6\u6570\u636e\u3002<code>memory_profiler<\/code>\u53ef\u4ee5\u5e2e\u52a9\u4f60\u9010\u884c\u5206\u6790\u5185\u5b58\u4f7f\u7528\uff0c\u627e\u5230\u6f5c\u5728\u7684\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u3002<\/p>\n<p><strong>Python\u5185\u5b58\u4f7f\u7528\u8fc7\u9ad8\u7684\u539f\u56e0\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5185\u5b58\u4f7f\u7528\u8fc7\u9ad8\u53ef\u80fd\u4e0e\u591a\u79cd\u56e0\u7d20\u6709\u5173\uff0c\u4f8b\u5982\u672a\u91ca\u653e\u7684\u5bf9\u8c61\u5f15\u7528\u3001\u5faa\u73af\u5f15\u7528\u3001\u4f7f\u7528\u4e0d\u5f53\u7684\u6570\u636e\u7ed3\u6784\uff08\u5982\u5217\u8868\u6216\u5b57\u5178\uff09\uff0c\u6216\u8005\u4e0d\u5fc5\u8981\u7684\u5168\u5c40\u53d8\u91cf\u3002\u4f7f\u7528\u5927\u6570\u636e\u96c6\u65f6\uff0c\u6570\u636e\u7684\u5b58\u50a8\u65b9\u5f0f\u4e5f\u4f1a\u5f71\u54cd\u5185\u5b58\u4f7f\u7528\u3002\u4e86\u89e3\u6570\u636e\u7684\u751f\u547d\u5468\u671f\u548c\u7ba1\u7406\u5185\u5b58\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u5206\u6790Python\u7684\u5185\u5b58\u4f7f\u7528\uff1f<\/strong><br \/>\u5206\u6790Python\u5185\u5b58\u4f7f\u7528\u7684\u5de5\u5177\u6709\u5f88\u591a\u3002<code>memory_profiler<\/code>\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u9009\u62e9\uff0c\u80fd\u591f\u9010\u884c\u5206\u6790\u4ee3\u7801\u7684\u5185\u5b58\u6d88\u8017\u3002<code>objgraph<\/code>\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8bc6\u522b\u5185\u5b58\u6cc4\u6f0f\u548c\u53ef\u7591\u7684\u5bf9\u8c61\u5f15\u7528\u3002<code>tracemalloc<\/code>\u662fPython\u81ea\u5e26\u7684\u5de5\u5177\uff0c\u80fd\u591f\u8ddf\u8e2a\u5185\u5b58\u5206\u914d\u3002\u7ed3\u5408\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u66f4\u5168\u9762\u5730\u4e86\u89e3\u7a0b\u5e8f\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff0c\u5e76\u8fdb\u884c\u4f18\u5316\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u67e5\u770bPython\u5185\u5b58\u4f7f\u7528\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5404\u79cd\u5de5\u5177\u548c\u5e93\uff0c\u5982psutil\u3001memory_profiler\u3001objgra [&hellip;]","protected":false},"author":3,"featured_media":1156222,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156217"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1156217"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156217\/revisions"}],"predecessor-version":[{"id":1156223,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156217\/revisions\/1156223"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1156222"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1156217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1156217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1156217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}