{"id":195220,"date":"2020-07-13T08:13:51","date_gmt":"2020-07-13T00:13:51","guid":{"rendered":"https:\/\/gulass.cn\/?p=195220"},"modified":"2020-07-01T09:11:21","modified_gmt":"2020-07-01T01:11:21","slug":"linux-process-memory","status":"publish","type":"post","link":"https:\/\/gulass.cn\/linux-process-memory.html","title":{"rendered":"Linux\u8fdb\u7a0b\u5185\u5b58\u7ba1\u7406\u5bf9\u7a0b\u5e8f\u5f00\u53d1\u8005\u7684\u5f71\u54cd\u8be6\u8c08"},"content":{"rendered":"
\u5bfc\u8bfb<\/td>\n | \u672c\u7ae0\u9996\u5148\u4ee5\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u8005\u7684\u89d2\u5ea6\u5ba1\u89c6Linux\u7684\u8fdb\u7a0b\u5185\u5b58\u7ba1\u7406\uff0c\u5728\u6b64\u57fa\u7840\u4e0a\u9010\u6b65\u6df1\u5165\u5230\u5185\u6838\u4e2d\u8ba8\u8bba\u7cfb\u7edf\u7269\u7406\u5185\u5b58\u7ba1\u7406\u548c\u5185\u6838\u5185\u5b58\u7684\u4f7f\u7528\u65b9\u6cd5\u3002\u529b\u6c42\u4ece\u5916\u5230\u5185\u3001\u6c34\u5230\u6e20\u6210\u5730\u5f15\u5bfc\u7f51\u53cb\u5206\u6790Linux\u7684\u5185\u5b58\u7ba1\u7406\u4e0e\u4f7f\u7528\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \u672c\u7ae0\u9996\u5148\u4ee5\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u8005\u7684\u89d2\u5ea6\u5ba1\u89c6Linux\u7684\u8fdb\u7a0b\u5185\u5b58\u7ba1\u7406\uff0c\u5728\u6b64\u57fa\u7840\u4e0a\u9010\u6b65\u6df1\u5165\u5230\u5185\u6838\u4e2d\u8ba8\u8bba\u7cfb\u7edf\u7269\u7406\u5185\u5b58\u7ba1\u7406\u548c\u5185\u6838\u5185\u5b58\u7684\u4f7f\u7528\u65b9\u6cd5\u3002\u529b\u6c42\u4ece\u5916\u5230\u5185\u3001\u6c34\u5230\u6e20\u6210\u5730\u5f15\u5bfc\u7f51\u53cb\u5206\u6790Linux\u7684\u5185\u5b58\u7ba1\u7406\u4e0e\u4f7f\u7528\u3002\u5728\u672c\u7ae0\u6700\u540e\uff0c\u6211\u4eec\u7ed9\u51fa\u4e00\u4e2a\u5185\u5b58\u6620\u5c04\u7684\u5b9e\u4f8b\uff0c\u5e2e\u52a9\u7f51\u53cb\u4eec\u7406\u89e3\u5185\u6838\u5185\u5b58\u7ba1\u7406\u4e0e\u7528\u6237\u5185\u5b58\u7ba1\u7406\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u5e0c\u671b\u5927\u5bb6\u6700\u7ec8\u80fd\u9a7e\u9a6dLinux\u5185\u5b58\u7ba1\u7406\u3002<\/p>\n <\/p>\n \u524d\u8a00<\/strong><\/div>\n \u5185\u5b58\u7ba1\u7406\u4e00\u5411\u662f\u6240\u6709\u64cd\u4f5c\u7cfb\u7edf\u4e66\u7c4d\u4e0d\u60dc\u7b14\u58a8\u91cd\u70b9\u8ba8\u8bba\u7684\u5185\u5bb9\uff0c\u65e0\u8bba\u5e02\u9762\u4e0a\u6216\u662f\u7f51\u4e0a\u90fd\u5145\u65a5\u7740\u5927\u91cf\u6d89\u53ca\u5185\u5b58\u7ba1\u7406\u7684\u6559\u6750\u548c\u8d44\u6599\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u8fd9\u91cc\u6240\u8981\u5199\u7684Linux\u5185\u5b58\u7ba1\u7406\u91c7\u53d6\u907f\u91cd\u5c31\u8f7b\u7684\u7b56\u7565\uff0c\u4ece\u7406\u8bba\u5c42\u9762\u5c31\u4e0d\u53bb\u73ed\u95e8\u5f04\u65a7\uff0c\u8d3b\u7b11\u5927\u65b9\u4e86\u3002\u6211\u4eec\u6700\u60f3\u505a\u7684\u548c\u53ef\u80fd\u505a\u5230\u7684\u662f\u4ece\u5f00\u53d1\u8005\u7684\u89d2\u5ea6\u8c08\u8c08\u5bf9\u5185\u5b58\u7ba1\u7406\u7684\u7406\u89e3\uff0c\u6700\u7ec8\u76ee\u7684\u662f\u628a\u6211\u4eec\u5728\u5185\u6838\u5f00\u53d1\u4e2d\u4f7f\u7528\u5185\u5b58\u7684\u7ecf\u9a8c\u548c\u5bf9Linux\u5185\u5b58\u7ba1\u7406\u7684\u8ba4\u8bc6\u4e0e\u5927\u5bb6\u5171\u4eab\u3002<\/p>\n \u5f53\u7136\uff0c\u8fd9\u5176\u4e2d\u6211\u4eec\u4e5f\u4f1a\u6d89\u53ca\u5230\u4e00\u4e9b\u8bf8\u5982\u6bb5\u9875\u7b49\u5185\u5b58\u7ba1\u7406\u7684\u57fa\u672c\u7406\u8bba\uff0c\u4f46\u6211\u4eec\u7684\u76ee\u7684\u4e0d\u662f\u4e3a\u4e86\u5f3a\u8c03\u7406\u8bba\uff0c\u800c\u662f\u4e3a\u4e86\u6307\u5bfc\u7406\u89e3\u5f00\u53d1\u4e2d\u7684\u5b9e\u8df5\uff0c\u6240\u4ee5\u4ec5\u4ec5\u70b9\u5230\u4e3a\u6b62\uff0c\u4e0d\u505a\u6df1\u7a76\u3002<\/p>\n \u9075\u5faa\u201c\u7406\u8bba\u6765\u6e90\u4e8e\u5b9e\u8df5\u201d\u7684\u201c\u6559\u6761\u201d\uff0c\u6211\u4eec\u5148\u4e0d\u5fc5\u4e00\u4e0b\u5b50\u5c31\u94bb\u5165\u5185\u6838\u91cc\u53bb\u770b\u7cfb\u7edf\u5185\u5b58\u5230\u5e95\u662f\u5982\u4f55\u7ba1\u7406\uff0c\u90a3\u6837\u5f80\u5f80\u4f1a\u8ba9\u4f60\u9677\u5165\u4f3c\u61c2\u975e\u61c2\u7684\u7a98\u5883(\u6211\u5f53\u5e74\u5c31\u72af\u4e86\u8fd9\u4e2a\u9519\u8bef!)\u3002\u6240\u4ee5\u6700\u597d\u7684\u65b9\u5f0f\u662f\u5148\u4ece\u5916\u90e8(\u7528\u6237\u7f16\u7a0b\u8303\u7574)\u6765\u89c2\u5bdf\u8fdb\u7a0b\u5982\u4f55\u4f7f\u7528\u5185\u5b58\uff0c\u7b49\u5230\u5927\u5bb6\u5bf9\u5185\u5b58\u7684\u4f7f\u7528\u6709\u4e86\u8f83\u76f4\u89c2\u7684\u8ba4\u8bc6\u540e\uff0c\u518d\u6df1\u5165\u5230\u5185\u6838\u4e2d\u53bb\u5b66\u4e60\u5185\u5b58\u5982\u4f55\u88ab\u7ba1\u7406\u7b49\u7406\u8bba\u77e5\u8bc6\u3002\u6700\u540e\u518d\u901a\u8fc7\u4e00\u4e2a\u5b9e\u4f8b\u7f16\u7a0b\u5c06\u6240\u8bb2\u5185\u5bb9\u878d\u4f1a\u8d2f\u901a\u3002<\/p>\n \u8fdb\u7a0b\u4e0e\u5185\u5b58<\/strong><\/div>\n \u8fdb\u7a0b\u5982\u4f55\u4f7f\u7528\u5185\u5b58?<\/strong><\/span><\/div>\n \u6beb\u65e0\u7591\u95ee\uff0c\u6240\u6709\u8fdb\u7a0b(\u6267\u884c\u7684\u7a0b\u5e8f)\u90fd\u5fc5\u987b\u5360\u7528\u4e00\u5b9a\u6570\u91cf\u7684\u5185\u5b58\uff0c\u5b83\u6216\u662f\u7528\u6765\u5b58\u653e\u4ece\u78c1\u76d8\u8f7d\u5165\u7684\u7a0b\u5e8f\u4ee3\u7801\uff0c\u6216\u662f\u5b58\u653e\u53d6\u81ea\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u7b49\u7b49\u3002\u4e0d\u8fc7\u8fdb\u7a0b\u5bf9\u8fd9\u4e9b\u5185\u5b58\u7684\u7ba1\u7406\u65b9\u5f0f\u56e0\u5185\u5b58\u7528\u9014\u4e0d\u4e00\u800c\u4e0d\u5c3d\u76f8\u540c\uff0c\u6709\u4e9b\u5185\u5b58\u662f\u4e8b\u5148\u9759\u6001\u5206\u914d\u548c\u7edf\u4e00\u56de\u6536\u7684\uff0c\u800c\u6709\u4e9b\u5374\u662f\u6309\u9700\u8981\u52a8\u6001\u5206\u914d\u548c\u56de\u6536\u7684\u3002<\/p>\n \u5bf9\u4efb\u4f55\u4e00\u4e2a\u666e\u901a\u8fdb\u7a0b\u6765\u8bb2\uff0c\u5b83\u90fd\u4f1a\u6d89\u53ca\u52305\u79cd\u4e0d\u540c\u7684\u6570\u636e\u6bb5\u3002\u7a0d\u6709\u7f16\u7a0b\u77e5\u8bc6\u7684\u670b\u53cb\u90fd\u80fd\u60f3\u5230\u8fd9\u51e0\u4e2a\u6570\u636e\u6bb5\u4e2d\u5305\u542b\u6709\u201c\u7a0b\u5e8f\u4ee3\u7801\u6bb5\u201d\u3001\u201c\u7a0b\u5e8f\u6570\u636e\u6bb5\u201d\u3001\u201c\u7a0b\u5e8f\u5806\u6808\u6bb5\u201d\u7b49\u3002\u4e0d\u9519\uff0c\u8fd9\u51e0\u79cd\u6570\u636e\u6bb5\u90fd\u5728\u5176\u4e2d\uff0c\u4f46\u9664\u4e86\u4ee5\u4e0a\u51e0\u79cd\u6570\u636e\u6bb5\u4e4b\u5916\uff0c\u8fdb\u7a0b\u8fd8\u53e6\u5916\u5305\u542b\u4e24\u79cd\u6570\u636e\u6bb5\u3002\u4e0b\u9762\u6211\u4eec\u6765\u7b80\u5355\u5f52\u7eb3\u4e00\u4e0b\u8fdb\u7a0b\u5bf9\u5e94\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\u6240\u5305\u542b\u76845\u79cd\u4e0d\u540c\u7684\u6570\u636e\u533a\u3002<\/p>\n \u4ee3\u7801\u6bb5\uff1a\u4ee3\u7801\u6bb5\u662f\u7528\u6765\u5b58\u653e\u53ef\u6267\u884c\u6587\u4ef6\u7684\u64cd\u4f5c\u6307\u4ee4\uff0c\u4e5f\u5c31\u662f\u8bf4\u662f\u5b83\u662f\u53ef\u6267\u884c\u7a0b\u5e8f\u5728\u5185\u5b58\u4e2d\u7684\u955c\u50cf\u3002\u4ee3\u7801\u6bb5\u9700\u8981\u9632\u6b62\u5728\u8fd0\u884c\u65f6\u88ab\u975e\u6cd5\u4fee\u6539\uff0c\u6240\u4ee5\u53ea\u51c6\u8bb8\u8bfb\u53d6\u64cd\u4f5c\uff0c\u800c\u4e0d\u5141\u8bb8\u5199\u5165(\u4fee\u6539)\u64cd\u4f5c\u2014\u2014\u5b83\u662f\u4e0d\u53ef\u5199\u7684\u3002<\/p>\n \u6570\u636e\u6bb5\uff1a\u6570\u636e\u6bb5\u7528\u6765\u5b58\u653e\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\u5df2\u521d\u59cb\u5316\u5168\u5c40\u53d8\u91cf\uff0c\u6362\u53e5\u8bdd\u8bf4\u5c31\u662f\u5b58\u653e\u7a0b\u5e8f\u9759\u6001\u5206\u914d[1]\u7684\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n BSS\u6bb5[2]\uff1aBSS\u6bb5\u5305\u542b\u4e86\u7a0b\u5e8f\u4e2d\u672a\u521d\u59cb\u5316\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u5728\u5185\u5b58\u4e2d bss\u6bb5\u5168\u90e8\u7f6e\u96f6\u3002<\/p>\n \u5806(heap)\uff1a\u5806\u662f\u7528\u4e8e\u5b58\u653e\u8fdb\u7a0b\u8fd0\u884c\u4e2d\u88ab\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58\u6bb5\uff0c\u5b83\u7684\u5927\u5c0f\u5e76\u4e0d\u56fa\u5b9a\uff0c\u53ef\u52a8\u6001\u6269\u5f20\u6216\u7f29\u51cf\u3002\u5f53\u8fdb\u7a0b\u8c03\u7528malloc\u7b49\u51fd\u6570\u5206\u914d\u5185\u5b58\u65f6\uff0c\u65b0\u5206\u914d\u7684\u5185\u5b58\u5c31\u88ab\u52a8\u6001\u6dfb\u52a0\u5230\u5806\u4e0a(\u5806\u88ab\u6269\u5f20);\u5f53\u5229\u7528free\u7b49\u51fd\u6570\u91ca\u653e\u5185\u5b58\u65f6\uff0c\u88ab\u91ca\u653e\u7684\u5185\u5b58\u4ece\u5806\u4e2d\u88ab\u5254\u9664(\u5806\u88ab\u7f29\u51cf)<\/p>\n \u6808\uff1a\u6808\u662f\u7528\u6237\u5b58\u653e\u7a0b\u5e8f\u4e34\u65f6\u521b\u5efa\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u4e5f\u5c31\u662f\u8bf4\u6211\u4eec\u51fd\u6570\u62ec\u5f27\u201c{}\u201d\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf(\u4f46\u4e0d\u5305\u62ecstatic\u58f0\u660e\u7684\u53d8\u91cf\uff0cstatic\u610f\u5473\u7740\u5728\u6570\u636e\u6bb5\u4e2d\u5b58\u653e\u53d8\u91cf)\u3002\u9664\u6b64\u4ee5\u5916\uff0c\u5728\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u5176\u53c2\u6570\u4e5f\u4f1a\u88ab\u538b\u5165\u53d1\u8d77\u8c03\u7528\u7684\u8fdb\u7a0b\u6808\u4e2d\uff0c\u5e76\u4e14\u5f85\u5230\u8c03\u7528\u7ed3\u675f\u540e\uff0c\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e5f\u4f1a\u88ab\u5b58\u653e\u56de\u6808\u4e2d\u3002\u7531\u4e8e\u6808\u7684\u5148\u8fdb\u5148\u51fa\u7279\u70b9\uff0c\u6240\u4ee5\u6808\u7279\u522b\u65b9\u4fbf\u7528\u6765\u4fdd\u5b58\/\u6062\u590d\u8c03\u7528\u73b0\u573a\u3002\u4ece\u8fd9\u4e2a\u610f\u4e49\u4e0a\u8bb2\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u5806\u6808\u770b\u6210\u4e00\u4e2a\u5bc4\u5b58\u3001\u4ea4\u6362\u4e34\u65f6\u6570\u636e\u7684\u5185\u5b58\u533a\u3002<\/p>\n \u8fdb\u7a0b\u5982\u4f55\u7ec4\u7ec7\u8fd9\u4e9b\u533a\u57df?<\/strong><\/span><\/div>\n \u4e0a\u8ff0\u51e0\u79cd\u5185\u5b58\u533a\u57df\u4e2d\u6570\u636e\u6bb5\u3001BSS\u548c\u5806\u901a\u5e38\u662f\u88ab\u8fde\u7eed\u5b58\u50a8\u7684\u2014\u2014\u5185\u5b58\u4f4d\u7f6e\u4e0a\u662f\u8fde\u7eed\u7684\uff0c\u800c\u4ee3\u7801\u6bb5\u548c\u6808\u5f80\u5f80\u4f1a\u88ab\u72ec\u7acb\u5b58\u653e\u3002\u6709\u8da3\u7684\u662f\uff0c\u5806\u548c\u6808\u4e24\u4e2a\u533a\u57df\u5173\u7cfb\u5f88\u201c\u66a7\u6627\u201d\uff0c\u4ed6\u4eec\u4e00\u4e2a\u5411\u4e0b\u201c\u957f\u201d(i386\u4f53\u7cfb\u7ed3\u6784\u4e2d\u6808\u5411\u4e0b\u3001\u5806\u5411\u4e0a)\uff0c\u4e00\u4e2a\u5411\u4e0a\u201c\u957f\u201d\uff0c\u76f8\u5bf9\u800c\u751f\u3002\u4f46\u4f60\u4e0d\u5fc5\u62c5\u5fc3\u4ed6\u4eec\u4f1a\u78b0\u5934\uff0c\u56e0\u4e3a\u4ed6\u4eec\u4e4b\u95f4\u95f4\u9694\u5f88\u5927(\u5230\u5e95\u5927\u5230\u591a\u5c11\uff0c\u4f60\u53ef\u4ee5\u4ece\u4e0b\u9762\u7684\u4f8b\u5b50\u7a0b\u5e8f\u8ba1\u7b97\u4e00\u4e0b)\uff0c\u7edd\u5c11\u6709\u673a\u4f1a\u80fd\u78b0\u5230\u4e00\u8d77\u3002<\/p>\n \u5b9e\u5b58\u3001\u865a\u5b58<\/strong><\/div>\n \u5b9e\u5b58\uff1a\u8fdb\u7a0b\u5206\u914d\u7684\u3001\u52a0\u8f7d\u5230\u4e3b\u5b58\u4e2d\u7684\u5185\u5b58\u3002\u5305\u542b\u6765\u81ea\u5171\u4eab\u5e93\u7684\u5185\u5b58\uff0c\u53ea\u8981\u8fd9\u4e9b\u5e93\u5360\u7528\u7684\u9875\u6846\u8fd8\u5728\u4e3b\u5b58\u4e2d\uff0c\u4e5f\u5305\u542b\u6240\u6709\u6b63\u5728\u4f7f\u7528\u7684\u5806\u6808\u548c\u5806\u5185\u5b58\u3002\u53ef\u4ee5\u901a\u8fc7 ps -o rss \u67e5\u770b\u8fdb\u7a0b\u7684\u5b9e\u5b58\u5927\u5c0f\u3002<\/p>\n \u865a\u5b58\uff1a\u5305\u542b\u8fdb\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u7684\u6240\u6709\u5185\u5b58\uff0c\u5305\u542b\u88ab\u6362\u51fa\u3001\u5df2\u7ecf\u5206\u914d\u4f46\u8fd8\u672a\u4f7f\u7528\u7684\u5185\u5b58\uff0c\u4ee5\u53ca\u6765\u81ea\u5171\u4eab\u5e93\u7684\u5185\u5b58\u3002\u53ef\u4ee5\u901a\u8fc7 ps -o vsz \u67e5\u770b\u8fdb\u7a0b\u7684\u865a\u5b58\u5927\u5c0f\u3002<\/p>\n \u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u8fdb\u7a0bA\u5177\u6709500K\u4e8c\u8fdb\u5236\u6587\u4ef6\u5e76\u4e14\u94fe\u63a5\u52302500K\u5171\u4eab\u5e93\uff0c\u5219\u5177\u6709200K\u7684\u5806\u6808\/\u5806\u5206\u914d\uff0c\u5176\u4e2d100K\u5b9e\u9645\u4e0a\u5728\u5185\u5b58\u4e2d(\u5176\u4f59\u662f\u4ea4\u6362\u6216\u672a\u4f7f\u7528)\uff0c\u5e76\u4e14\u5b83\u5b9e\u9645\u4e0a\u53ea\u52a0\u8f7d\u4e861000K\u7684\u5171\u4eab\u5e93\u7136\u540e\u662f400K\u81ea\u5df1\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\uff1a<\/p>\n \r\nRSS: 400K + 1000K + 100K = 1500K \r\nVSZ: 500K + 2500K + 200K = 3200K \r\n<\/pre>\n |