{"id":296,"date":"2012-04-05T21:41:36","date_gmt":"2012-04-06T01:41:36","guid":{"rendered":"http:\/\/zhanxw.com\/blog\/?p=296"},"modified":"2012-04-22T00:15:57","modified_gmt":"2012-04-22T04:15:57","slug":"linux%e7%8e%af%e5%a2%83%e4%b8%8b%e6%b5%8b%e9%87%8f%e7%a8%8b%e5%ba%8f%e7%9a%84%e5%86%85%e5%ad%98%e5%8d%a0%e7%94%a8","status":"publish","type":"post","link":"https:\/\/zhanxw.com\/blog\/2012\/04\/linux%e7%8e%af%e5%a2%83%e4%b8%8b%e6%b5%8b%e9%87%8f%e7%a8%8b%e5%ba%8f%e7%9a%84%e5%86%85%e5%ad%98%e5%8d%a0%e7%94%a8\/","title":{"rendered":"Linux\u73af\u5883\u4e0b\u6d4b\u91cf\u7a0b\u5e8f\u7684\u5185\u5b58\u5360\u7528"},"content":{"rendered":"<p>Measure memory consumption of programs in Linux<\/p>\n<p>\u5728\u8861\u91cf\u7a0b\u5e8f\u6027\u80fd\u65f6\u4e0d\u4ec5\u9700\u8981\u6d4b\u91cf\u8fd0\u884c\u65f6\u95f4\uff0c\u8fd8\u5e38\u5e38\u9700\u8981\u6d4b\u91cf\u5176\u5185\u5b58\u4f7f\u7528\u3002\u6bd4\u5982\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u6bd4\u66f4\u4e00\u4e2a\u7a0b\u5e8f\u5feb\uff0c\u6709\u53ef\u80fd\u662f\u4ee5\u7a7a\u95f4\u6362\u53d6\u65f6\u95f4\uff0c\u56e0\u6b64\u5355\u7eaf\u6bd4\u8f83\u8fd0\u884c\u901f\u5ea6\u5c31\u5931\u53bb\u4e86\u610f\u4e49\u3002\u6211\u4eec\u8fd9\u91cc\u6f14\u793a\u5982\u4f55\u5728Linux\u73af\u5883\u4e0b\u6d4b\u91cf\u7a0b\u5e8f\u7684\u5185\u5b58\u5360\u7528\u3002<\/p>\n<p>\u6211\u4eec\u5148\u8981\u5f04\u6e05\u695a\u5185\u5b58\u7684\u542b\u4e49\uff1a\u5bf9\u4e8e\u8fd0\u884c\u4e2d\u7684\u7a0b\u5e8f\u800c\u8a00\uff0c\u5b83\u6709\u56db\u4e2a\u90e8\u5206\uff1a\u6570\u636e\uff0c\u6587\u672c\uff0c\u5806\uff0c\u6808\uff08data, text, stack, heap\uff09\u3002\u5bf9\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u800c\u8a00\uff0c\u6240\u6709\u5206\u914d\u7ed9\u7a0b\u5e8f\u7684\u5185\u5b58\u90fd\u7528\u865a\u62df\u5185\u5b58\u8868\u793a\uff08Virtual Memory)\u3002\u5bf9\u786c\u4ef6\u800c\u8a00\uff0c\u5185\u5b58\u5206\u4e3a\u7269\u7406\u5185\u5b58\uff08Physical\uff09\u548c\u4ea4\u6362\u5185\u5b58\uff08Swap\uff09\u3002\u8fd9\u4e09\u79cd\u73af\u5883\u7684\u5173\u7cfb\u6709\u4e24\u79cd\uff1a\uff081\uff09\u7a0b\u5e8f\u5185\u5b58\u4f1a\u88ab\u6620\u5c04\u5230\u865a\u62df\u5185\u5b58\uff0c\u5373\u865a\u62df\u5185\u5b58\u4e2d\u5206\u6bb5\u5bf9\u5e94\u6570\u636e\u3001\u6587\u672c\u3001\u5806\u3001\u6808\uff1b \uff082\uff09\u865a\u62df\u5185\u5b58\u4e2d\u67d0\u4e9b\u90e8\u5206\u653e\u5728\u7269\u7406\u5185\u5b58\u4e2d\uff0c\u53e6\u4e00\u4e9b\u653e\u5230\u4ea4\u6362\u5185\u5b58\u4e2d\u3002<\/p>\n<p>\u4e3a\u4e86\u7a0b\u5e8f\u8fd0\u884c\u6709\u7a7a\u95f4\u4e0a\u7684\u6548\u7387\uff0cLinux\u7cfb\u7edf\u4e0b\u4f1a\u5171\u4eab\u5185\u5b58\u3002\u4f8b\u5982\u5171\u4eab\u7a0b\u5e8f\u5e93\uff08\u6bd4\u5982libc)\u6216\u8005\u662f\u5185\u5b58\u6620\u5c04\uff08memory map\uff09\u3002\u636e\u6211\u7684\u7406\u89e3\uff0c\u5171\u4eab\u90e8\u5206\u5728\u7a0b\u5e8f\u7a7a\u95f4\u4e2d\u53ef\u4ee5\u662f\u6587\u672c\u90e8\u5206\uff0c\u5728\u865a\u62df\u5185\u5b58\u4e2d\u653e\u5728\u76f8\u540c\u7684\u5730\u5740\uff0c\u5728\u786c\u4ef6\u4e2d\u53ef\u653e\u5728\u7269\u7406\u5185\u5b58\u6216\u4ea4\u6362\u5185\u5b58\u3002<\/p>\n<p>\u5bf9\u5e94\u4e0a\u9762\u4e0d\u540c\u7684\u6982\u5ff5\uff0cLinux\u5e38\u7528\u7684\u5185\u5b58\u76f8\u5173\u672f\u8bed\u6709\uff1a<br \/>\n\u5e38\u9a7b\u5185\u5b58\uff08Resident memory\uff09\uff0c \u8868\u793a\u786c\u4ef6\u4e2d\u7269\u7406\u5185\u5b58\u7684\u5360\u7528\u3002<br \/>\n\u865a\u62df\u5185\u5b58\uff0cVirtual memory\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f7f\u7528\u5185\u5b58\u7684\u4e00\u79cd\u62bd\u8c61\u3002<br \/>\n\u9875\uff08page\uff09\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4ee5\u9875\u4e3a\u5355\u4f4d\u7ba1\u7406\uff08\u5206\u914d\uff0c\u56de\u6536\uff09\u5185\u5b58<\/p>\n<p>\u6d4b\u91cf\u7a0b\u5e8f\u7684\u5185\u5b58\u5360\u7528\u53ef\u4ee5\u7528\u4e0d\u540c\u7684\u7edf\u8ba1\u91cf\u3002\u4ece\u7a0b\u5e8f\u7684\u751f\u547d\u5468\u671f\u6765\u770b\uff0c\u53ef\u4ee5\u5206\u4e3a\u6700\u5927\uff08peak\uff09\u5185\u5b58\u5360\u7528\uff0c\u5373\u65f6\u5185\u5b58\u5360\u7528\uff0c\u548c\u5e73\u5747\uff08average\uff09\u5185\u5b58\u5360\u7528\u3002\u4ece\u5185\u5b58\u7684\u542b\u4e49\u6765\u8bb2\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u672c\u8eab\u7684\u8bed\u5883\uff08context\uff09\uff0c\u6216\u662f\u64cd\u4f5c\u7cfb\u7edf\uff0c\u6216\u662f\u786c\u4ef6\u7684\u8bed\u5883\uff0c\u6bd4\u5982\uff1a\u6808\u5185\u5b58\u4f7f\u7528\uff08heap memory\uff09\uff0c\u865a\u62df\u5185\u5b58\u5360\u7528\uff0c\u7269\u7406\u5185\u5b58\u5360\u7528\u3002<\/p>\n<p>\u6d4b\u91cf\u4e00\u4e2a\u7a0b\u5e8f\u7684\u5185\u5b58\u5360\u7528\u65f6\uff0c\u6700\u7406\u60f3\u7684\u662f\u6211\u4eec\u77e5\u9053\uff08\u6bcf\u65f6\u6bcf\u523b\uff09\u7a0b\u5e8f\u7684\u56db\u4e2a\u90e8\u5206\uff08\u5728\u865a\u62df\u5185\u5b58\u4e2d\uff09\u5206\u522b\u6709\u591a\u5927\uff0c\u5bf9\u5e94\u7684\uff0c\u5728\u7269\u7406\u5185\u5b58\u6216\u8005\u4ea4\u6362\u5185\u5b58\u4e2d\u5360\u7528\u4e86\u591a\u5c11\u7a7a\u95f4\u3002\u4f46\u662f\u8fd9\u662f\u4e00\u4e2a\u5f88\u96be\u8fbe\u5230\u7684\u76ee\u6807\uff0c\u56e0\u4e3a\uff1a\uff081\uff09\u64cd\u4f5c\u7cfb\u7edf\u4ee5\u9875\u65b9\u5f0f\u5206\u914d\u5185\u5b58\uff0c\u5f88\u96be\u5f97\u5230\u6bcf\u4e00\u4e2a\u9875\u4e2d\u5177\u4f53\u591a\u5c11\u7a7a\u95f4\u88ab\u5360\u7528\uff1b\uff082\uff09\u865a\u62df\u5185\u5b58\u5f80\u5f80\u6bd4\u7269\u7406\u5185\u5b58\u5927\u5f88\u591a\uff0c\u5f53\u64cd\u4f5c\u7cfb\u7edf\u7ed9\u4e00\u4e2a\u8fd0\u884c\u7684\u7a0b\u5e8f\u5206\u914d\u5185\u5b58\u65f6\u603b\u662f\u63d0\u4f9b\u591a\u4f59\u7a0b\u5e8f\u7cbe\u786e\u9700\u8981\u7684\u5185\u5b58\uff1b\uff083\uff09\u6ce8\u7a0b\u5e8f\u5728\u7269\u7406\u5185\u5b58\u7684\u5360\u7528\u52a0\u4e0a\u7a0b\u5e8f\u5728\u4ea4\u6362\u5185\u5b58\u7684\u5360\u7528\u6784\u6210\u4e86\u7a0b\u5e8f\u603b\u7684\u4f7f\u7528\u60c5\u51b5\uff0c\u4f46\u662f\u73b0\u6709\u7684\u5de5\u5177\u5e76\u4e0d\u76f4\u63a5\u7ed9\u51fa\u8fd9\u4e00\u6570\u503c\uff0c\u9700\u8981\u624b\u52a8\u628aResident Memory\u548cShared Memory\u76f8\u52a0 \u3002<\/p>\n<p>\u5728\u5b9e\u9645\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u4e00\u4e9b\u73b0\u6709\u7684\u5de5\u5177\uff0c\u6211\u4eec\u5f80\u5f80\u66f4\u5173\u5fc3\u8fd9\u51e0\u4e2a\u6613\u4e8e\u83b7\u5f97\u7edf\u8ba1\u91cf\uff1a<br \/>\n\uff081\uff09max resident memory size<br \/>\n\u53ef\u4ee5\u4f7f\u7528time -v\u547d\u4ee4\uff08\u8981\u6307\u5b9a\u5168\u90e8\u8def\u5f84\uff0c\u5426\u5219bash\u7684time\u547d\u4ee4\u4e0d\u8bc6\u522b-v\uff09<br \/>\n\u6ce8\u610f\u8fd9\u4e2a\u6d4b\u91cf\u503c\u5f80\u5f80\u8fdc\u8fdc\u9ad8\u4e8e\u7a0b\u5e8f\u8fd0\u884c\u65f6\u6d88\u8017\u7684\u5185\u5b58\u3002<br \/>\n\uff08\u8fd9\u662ftime 1.7\u7248\u672c\u7684bug\uff0ctime \u62a5\u544a\u7684\u5185\u5b58\u7528\u91cf\u662f\u5b9e\u9645\u76844\u500d\uff0c\u56e0\u4e3await3\/wait4\u8fd4\u56de\u7684\u5b9e\u9645\u5185\u5b58\u7528\u91cf\uff0c\u5355\u4f4d\u662fkbyte\uff0c\u4f46time\u9519\u628a\u5355\u4f4d\u8bbe\u5b9a\u4e3apage\uff09<br \/>\n\u672c\u8d28\u4e0atime\u4f7f\u7528\u4e86wait3\u6216\u8005wait4\u547d\u4ee4\u6765\u83b7\u53d6\u7a0b\u5e8f\u7684resources \uff08\u89c1 \u2018man wait3\u2019\uff09\u3002<br \/>\n\u4f7f\u7528Python\u5b9e\u73b0\u65f6\uff0c\u53ef\u4ee5\u7528resources module\u6765\u83b7\u53d6\u3002<br \/>\n\u53e6\u5916\uff0c\u4f7f\u7528\u8fd9\u4e2a\u601d\u8def\u8fd8\u53ef\u4ee5\u5f97\u5230\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\uff08\u7528\u6237\u65f6\u95f4\uff0c\u5185\u6838\u65f6\u95f4\uff0c\u5b9e\u9645\u65f6\u95f4\uff09<\/p>\n<p>\uff082\uff09top \u6216\u8005ps<br \/>\n\u53ef\u4ee5\u67e5\u770b\u7a0b\u5e8f\u7684Resident Size(RES)\u5217\uff0cShare Memory\uff08SHR\uff09\u4ee5\u53caVirtual Memory Size(VIRT)\uff0c\u8fd9\u4e00\u7edf\u8ba1\u91cf\u9ed8\u8ba4\u6bcf\u79d2\u66f4\u65b0\u4e00\u6b21\u3002\u8fd9\u4e00\u65b9\u5f0f\u7684\u7f3a\u70b9\u662f\u6d4b\u91cf\u503c\u4e3a\u5373\u65f6\u6d4b\u91cf\uff0c\u6bcf\u65f6\u6bcf\u523b\u8fd9\u4e2a\u503c\u90fd\u6709\u53ef\u80fd\u53d8\u5316\u3002\u4f18\u70b9\u662f\u4f7f\u7528\u8d77\u6765\u5f88\u65b9\u4fbf\u3002<br \/>\n\u4f7f\u7528Python\u5b9e\u73b0\u65f6\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528multiprocesing.Process\u542f\u52a8\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u7136\u540e\u4f7f\u7528psutil module\uff0c\u6784\u9020psutl.Process(<pid>)\uff0c\u7136\u540e\u4f7f\u7528get_memory_info\u65b9\u5f0f\u6765\u83b7\u53d6rss,vms<\/p>\n<p>\uff083\uff09\u4e0e\uff082\uff09\u7c7b\u4f3c\uff0c\u4f46\u4e3a\u4e86\u66f4\u591a\u7684\u4fe1\u606f\uff0c\u8fd8\u53ef\u4ee5\u8bfb\u53d6\/proc<br \/>\n\u8fd9\u662f\u901a\u8fc7\u8bfb\u53d6\/proc\/<pid>\/status\u548c\/proc\/<pid>\/smaps\u6765\u83b7\u5f97\u7a0b\u5e8f\u7684\u5185\u5b58\u4f7f\u7528\u91cf\u7684\u3002\u7406\u8bba\u4e0a\u8fd9\u662f\u6700\u7cbe\u786e\u7684\u6d4b\u91cf\u3002\u8fd9\u4e24\u4e2a\u6587\u4ef6\u7684\u5177\u4f53\u683c\u5f0f\u53ef\u4ee5\u7528\u2018man proc\u2019\u6765\u83b7\u5f97\u3002<br \/>\n\u5728Python\u4e2d\uff0c\u9700\u8981\u53e6\u5916\u5199\u7a0b\u5e8f\u6765\u89e3\u91ca\u8fd9\u4e24\u4e2a\u6587\u4ef6\uff0c\u4f8b\u5982\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre>\r\nzhanxw@amd: ~> cat \/proc\/15648\/status\r\nName:\ttakeMem\r\nState:\tR (running)\r\nTgid:\t15648\r\nPid:\t15648\r\nPPid:\t15163\r\nTracerPid:\t0\r\nUid:\t248396\t248396\t248396\t248396\r\nGid:\t248396\t248396\t248396\t248396\r\nFDSize:\t256\r\nGroups:\t500 1007 1013 1017 1028 1033 248396 \r\nVmPeak:\t   12752 kB\r\nVmSize:\t   12752 kB\r\nVmLck:\t       0 kB\r\nVmHWM:\t    1756 kB\r\nVmRSS:\t    1756 kB\r\nVmData:\t    1112 kB\r\nVmStk:\t     136 kB\r\nVmExe:\t       4 kB\r\nVmLib:\t    3232 kB\r\nVmPTE:\t      40 kB\r\nVmSwap:\t       0 kB\r\nThreads:\t1\r\n<\/pre>\n<p>\u5728\u5b9e\u8df5\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5df2\u6709\u7684\u7a0b\u5e8f\uff0c\u4f8b\u5982tmem\u30104\u3011\uff0c\u4ee5\u53calh3\u7684runit\u30106\u3011\uff08\u4e0b\u8f7dudp\uff0c\u5728runit\/\u4e0b\uff0c\u7528make\u7f16\u8bd1\uff09\u3002\u8f93\u51fa\u5982\u4e0b\u3002<\/p>\n<pre>\r\ntmem\r\n\r\n.\/tmem .\/takeMem 1000000000\r\n\t    3908\t\t    3908\t      84\t      84\r\nAllocating 1000000000 memory\r\n\t  988336\t\t  988336\t  148012\t  148012\r\n\t  988336\t\t  988336\t  308524\t  308524\r\n\t  988336\t\t  988336\t  442636\t  442636\r\n\t  988336\t\t  988336\t  567244\t  567244\r\n\t  988336\t\t  988336\t  686308\t  686308\r\n\t  988336\t\t  988336\t  796396\t  796396\r\n\t  988336\t\t  988336\t  889588\t  889588\r\n\t  988336\t\t  988336\t  949516\t  949516\r\n<\/pre>\n<pre>\r\nrunit\r\n\r\n.\/runit ~\/mycode\/smake\/takeMem 1000\r\nAllocating 1000 memory\r\n-- CMD: .\/runit \/net\/fantasia\/home\/zhanxw\/mycode\/smake\/takeMem 1000\r\n\r\n-- totalmem     198345252.000 kB\r\n-- available    190966956.000 kB\r\n-- free           1569216.000 kB\r\n\r\n-- retval                   0\r\n-- real                15.749 sec\r\n-- user                15.350 sec\r\n-- sys                  0.170 sec\r\n-- maxrss             704.000 kB\r\n-- avgrss             704.000 kB\r\n-- maxvsize         11904.000 kB\r\n-- avgvsize         11904.000 kB\r\n<\/pre>\n<p>\u53c2\u8003\u8d44\u6599\uff1a<br \/>\n\u30101\u3011<a href=\"http:\/\/docs.python.org\/library\/resource.html\" title=\"Python resource module\">Python resource \u6a21\u5757 <\/a><br \/>\n\u30102\u3011<a href=\"http:\/\/docs.python.org\/library\/os.html\" title=\"Python os module\">Python os \u6a21\u5757\uff0c\u4ecb\u7ecdwait3\uff0cwait4<\/a><br \/>\n\u30103\u3011<a href=\"http:\/\/docs.python.org\/library\/multiprocessing.html\" title=\"Python multiprocessing module\">Python multiprocessing\u6a21\u5757\uff0c\u4ecb\u7ecd\u4e86Process\u8fd9\u4e2a\u6613\u4e8e\u4f7f\u7528\u7684\u7c7b\uff0c\u4e0esubprocess\u76f8\u4f3c\uff0c\u4f46\u6709\u66f4\u7075\u6d3b\u7684\u7528\u6cd5<\/a><br \/>\n\u30104\u3011<a href=\"http:\/\/locklessinc.com\/articles\/memory_usage\/\" title=\"Measuring Memory Usage  \">Measuring Memory Usage\uff0c\u4ecb\u7ecd\u4e86\u7528C\u6765\u8bfb\u53d6\/proc\/<pid>\/status\u7684\u7a0b\u5e8f\uff0c\u5e76\u7ed9\u51fatmem\u7684\u4e0b\u8f7d\u94fe\u63a5<\/a><br \/>\n\u30105\u3011<a href=\"http:\/\/careers.directi.com\/display\/tu\/Understanding+and+optimizing+Memory+utilization\" title=\"Understanding memory\">Understanding memory\uff0c\u975e\u5e38\u8be6\u7ec6\u7684\u4ecb\u7ecdLinux\u5185\u5b58\u5206\u914d\uff0c\u4f18\u5316<\/a><br \/>\n\u30106\u3011<a href=\"http:\/\/attractivechaos.wordpress.com\/programs\/\" title=\"udp source code\">Heng Li&#8217;s personal website \u53ef\u4ee5\u4e0b\u8f7d\u6bd4\u8f83hash library\u7684\u7a0b\u5e8f\uff0c\u91cc\u9762\u6709\u4e00\u4e2arunint\/\u6587\u4ef6\u5939\uff0c\u7f16\u8bd1\u4e4b\u540e\u53ef\u4ee5\u7528runit\u6765\u6d4b\u91cf\u5185\u5b58\u4f7f\u7528<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Measure memory consumption of programs in Linux \u5728\u8861\u91cf\u7a0b\u5e8f\u6027\u80fd\u65f6\u4e0d\u4ec5\u9700\u8981\u6d4b\u91cf\u8fd0\u884c\u65f6\u95f4\uff0c\u8fd8\u5e38\u5e38\u9700\u8981\u6d4b\u91cf\u5176\u5185\u5b58\u4f7f\u7528\u3002\u6bd4\u5982\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u6bd4\u66f4\u4e00\u4e2a\u7a0b\u5e8f\u5feb\uff0c\u6709\u53ef\u80fd\u662f\u4ee5\u7a7a\u95f4\u6362\u53d6\u65f6\u95f4\uff0c\u56e0\u6b64\u5355\u7eaf\u6bd4\u8f83\u8fd0\u884c\u901f\u5ea6\u5c31\u5931\u53bb\u4e86\u610f\u4e49\u3002\u6211\u4eec\u8fd9\u91cc\u6f14\u793a\u5982\u4f55\u5728Linux\u73af\u5883\u4e0b\u6d4b\u91cf\u7a0b\u5e8f\u7684\u5185\u5b58\u5360\u7528\u3002 \u6211\u4eec\u5148\u8981\u5f04\u6e05\u695a\u5185\u5b58\u7684\u542b\u4e49\uff1a\u5bf9\u4e8e\u8fd0\u884c\u4e2d\u7684\u7a0b\u5e8f\u800c\u8a00\uff0c\u5b83\u6709\u56db\u4e2a\u90e8\u5206\uff1a\u6570\u636e\uff0c\u6587\u672c\uff0c\u5806\uff0c\u6808\uff08data, text, stack, heap\uff09\u3002\u5bf9\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u800c\u8a00\uff0c\u6240\u6709\u5206\u914d\u7ed9\u7a0b\u5e8f\u7684\u5185\u5b58\u90fd\u7528\u865a\u62df\u5185\u5b58\u8868\u793a\uff08Virtual Memory)\u3002\u5bf9\u786c\u4ef6\u800c\u8a00\uff0c\u5185\u5b58\u5206\u4e3a\u7269\u7406\u5185\u5b58\uff08Physical\uff09\u548c\u4ea4\u6362\u5185\u5b58\uff08Swap\uff09\u3002\u8fd9\u4e09\u79cd\u73af\u5883\u7684\u5173\u7cfb\u6709\u4e24\u79cd\uff1a\uff081\uff09\u7a0b\u5e8f\u5185\u5b58\u4f1a\u88ab\u6620\u5c04\u5230\u865a\u62df\u5185\u5b58\uff0c\u5373\u865a\u62df\u5185\u5b58\u4e2d\u5206\u6bb5\u5bf9\u5e94\u6570\u636e\u3001\u6587\u672c\u3001\u5806\u3001\u6808\uff1b \uff082\uff09\u865a\u62df\u5185\u5b58\u4e2d\u67d0\u4e9b\u90e8\u5206\u653e\u5728\u7269\u7406\u5185\u5b58\u4e2d\uff0c\u53e6\u4e00\u4e9b\u653e\u5230\u4ea4\u6362\u5185\u5b58\u4e2d\u3002 \u4e3a\u4e86\u7a0b\u5e8f\u8fd0\u884c\u6709\u7a7a\u95f4\u4e0a\u7684\u6548\u7387\uff0cLinux\u7cfb\u7edf\u4e0b\u4f1a\u5171\u4eab\u5185\u5b58\u3002\u4f8b\u5982\u5171\u4eab\u7a0b\u5e8f\u5e93\uff08\u6bd4\u5982libc)\u6216\u8005\u662f\u5185\u5b58\u6620\u5c04\uff08memory map\uff09\u3002\u636e\u6211\u7684\u7406\u89e3\uff0c\u5171\u4eab\u90e8\u5206\u5728\u7a0b\u5e8f\u7a7a\u95f4\u4e2d\u53ef\u4ee5\u662f\u6587\u672c\u90e8\u5206\uff0c\u5728\u865a\u62df\u5185\u5b58\u4e2d\u653e\u5728\u76f8\u540c\u7684\u5730\u5740\uff0c\u5728\u786c\u4ef6\u4e2d\u53ef\u653e\u5728\u7269\u7406\u5185\u5b58\u6216\u4ea4\u6362\u5185\u5b58\u3002 \u5bf9\u5e94\u4e0a\u9762\u4e0d\u540c\u7684\u6982\u5ff5\uff0cLinux\u5e38\u7528\u7684\u5185\u5b58\u76f8\u5173\u672f\u8bed\u6709\uff1a \u5e38\u9a7b\u5185\u5b58\uff08Resident memory\uff09\uff0c \u8868\u793a\u786c\u4ef6\u4e2d\u7269\u7406\u5185\u5b58\u7684\u5360\u7528\u3002 \u865a\u62df\u5185\u5b58\uff0cVirtual memory\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f7f\u7528\u5185\u5b58\u7684\u4e00\u79cd\u62bd\u8c61\u3002 \u9875\uff08page\uff09\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4ee5\u9875\u4e3a\u5355\u4f4d\u7ba1\u7406\uff08\u5206\u914d\uff0c\u56de\u6536\uff09\u5185\u5b58 \u6d4b\u91cf\u7a0b\u5e8f\u7684\u5185\u5b58\u5360\u7528\u53ef\u4ee5\u7528\u4e0d\u540c\u7684\u7edf\u8ba1\u91cf\u3002\u4ece\u7a0b\u5e8f\u7684\u751f\u547d\u5468\u671f\u6765\u770b\uff0c\u53ef\u4ee5\u5206\u4e3a\u6700\u5927\uff08peak\uff09\u5185\u5b58\u5360\u7528\uff0c\u5373\u65f6\u5185\u5b58\u5360\u7528\uff0c\u548c\u5e73\u5747\uff08average\uff09\u5185\u5b58\u5360\u7528\u3002\u4ece\u5185\u5b58\u7684\u542b\u4e49\u6765\u8bb2\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u672c\u8eab\u7684\u8bed\u5883\uff08context\uff09\uff0c\u6216\u662f\u64cd\u4f5c\u7cfb\u7edf\uff0c\u6216\u662f\u786c\u4ef6\u7684\u8bed\u5883\uff0c\u6bd4\u5982\uff1a\u6808\u5185\u5b58\u4f7f\u7528\uff08heap memory\uff09\uff0c\u865a\u62df\u5185\u5b58\u5360\u7528\uff0c\u7269\u7406\u5185\u5b58\u5360\u7528\u3002 \u6d4b\u91cf\u4e00\u4e2a\u7a0b\u5e8f\u7684\u5185\u5b58\u5360\u7528\u65f6\uff0c\u6700\u7406\u60f3\u7684\u662f\u6211\u4eec\u77e5\u9053\uff08\u6bcf\u65f6\u6bcf\u523b\uff09\u7a0b\u5e8f\u7684\u56db\u4e2a\u90e8\u5206\uff08\u5728\u865a\u62df\u5185\u5b58\u4e2d\uff09\u5206\u522b\u6709\u591a\u5927\uff0c\u5bf9\u5e94\u7684\uff0c\u5728\u7269\u7406\u5185\u5b58\u6216\u8005\u4ea4\u6362\u5185\u5b58\u4e2d\u5360\u7528\u4e86\u591a\u5c11\u7a7a\u95f4\u3002\u4f46\u662f\u8fd9\u662f\u4e00\u4e2a\u5f88\u96be\u8fbe\u5230\u7684\u76ee\u6807\uff0c\u56e0\u4e3a\uff1a\uff081\uff09\u64cd\u4f5c\u7cfb\u7edf\u4ee5\u9875\u65b9\u5f0f\u5206\u914d\u5185\u5b58\uff0c\u5f88\u96be\u5f97\u5230\u6bcf\u4e00\u4e2a\u9875\u4e2d\u5177\u4f53\u591a\u5c11\u7a7a\u95f4\u88ab\u5360\u7528\uff1b\uff082\uff09\u865a\u62df\u5185\u5b58\u5f80\u5f80\u6bd4\u7269\u7406\u5185\u5b58\u5927\u5f88\u591a\uff0c\u5f53\u64cd\u4f5c\u7cfb\u7edf\u7ed9\u4e00\u4e2a\u8fd0\u884c\u7684\u7a0b\u5e8f\u5206\u914d\u5185\u5b58\u65f6\u603b\u662f\u63d0\u4f9b\u591a\u4f59\u7a0b\u5e8f\u7cbe\u786e\u9700\u8981\u7684\u5185\u5b58\uff1b\uff083\uff09\u6ce8\u7a0b\u5e8f\u5728\u7269\u7406\u5185\u5b58\u7684\u5360\u7528\u52a0\u4e0a\u7a0b\u5e8f\u5728\u4ea4\u6362\u5185\u5b58\u7684\u5360\u7528\u6784\u6210\u4e86\u7a0b\u5e8f\u603b\u7684\u4f7f\u7528\u60c5\u51b5\uff0c\u4f46\u662f\u73b0\u6709\u7684\u5de5\u5177\u5e76\u4e0d\u76f4\u63a5\u7ed9\u51fa\u8fd9\u4e00\u6570\u503c\uff0c\u9700\u8981\u624b\u52a8\u628aResident Memory\u548cShared Memory\u76f8\u52a0 \u3002 \u5728\u5b9e\u9645\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u4e00\u4e9b\u73b0\u6709\u7684\u5de5\u5177\uff0c\u6211\u4eec\u5f80\u5f80\u66f4\u5173\u5fc3\u8fd9\u51e0\u4e2a\u6613\u4e8e\u83b7\u5f97\u7edf\u8ba1\u91cf\uff1a \uff081\uff09max resident memory size \u53ef\u4ee5\u4f7f\u7528time -v\u547d\u4ee4\uff08\u8981\u6307\u5b9a\u5168\u90e8\u8def\u5f84\uff0c\u5426\u5219bash\u7684time\u547d\u4ee4\u4e0d\u8bc6\u522b-v\uff09 \u6ce8\u610f\u8fd9\u4e2a\u6d4b\u91cf\u503c\u5f80\u5f80\u8fdc\u8fdc\u9ad8\u4e8e\u7a0b\u5e8f\u8fd0\u884c\u65f6\u6d88\u8017\u7684\u5185\u5b58\u3002 \uff08\u8fd9\u662ftime 1.7\u7248\u672c\u7684bug\uff0ctime \u62a5\u544a\u7684\u5185\u5b58\u7528\u91cf\u662f\u5b9e\u9645\u76844\u500d\uff0c\u56e0\u4e3await3\/wait4\u8fd4\u56de\u7684\u5b9e\u9645\u5185\u5b58\u7528\u91cf\uff0c\u5355\u4f4d\u662fkbyte\uff0c\u4f46time\u9519\u628a\u5355\u4f4d\u8bbe\u5b9a\u4e3apage\uff09 \u672c\u8d28\u4e0atime\u4f7f\u7528\u4e86wait3\u6216\u8005wait4\u547d\u4ee4\u6765\u83b7\u53d6\u7a0b\u5e8f\u7684resources \uff08\u89c1 \u2018man wait3\u2019\uff09\u3002 \u4f7f\u7528Python\u5b9e\u73b0\u65f6\uff0c\u53ef\u4ee5\u7528resources module\u6765\u83b7\u53d6\u3002 \u53e6\u5916\uff0c\u4f7f\u7528\u8fd9\u4e2a\u601d\u8def\u8fd8\u53ef\u4ee5\u5f97\u5230\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\uff08\u7528\u6237\u65f6\u95f4\uff0c\u5185\u6838\u65f6\u95f4\uff0c\u5b9e\u9645\u65f6\u95f4\uff09 \uff082\uff09top \u6216\u8005ps \u53ef\u4ee5\u67e5\u770b\u7a0b\u5e8f\u7684Resident Size(RES)\u5217\uff0cShare Memory\uff08SHR\uff09\u4ee5\u53caVirtual Memory Size(VIRT)\uff0c\u8fd9\u4e00\u7edf\u8ba1\u91cf\u9ed8\u8ba4\u6bcf\u79d2\u66f4\u65b0\u4e00\u6b21\u3002\u8fd9\u4e00\u65b9\u5f0f\u7684\u7f3a\u70b9\u662f\u6d4b\u91cf\u503c\u4e3a\u5373\u65f6\u6d4b\u91cf\uff0c\u6bcf\u65f6\u6bcf\u523b\u8fd9\u4e2a\u503c\u90fd\u6709\u53ef\u80fd\u53d8\u5316\u3002\u4f18\u70b9\u662f\u4f7f\u7528\u8d77\u6765\u5f88\u65b9\u4fbf\u3002 \u4f7f\u7528Python\u5b9e\u73b0\u65f6\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528multiprocesing.Process\u542f\u52a8\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u7136\u540e\u4f7f\u7528psutil [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-296","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/comments?post=296"}],"version-history":[{"count":0,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/296\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/media?parent=296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/categories?post=296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/tags?post=296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}