{"id":1003654,"date":"2024-12-27T10:19:09","date_gmt":"2024-12-27T02:19:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1003654.html"},"modified":"2024-12-27T10:19:12","modified_gmt":"2024-12-27T02:19:12","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%8d%b0%e5%bd%93%e5%89%8d%e7%bc%93%e5%ad%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1003654.html","title":{"rendered":"python\u5982\u4f55\u6253\u5370\u5f53\u524d\u7f13\u5b58"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080926\/8499c43b-1d92-4dc3-acce-ae79a883d77c.webp\" alt=\"python\u5982\u4f55\u6253\u5370\u5f53\u524d\u7f13\u5b58\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6253\u5370\u5f53\u524d\u7f13\u5b58\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u6a21\u5757\u3001\u67e5\u770b\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3001\u4ee5\u53ca\u5229\u7528\u5916\u90e8\u5e93\u7b49\u3002\u5177\u4f53\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528Python\u5185\u7f6e\u7684<code>sys<\/code>\u6a21\u5757\u67e5\u770b\u5185\u5b58\u3001\u5229\u7528<code>gc<\/code>\u6a21\u5757\u68c0\u67e5\u5bf9\u8c61\u3001\u4ee5\u53ca\u4f7f\u7528<code>pprint<\/code>\u6a21\u5757\u683c\u5f0f\u5316\u8f93\u51fa\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5229\u7528<code>sys<\/code>\u6a21\u5757\u67e5\u770b\u5185\u5b58\u7684\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u901a\u8fc7<code>sys<\/code>\u6a21\u5757\u53ef\u4ee5\u83b7\u53d6\u4e00\u4e9b\u5173\u4e8e\u5185\u5b58\u4f7f\u7528\u7684\u4fe1\u606f\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u53ef\u4ee5\u901a\u8fc7<code>sys.getsizeof()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u4e00\u4e2a\u5bf9\u8c61\u5360\u7528\u7684\u5185\u5b58\u5927\u5c0f\u3002\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u7684\u5927\u5c0f\uff0c\u56e0\u6b64\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3\u7a0b\u5e8f\u4e2d\u67d0\u4e9b\u53d8\u91cf\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5229\u7528<code>sys<\/code>\u6a21\u5757\u67e5\u770b\u5185\u5b58<\/h3>\n<\/p>\n<p><p><code>sys<\/code>\u6a21\u5757\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bbf\u95eePython\u89e3\u91ca\u5668\u548c\u8fd0\u884c\u65f6\u73af\u5883\u7684\u63a5\u53e3\u3002\u901a\u8fc7<code>sys.getsizeof()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u68c0\u67e5\u67d0\u4e2a\u5bf9\u8c61\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528<code>sys.getsizeof()<\/code><\/h4>\n<\/p>\n<p><p><code>sys.getsizeof()<\/code>\u51fd\u6570\u662f\u68c0\u67e5\u5185\u5b58\u4f7f\u7528\u7684\u4e00\u4e2a\u7b80\u5355\u65b9\u6cd5\u3002\u5b83\u8fd4\u56de\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u7684\u5927\u5c0f\uff08\u5355\u4f4d\u4e3a\u5b57\u8282\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>print(&quot;The size of the list is:&quot;, sys.getsizeof(my_list))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5bfc\u5165\u4e86<code>sys<\/code>\u6a21\u5757\uff0c\u5e76\u4f7f\u7528<code>sys.getsizeof()<\/code>\u6765\u83b7\u53d6<code>my_list<\/code>\u5bf9\u8c61\u7684\u5927\u5c0f\u3002\u8fd9\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3\u67d0\u4e9b\u6570\u636e\u7ed3\u6784\u5728\u5185\u5b58\u4e2d\u7684\u5360\u7528\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u68c0\u67e5\u5185\u5b58\u4e2d\u6240\u6709\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u60f3\u8981\u67e5\u770b\u7a0b\u5e8f\u4e2d\u6240\u6709\u5bf9\u8c61\u7684\u5185\u5b58\u5360\u7528\u60c5\u51b5\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528<code>gc<\/code>\u6a21\u5757\u3002<code>gc<\/code>\u6a21\u5757\u662fPython\u7684\u5783\u573e\u56de\u6536\u63a5\u53e3\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u83b7\u53d6\u5173\u4e8e\u5185\u5b58\u4f7f\u7528\u7684\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import gc<\/p>\n<p>import sys<\/p>\n<h2><strong>\u5f3a\u5236\u8fdb\u884c\u5783\u573e\u56de\u6536<\/strong><\/h2>\n<p>gc.collect()<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u5bf9\u8c61<\/strong><\/h2>\n<p>all_objects = gc.get_objects()<\/p>\n<h2><strong>\u8f93\u51fa\u6bcf\u4e2a\u5bf9\u8c61\u7684\u5185\u5b58\u5927\u5c0f<\/strong><\/h2>\n<p>for obj in all_objects:<\/p>\n<p>    print(f&quot;Object: {repr(obj)} Size: {sys.getsizeof(obj)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u901a\u8fc7<code>gc.get_objects()<\/code>\u83b7\u53d6\u6240\u6709\u6d3b\u8dc3\u5bf9\u8c61\uff0c\u5e76\u9010\u4e2a\u6253\u5370\u5176\u5185\u5b58\u5360\u7528\u60c5\u51b5\u3002\u8fd9\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\u67e5\u770b\u7a0b\u5e8f\u5185\u5b58\u4f7f\u7528\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>pprint<\/code>\u6a21\u5757\u683c\u5f0f\u5316\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u5728\u6253\u5370\u590d\u6742\u7684\u5bf9\u8c61\u6216\u6570\u636e\u7ed3\u6784\u65f6\uff0c<code>pprint<\/code>\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u4f7f\u5f97\u4fe1\u606f\u66f4\u6613\u4e8e\u9605\u8bfb\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5bfc\u5165<code>pprint<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>pprint<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>pprint.pprint()<\/code>\u51fd\u6570\u7528\u4e8e\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pprint<\/p>\n<p>data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;job&#39;: {&#39;title&#39;: &#39;Engineer&#39;, &#39;years&#39;: 5}}<\/p>\n<p>pprint.pprint(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pprint.pprint()<\/code>\u51fd\u6570\u683c\u5f0f\u5316\u6253\u5370\u5b57\u5178<code>data<\/code>\uff0c\u4f7f\u5176\u7ed3\u6784\u6e05\u6670\u53ef\u89c1\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7ed3\u5408\u5185\u5b58\u4fe1\u606f\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5728\u7ed3\u5408\u5185\u5b58\u4fe1\u606f\u65f6\uff0c<code>pprint<\/code>\u6a21\u5757\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u5c55\u793a\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u5728\u6253\u5370\u5185\u5b58\u5360\u7528\u60c5\u51b5\u65f6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import gc<\/p>\n<p>import sys<\/p>\n<p>import pprint<\/p>\n<p>gc.collect()<\/p>\n<p>all_objects = gc.get_objects()<\/p>\n<p>formatted_objects = [{&#39;repr&#39;: repr(obj), &#39;size&#39;: sys.getsizeof(obj)} for obj in all_objects]<\/p>\n<p>pprint.pprint(formatted_objects)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u5185\u5b58\u4e2d\u6240\u6709\u5bf9\u8c61\u7684\u8868\u793a\u548c\u5927\u5c0f\u653e\u5165\u4e00\u4e2a\u5217\u8868\uff0c\u5e76\u4f7f\u7528<code>pprint<\/code>\u6a21\u5757\u5c06\u5176\u683c\u5f0f\u5316\u8f93\u51fa\u3002\u8fd9\u4f7f\u5f97\u67e5\u770b\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u66f4\u52a0\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Python\u81ea\u5e26\u7684\u6a21\u5757\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u4e13\u95e8\u7528\u4e8e\u5206\u6790\u5185\u5b58\u4f7f\u7528\uff0c\u4f8b\u5982<code>memory_profiler<\/code>\u548c<code>pympler<\/code>\u3002<\/p>\n<\/p>\n<p><h4>1\u3001<code>memory_profiler<\/code><\/h4>\n<\/p>\n<p><p><code>memory_profiler<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u76d1\u63a7Python\u7a0b\u5e8f\u5185\u5b58\u4f7f\u7528\u7684\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u547d\u4ee4\u884c\u5de5\u5177\u548cAPI\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install memory_profiler<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u5de5\u5177\u76d1\u63a7\u811a\u672c\u5185\u5b58\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">mprof run my_script.py<\/p>\n<p>mprof plot<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001<code>pympler<\/code><\/h4>\n<\/p>\n<p><p><code>pympler<\/code>\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u5206\u6790\u5185\u5b58\u7684\u5de5\u5177\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u8be6\u7ec6\u7684\u5185\u5b58\u4f7f\u7528\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pympler<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>pympler<\/code>\u53ef\u4ee5\u66f4\u8be6\u7ec6\u5730\u67e5\u770b\u5185\u5b58\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pympler import summary, muppy<\/p>\n<p>all_objects = muppy.get_objects()<\/p>\n<p>sum1 = summary.summarize(all_objects)<\/p>\n<p>summary.print_(sum1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pympler<\/code>\u5e93\u4e2d\u7684<code>muppy<\/code>\u548c<code>summary<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u5e76\u6253\u5370\u5185\u5b58\u4f7f\u7528\u7684\u6982\u8ff0\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u67e5\u770b\u5f53\u524d\u7f13\u5b58\u548c\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u3002\u4ece\u4f7f\u7528\u5185\u7f6e\u6a21\u5757<code>sys<\/code>\u3001<code>gc<\/code>\uff0c\u5230\u5229\u7528<code>pprint<\/code>\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u518d\u5230\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982<code>memory_profiler<\/code>\u548c<code>pympler<\/code>\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u4e86\u89e3\u7a0b\u5e8f\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u5bf9\u4e8e\u4f18\u5316\u548c\u8c03\u8bd5\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u8fd9\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u8bc6\u522b\u6027\u80fd\u74f6\u9888\u548c\u6f5c\u5728\u7684\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u67e5\u770bPython\u4e2d\u7684\u5f53\u524d\u7f13\u5b58\u4f7f\u7528\u60c5\u51b5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u6a21\u5757<code>sys<\/code>\u6765\u67e5\u770b\u5f53\u524d\u7684\u7f13\u5b58\u4f7f\u7528\u60c5\u51b5\u3002\u901a\u8fc7<code>sys.getsizeof()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u83b7\u53d6\u5bf9\u8c61\u7684\u5927\u5c0f\uff0c\u4ece\u800c\u4e86\u89e3\u5185\u5b58\u7684\u4f7f\u7528\u60c5\u51b5\u3002\u82e5\u60f3\u6df1\u5165\u4e86\u89e3\uff0c\u4f7f\u7528<code>memory_profiler<\/code>\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5206\u6790\u5185\u5b58\u4f7f\u7528\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5185\u7f6e\u65b9\u6cd5\u53ef\u4ee5\u6e05\u7a7a\u7f13\u5b58\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u63d0\u4f9b\u4e86<code>gc.collect()<\/code>\u65b9\u6cd5\u6765\u624b\u52a8\u89e6\u53d1\u5783\u573e\u56de\u6536\uff0c\u4ece\u800c\u6e05\u7a7a\u672a\u4f7f\u7528\u7684\u7f13\u5b58\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u5e76\u4e0d\u4e00\u5b9a\u4f1a\u6e05\u7a7a\u6240\u6709\u7f13\u5b58\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f18\u5316Python\u7a0b\u5e8f\u7684\u7f13\u5b58\u4f7f\u7528\uff1f<\/strong><br \/>\u4f18\u5316\u7f13\u5b58\u4f7f\u7528\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\uff08\u5982<code>collections.deque<\/code>\u6216<code>set<\/code>\uff09\uff0c\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u5bf9\u8c61\u521b\u5efa\uff0c\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6765\u8282\u7701\u5185\u5b58\uff0c\u6216\u8005\u8003\u8651\u4f7f\u7528<code>functools.lru_cache<\/code>\u6765\u7f13\u5b58\u51fd\u6570\u7ed3\u679c\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6253\u5370\u5f53\u524d\u7f13\u5b58\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u6a21\u5757\u3001\u67e5\u770b\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3001\u4ee5\u53ca\u5229\u7528\u5916\u90e8\u5e93\u7b49\u3002\u5177\u4f53\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528Py [&hellip;]","protected":false},"author":3,"featured_media":1003661,"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\/1003654"}],"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=1003654"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1003654\/revisions"}],"predecessor-version":[{"id":1003662,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1003654\/revisions\/1003662"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1003661"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1003654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1003654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1003654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}