{"id":27289,"date":"2025-09-26T08:42:42","date_gmt":"2025-09-26T00:42:42","guid":{"rendered":"https:\/\/aif.amtbbs.org\/?p=27289"},"modified":"2025-09-26T08:42:42","modified_gmt":"2025-09-26T00:42:42","slug":"malloc%e5%ba%95%e5%b1%82%e6%9c%ba%e5%88%b6%ef%bc%9abrk%e4%b8%8e-mmap%e5%a6%82%e4%bd%95%e9%80%89%e5%86%85%e5%ad%98%e5%88%86%e9%85%8d%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/aif.amtbbs.org\/index.php\/2025\/09\/26\/27289\/","title":{"rendered":"malloc\u5e95\u5c42\u673a\u5236\uff1abrk\u4e0e mmap\u5982\u4f55\u9009\u5185\u5b58\u5206\u914d\uff1f"},"content":{"rendered":"<div class=\"article-desc\">\u5728\u6df1\u5165\u63a2\u8ba8brk\u4e0emmap\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u6765\u660e\u786e\u4e00\u4e2a\u6982\u5ff5\uff1a\u5728 Linux \u7cfb\u7edf\u4e2d\uff0c\u5185\u5b58\u5206\u914d\u7684\u6838\u5fc3\u7cfb\u7edf\u8c03\u7528\u4e3b\u8981\u5c31\u662fbrk\u548cmmap \uff0c\u5b83\u4eec\u662f\u8fdb\u7a0b\u83b7\u53d6\u5185\u5b58\u7684\u4e24\u79cd\u5173\u952e\u65b9\u5f0f\uff0c\u5c31\u50cf\u7a0b\u5e8f\u733f\u4f38\u5411\u5185\u5b58\u7684\u4e24\u53ea\u624b\uff0c\u5404\u81ea\u6709\u7740\u72ec\u7279\u7684\u5206\u5de5\u548c\u6280\u5de7\u3002<\/div>\n<div id=\"postspictures\" class=\"article-content\">\n<div id=\"container\" class=\"container am-engine\" data-v-01a18e2f=\"\" data-element=\"root\">\n<p>brk \u4e0e mmap \u4f5c\u4e3a\u5185\u5b58\u5206\u914d\u7684\u300c\u53cc\u5f15\u64ce\u300d\uff0c\u5404\u81ea\u62e5\u6709\u72ec\u7279\u7684\u8fd0\u884c\u673a\u5236\u548c\u9002\u7528\u573a\u666f\u3002brk \u901a\u8fc7\u7ebf\u6027\u6269\u5c55\u5806\u533a\uff0c\u5728\u5c0f\u989d\u5185\u5b58\u5206\u914d\u573a\u666f\u4e2d\u8868\u73b0\u51fa\u8f7b\u5feb\u7075\u6d3b\u7684\u7279\u70b9\uff0c\u80fd\u591f\u9ad8\u6548\u5730\u6ee1\u8db3\u7a0b\u5e8f\u5bf9\u5c0f\u5757\u5185\u5b58\u7684\u9891\u7e41\u9700\u6c42\u3002\u800c mmap \u5219\u51ed\u501f\u5176\u72ec\u7acb\u6620\u5c04\u7684\u7279\u6027\uff0c\u5728\u5927\u5757\u5185\u5b58\u5206\u914d\u4ee5\u53ca\u9700\u8981\u5171\u4eab\u5185\u5b58\u3001\u6587\u4ef6\u6620\u5c04\u7684\u590d\u6742\u573a\u666f\u4e2d\u5c55\u73b0\u51fa\u5f3a\u5927\u7684\u7a33\u5b9a\u6027\u548c\u7075\u6d3b\u6027 \u3002\u7406\u89e3\u8fd9\u4e24\u79cd\u5185\u5b58\u5206\u914d\u65b9\u5f0f\u7684\u5e95\u5c42\u673a\u5236\uff0c\u662f\u5f00\u53d1\u8005\u4f18\u5316\u7a0b\u5e8f\u6027\u80fd\u3001\u6392\u67e5\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u7684\u5173\u952e\u3002<\/p>\n<p>\u5728\u5de5\u7a0b\u5b9e\u8df5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5404\u79cd\u57fa\u4e8e brk \u548c mmap \u7684\u4f18\u5316\u7b56\u7565\u548c\u5185\u5b58\u7ba1\u7406\u6280\u672f\u3002\u5185\u5b58\u6c60\u6280\u672f\u901a\u8fc7\u9884\u5148\u5206\u914d\u548c\u590d\u7528\u5185\u5b58\uff0c\u51cf\u5c11\u4e86\u7cfb\u7edf\u8c03\u7528\u7684\u6b21\u6570\uff0c\u63d0\u9ad8\u4e86\u5185\u5b58\u5206\u914d\u7684\u6548\u7387\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u9ad8\u9891\u5c0f\u989d\u5185\u5b58\u5206\u914d\u7684\u573a\u666f \u3002\u788e\u7247\u6574\u7406\u6280\u672f\u5219\u901a\u8fc7\u5b9a\u671f\u6574\u7406\u5185\u5b58\u788e\u7247\uff0c\u63d0\u9ad8\u4e86\u5185\u5b58\u7684\u5229\u7528\u7387\uff0c\u51cf\u5c11\u4e86\u5185\u5b58\u788e\u7247\u5316\u5bf9\u7a0b\u5e8f\u6027\u80fd\u7684\u5f71\u54cd \u3002\u8fd9\u4e9b\u6280\u672f\u7684\u5e94\u7528\uff0c\u8fdb\u4e00\u6b65\u5c55\u793a\u4e86 brk \u548c mmap \u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u7684\u91cd\u8981\u6027\u548c\u5b9e\u7528\u6027 \u3002<\/p>\n<h2>\u4e00\u3001\u5185\u5b58\u5206\u914d\u7684\u300c\u53cc\u5f15\u64ce\u300d\uff1abrk \u4e0e mmap \u6838\u5fc3\u539f\u7406<\/h2>\n<p>\u5728\u6df1\u5165\u63a2\u8ba8brk\u4e0emmap\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u6765\u660e\u786e\u4e00\u4e2a\u6982\u5ff5\uff1a\u5728 Linux \u7cfb\u7edf\u4e2d\uff0c\u5185\u5b58\u5206\u914d\u7684\u6838\u5fc3\u7cfb\u7edf\u8c03\u7528\u4e3b\u8981\u5c31\u662fbrk\u548cmmap \uff0c\u5b83\u4eec\u662f\u8fdb\u7a0b\u83b7\u53d6\u5185\u5b58\u7684\u4e24\u79cd\u5173\u952e\u65b9\u5f0f\uff0c\u5c31\u50cf\u7a0b\u5e8f\u733f\u4f38\u5411\u5185\u5b58\u7684\u4e24\u53ea\u624b\uff0c\u5404\u81ea\u6709\u7740\u72ec\u7279\u7684\u5206\u5de5\u548c\u6280\u5de7\u3002\u63a5\u4e0b\u6765\uff0c\u5c31\u8ba9\u6211\u4eec\u63ed\u5f00\u5b83\u4eec\u795e\u79d8\u7684\u9762\u7eb1\uff0c\u770b\u770b\u5b83\u4eec\u662f\u5982\u4f55\u5728\u5185\u5b58\u7684\u821e\u53f0\u4e0a\u7fe9\u7fe9\u8d77\u821e\u7684\u3002<\/p>\n<h3>1.1 brk\uff1a\u5806\u533a\u7684\u7ebf\u6027\u6269\u5c55\u5f15\u64ce<\/h3>\n<p>brk \u7cfb\u7edf\u8c03\u7528\u662f\u8fdb\u7a0b\u5806\u5185\u5b58\u7ba1\u7406\u7684\u91cd\u8981\u5de5\u5177\uff0c\u5176\u6838\u5fc3\u673a\u5236\u5728\u4e8e\u901a\u8fc7\u79fb\u52a8\u8fdb\u7a0b\u5806\u9876\u6307\u9488\uff08program break\uff09\u6765\u52a8\u6001\u6269\u5c55\u5185\u5b58\u7a7a\u95f4\u3002\u8fdb\u7a0b\u542f\u52a8\u65f6\uff0c\u5806\u533a\u4f4d\u4e8e\u6570\u636e\u6bb5\u672b\u7aef\uff0c\u968f\u7740\u7a0b\u5e8f\u8fd0\u884c\uff0c\u5f53\u9700\u8981\u66f4\u591a\u5185\u5b58\u65f6\uff0cbrk \u4f1a\u5c06\u5806\u9876\u6307\u9488\u5411\u9ad8\u5730\u5740\u79fb\u52a8\uff0c\u65b0\u5206\u914d\u7684\u5185\u5b58\u4fbf\u7d27\u63a5\u5728\u5df2\u6709\u5806\u5185\u5b58\u4e4b\u540e\uff0c\u5f62\u6210\u8fde\u7eed\u7684\u7ebf\u6027\u533a\u57df\u3002\u8fd9\u4e00\u8fc7\u7a0b\u5c31\u597d\u6bd4\u5728\u73b0\u6709\u571f\u5730\u4e0a\u8fdb\u884c\u6269\u5efa\uff0c\u4e0d\u65ad\u62d3\u5c55\u53ef\u4f7f\u7528\u7684\u7a7a\u95f4\u3002<\/p>\n<p><img data-dominant-color=\"f2f1e5\" data-has-transparency=\"false\" style=\"--dominant-color: #f2f1e5;\" loading=\"lazy\" decoding=\"async\" class=\"not-transparent alignnone size-full wp-image-27290\" src=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2025\/09\/92097c8185f1040d5f3780f06429db6f9db3eb.jpg\" width=\"720\" height=\"542\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" srcset=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2025\/09\/92097c8185f1040d5f3780f06429db6f9db3eb.jpg 720w, https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2025\/09\/92097c8185f1040d5f3780f06429db6f9db3eb-300x226.jpg 300w, https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2025\/09\/92097c8185f1040d5f3780f06429db6f9db3eb-150x113.jpg 150w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><span class=\"data-image-title\">\u56fe\u7247<\/span><\/p>\n<p>\u4ece\u5185\u5b58\u5206\u914d\u7684\u5b9e\u9645\u8fc7\u7a0b\u6765\u770b\uff0cbrk \u6709\u7740\u72ec\u7279\u7684\u4f18\u52bf\u548c\u7279\u6027\u3002\u9996\u5148\uff0cbrk \u5728\u8fdb\u884c\u5185\u5b58\u5206\u914d\u65f6\uff0c\u4ec5\u4fee\u6539\u865a\u62df\u5185\u5b58\u8fb9\u754c\uff0c\u5e76\u4e0d\u4f1a\u7acb\u5373\u5206\u914d\u7269\u7406\u5185\u5b58\u3002\u53ea\u6709\u5f53\u8fdb\u7a0b\u9996\u6b21\u8bbf\u95ee\u65b0\u5206\u914d\u7684\u865a\u62df\u5185\u5b58\u533a\u57df\u65f6\uff0c\u624d\u4f1a\u89e6\u53d1\u7f3a\u9875\u4e2d\u65ad\uff0c\u6b64\u65f6\u64cd\u4f5c\u7cfb\u7edf\u624d\u4f1a\u771f\u6b63\u5206\u914d\u7269\u7406\u5185\u5b58\uff0c\u5e76\u5efa\u7acb\u865a\u62df\u5185\u5b58\u4e0e\u7269\u7406\u5185\u5b58\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\u3002\u8fd9\u79cd\u6309\u9700\u5206\u914d\u7684\u7b56\u7565\u6709\u6548\u5730\u907f\u514d\u4e86\u5185\u5b58\u7684\u63d0\u524d\u6d6a\u8d39\uff0c\u63d0\u9ad8\u4e86\u5185\u5b58\u4f7f\u7528\u6548\u7387 \u3002<\/p>\n<p>\u5176\u6b21\uff0cbrk \u5206\u914d\u7684\u5185\u5b58\u662f\u8fde\u7eed\u7684\uff0c\u8fd9\u5728\u8bb8\u591a\u573a\u666f\u4e0b\u90fd\u6781\u4e3a\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e9b\u9700\u8981\u9891\u7e41\u8bfb\u5199\u5927\u5757\u8fde\u7eed\u6570\u636e\u7684\u5e94\u7528\uff0c\u5982\u6570\u636e\u5e93\u7f13\u5b58\uff0c\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6570\u636e\u8bbf\u95ee\u901f\u5ea6\uff0c\u51cf\u5c11\u7f13\u5b58\u672a\u547d\u4e2d\u7684\u6b21\u6570\uff0c\u56e0\u4e3a\u8fde\u7eed\u5185\u5b58\u6709\u5229\u4e8e\u63d0\u9ad8\u7f13\u5b58\u547d\u4e2d\u7387\uff0c\u4f7f\u5f97\u6570\u636e\u80fd\u591f\u66f4\u9ad8\u6548\u5730\u5728\u5185\u5b58\u4e0e\u7f13\u5b58\u4e4b\u95f4\u4f20\u8f93 \u3002\u6b64\u5916\uff0cglibc \u7684 sbrk \u51fd\u6570\u5bf9 brk \u8fdb\u884c\u4e86\u5c01\u88c5\uff0c\u63d0\u4f9b\u4e86\u66f4\u4e3a\u4fbf\u6377\u7684\u589e\u91cf\u5206\u914d\u63a5\u53e3\u3002\u901a\u8fc7 sbrk\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u76f4\u63a5\u6307\u5b9a\u589e\u52a0\u6216\u51cf\u5c11\u7684\u5185\u5b58\u5927\u5c0f\uff0c\u800c\u65e0\u9700\u624b\u52a8\u8ba1\u7b97\u65b0\u7684\u5806\u9876\u5730\u5740\uff0c\u5927\u5927\u7b80\u5316\u4e86\u5185\u5b58\u64cd\u4f5c\u6d41\u7a0b\u3002<\/p>\n<p>\u4e0d\u8fc7\uff0cbrk \u4e5f\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\u3002\u7531\u4e8e\u5176\u5206\u914d\u7684\u5185\u5b58\u4f9d\u8d56\u5806\u9876\u6307\u9488\u7684\u79fb\u52a8\uff0c\u91ca\u653e\u5185\u5b58\u65f6\u9700\u6309\u987a\u5e8f\u8fdb\u884c\uff0c\u5373\u9ad8\u5730\u5740\u7684\u5185\u5b58\u5148\u91ca\u653e\uff0c\u4f4e\u5730\u5740\u7684\u5185\u5b58\u540e\u91ca\u653e\u3002\u8fd9\u5c31\u5bfc\u81f4\u5982\u679c\u4e2d\u95f4\u90e8\u5206\u7684\u5185\u5b58\u88ab\u91ca\u653e\uff0c\u4f1a\u5f62\u6210\u5185\u5b58\u7a7a\u6d1e\uff0c\u800c\u8fd9\u4e9b\u7a7a\u6d1e\u5728\u540e\u7eed\u7684\u5185\u5b58\u5206\u914d\u4e2d\u53ef\u80fd\u65e0\u6cd5\u88ab\u5145\u5206\u5229\u7528\uff0c\u4ece\u800c\u4ea7\u751f\u5185\u5b58\u788e\u7247\u5316\u95ee\u9898\u3002\u968f\u7740\u7a0b\u5e8f\u4e0d\u65ad\u5730\u8fdb\u884c\u5185\u5b58\u5206\u914d\u548c\u91ca\u653e\u64cd\u4f5c\uff0c\u5185\u5b58\u788e\u7247\u5316\u53ef\u80fd\u4f1a\u8d8a\u6765\u8d8a\u4e25\u91cd\uff0c\u6700\u7ec8\u5bfc\u81f4\u5373\u4f7f\u5806\u533a\u8fd8\u6709\u8db3\u591f\u7684\u7a7a\u95f2\u5185\u5b58\uff0c\u4f46\u7531\u4e8e\u788e\u7247\u7684\u5b58\u5728\uff0c\u65e0\u6cd5\u6ee1\u8db3\u8f83\u5927\u5185\u5b58\u5757\u7684\u5206\u914d\u9700\u6c42\uff0c\u5f71\u54cd\u7a0b\u5e8f\u7684\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<p><strong>\u8fd9\u79cd\u5206\u914d\u65b9\u5f0f\u6709\u7740\u81ea\u5df1\u72ec\u7279\u7684\u7279\u70b9\uff1a<\/strong><\/p>\n<ul data-id=\"u738a58b-fld7D4gV\">\n<li data-id=\"ld70c578-h3QfYXFa\">\u865a\u62df\u5185\u5b58\u4e0e\u7269\u7406\u5185\u5b58\u7684\u5ef6\u8fdf\u7ed1\u5b9a\uff1abrk\u4ec5\u4fee\u6539\u865a\u62df\u5185\u5b58\u8fb9\u754c\uff0c\u5e76\u4e0d\u4f1a\u7acb\u5373\u5206\u914d\u7269\u7406\u5185\u5b58\u3002\u53ea\u6709\u5f53\u7a0b\u5e8f\u9996\u6b21\u8bbf\u95ee\u8fd9\u7247\u65b0\u5206\u914d\u7684\u865a\u62df\u5185\u5b58\u65f6\uff0c\u624d\u4f1a\u89e6\u53d1\u7f3a\u9875\u4e2d\u65ad\uff0c\u64cd\u4f5c\u7cfb\u7edf\u8fd9\u65f6\u624d\u4f1a\u771f\u6b63\u5206\u914d\u7269\u7406\u5185\u5b58\u7ed9\u8fdb\u7a0b\u3002\u8fd9\u5c31\u50cf\u662f\u4f60\u5148\u89c4\u5212\u597d\u4e86\u65b0\u623f\u95f4\u7684\u4f4d\u7f6e\uff08\u8bbe\u7f6e\u865a\u62df\u5185\u5b58\u8fb9\u754c\uff09\uff0c\u4f46\u8fd8\u6ca1\u771f\u6b63\u5f00\u59cb\u780c\u5899\uff08\u5206\u914d\u7269\u7406\u5185\u5b58\uff09\uff0c\u76f4\u5230\u6709\u4eba\u8981\u4f4f\u8fdb\u53bb\uff08\u9996\u6b21\u8bbf\u95ee\uff09\u624d\u5f00\u59cb\u52a8\u5de5\u3002<\/li>\n<li data-id=\"ld70c578-8BKMKbgg\">\u5806\u533a\u5185\u5b58\u7684\u987a\u5e8f\u91ca\u653e\uff1a\u5206\u914d\u7684\u5185\u5b58\u5c5e\u4e8e\u8fdb\u7a0b\u5806\u533a\uff0c\u5728\u91ca\u653e\u7684\u65f6\u5019\u9700\u8981\u6309\u987a\u5e8f\u6765\uff0c\u540e\u5206\u914d\u7684\u5148\u91ca\u653e\u3002\u5c31\u597d\u6bd4\u4f60\u6269\u5efa\u7684\u623f\u95f4\uff0c\u4f60\u8981\u62c6\u9664\u7684\u65f6\u5019\uff0c\u5f97\u4ece\u6700\u540e\u5efa\u7684\u90a3\u95f4\u5f00\u59cb\u62c6\u3002<\/li>\n<li data-id=\"ld70c578-V2bliflg\">\u5c01\u88c5\u4e0e\u589e\u91cf\u5206\u914d\uff1a\u5728 glibc \u4e2d\uff0csbrk\u51fd\u6570\u662fbrk\u7684\u5c01\u88c5\uff0c\u5b83\u63d0\u4f9b\u4e86\u589e\u91cf\u5206\u914d\u63a5\u53e3\u3002\u4f8b\u5982\uff0csbrk(n)\u4f1a\u5c06\u5806\u9876\u6307\u9488\u79fb\u52a8n\u4e2a\u5b57\u8282\uff0c\u5b9e\u73b0\u589e\u91cf\u5f0f\u7684\u5185\u5b58\u5206\u914d\u3002\u8fd9\u5c31\u50cf\u662f\u4f60\u6bcf\u6b21\u53ef\u4ee5\u4e00\u5c0f\u90e8\u5206\u4e00\u5c0f\u90e8\u5206\u5730\u6269\u5efa\u4f60\u7684\u623f\u5b50\uff0c\u975e\u5e38\u7075\u6d3b\u3002<\/li>\n<\/ul>\n<p>brk\u8fd9\u79cd\u65b9\u5f0f\u7279\u522b\u9002\u5408\u5c0f\u5757\u5185\u5b58\u7684\u5feb\u901f\u5206\u914d\uff0c\u6bd4\u5982\u51e0 KB \u5230\u51e0\u5341 KB \u7684\u5185\u5b58\u5206\u914d\uff0c\u56e0\u4e3a\u5b83\u64cd\u4f5c\u7b80\u5355\uff0c\u53ea\u9700\u8981\u79fb\u52a8\u4e00\u4e0b\u5806\u9876\u6307\u9488\u5c31\u53ef\u4ee5\u5b8c\u6210\u5185\u5b58\u5206\u914d\u7684 \u201c\u89c4\u5212\u201d\uff0c\u901f\u5ea6\u975e\u5e38\u5feb\u3002\u4f46\u5b83\u4e5f\u6709\u81ea\u5df1\u7684\u5c40\u9650\u6027\uff0c\u6bd4\u5982\u5bb9\u6613\u4ea7\u751f\u5185\u5b58\u788e\u7247\uff0c\u5c31\u50cf\u4f60\u4e0d\u65ad\u5730\u5728\u623f\u5b50\u540e\u9762\u6269\u5efa\u5c0f\u623f\u95f4\uff0c\u62c6\u4e86\u53c8\u5efa\uff0c\u5efa\u4e86\u53c8\u62c6\uff0c\u6700\u540e\u53ef\u80fd\u4f1a\u5269\u4e0b\u5f88\u591a\u4e0d\u89c4\u5219\u7684\u5c0f\u5757\u7a7a\u5730\uff08\u5185\u5b58\u788e\u7247\uff09\uff0c\u5f88\u96be\u518d\u5229\u7528\u8d77\u6765\u3002<\/p>\n<h3>1.2 mmap\uff1a\u865a\u62df\u7a7a\u95f4\u7684\u72ec\u7acb\u6620\u5c04\u5668<\/h3>\n<p>mmap \u7cfb\u7edf\u8c03\u7528\u5219\u5f00\u8f9f\u4e86\u53e6\u4e00\u79cd\u5185\u5b58\u5206\u914d\u7684\u9014\u5f84\uff0c\u5b83\u5728\u5806\u4e0e\u6808\u4e4b\u95f4\u7684 \u201c\u6587\u4ef6\u6620\u5c04\u533a\u201d \u521b\u5efa\u72ec\u7acb\u7684\u5185\u5b58\u533a\u57df\u3002\u5f53\u8c03\u7528 mmap \u65f6\uff0c\u8fdb\u7a0b\u53ef\u4ee5\u6307\u5b9a\u6620\u5c04\u7684\u957f\u5ea6\u3001\u6743\u9650\uff08\u5982\u8bbe\u7f6e\u4e3a MAP_ANONYMOUS \u8868\u793a\u533f\u540d\u6620\u5c04\uff0c\u4e0d\u4e0e\u4efb\u4f55\u6587\u4ef6\u5173\u8054\uff09\u7b49\u53c2\u6570\uff0c\u5185\u6838\u4f1a\u636e\u6b64\u751f\u6210\u72ec\u7acb\u7684\u5185\u5b58\u7ba1\u7406\u5355\u5143\uff08vm_area_struct\uff09\u3002\u8fd9\u4e2a\u5185\u5b58\u7ba1\u7406\u5355\u5143\u5c31\u50cf\u662f\u4e00\u4e2a\u72ec\u7acb\u7684 \u201c\u5c0f\u623f\u95f4\u201d\uff0c\u4e0e\u5806\u533a\u7684\u5185\u5b58\u7ba1\u7406\u76f8\u4e92\u72ec\u7acb\uff0c\u62e5\u6709\u81ea\u5df1\u7684\u5730\u5740\u7a7a\u95f4\u548c\u6743\u9650\u8bbe\u7f6e \u3002\u53c2\u8003\u8fd9\u7bc7\u300a\u8d85\u786c\u6838\uff0c\u57fa\u4e8emmap\u548c\u96f6\u62f7\u8d1d\u5b9e\u73b0\u9ad8\u6548\u7684\u5185\u5b58\u5171\u4eab\u300b<\/p>\n<p><img data-dominant-color=\"f0edd9\" data-has-transparency=\"false\" style=\"--dominant-color: #f0edd9;\" loading=\"lazy\" decoding=\"async\" class=\"not-transparent alignnone size-full wp-image-27291\" src=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2025\/09\/2913df877ab3c11dacb36618e30a31ef434ced.jpg\" width=\"720\" height=\"583\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" srcset=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2025\/09\/2913df877ab3c11dacb36618e30a31ef434ced.jpg 720w, https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2025\/09\/2913df877ab3c11dacb36618e30a31ef434ced-300x243.jpg 300w, https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2025\/09\/2913df877ab3c11dacb36618e30a31ef434ced-150x121.jpg 150w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><span class=\"data-image-title\">\u56fe\u7247<\/span><\/p>\n<p>mmap \u7684\u72ec\u7279\u4f18\u52bf\u4f7f\u5176\u5728\u7279\u5b9a\u573a\u666f\u4e0b\u8868\u73b0\u51fa\u8272\u3002\u4e00\u65b9\u9762\uff0cmmap \u652f\u6301\u975e\u8fde\u7eed\u5185\u5b58\u5206\u914d\uff0c\u8fd9\u5bf9\u4e8e\u9700\u8981\u5206\u914d\u5927\u5757\u5185\u5b58\u7684\u573a\u666f\u5c24\u4e3a\u91cd\u8981\u3002\u5f53\u7a0b\u5e8f\u9700\u8981\u7533\u8bf7\u4e00\u5757\u8f83\u5927\u7684\u5185\u5b58\u65f6\uff0cmmap \u53ef\u4ee5\u76f4\u63a5\u5728\u6587\u4ef6\u6620\u5c04\u533a\u627e\u5230\u5408\u9002\u7684\u7a7a\u95f2\u533a\u57df\u8fdb\u884c\u5206\u914d\uff0c\u800c\u4e0d\u53d7\u5806\u533a\u8fde\u7eed\u5185\u5b58\u7684\u9650\u5236\uff0c\u907f\u514d\u4e86\u56e0\u5806\u533a\u8fde\u7eed\u6269\u5bb9\u5bfc\u81f4\u7684\u6574\u4f53\u81a8\u80c0\u548c\u5185\u5b58\u788e\u7247\u5316\u95ee\u9898\u3002\u5728\u5927\u6570\u636e\u5904\u7406\u3001\u56fe\u5f62\u6e32\u67d3\u7b49\u9700\u8981\u5927\u91cf\u5185\u5b58\u7684\u5e94\u7528\u4e2d\uff0cmmap \u80fd\u591f\u9ad8\u6548\u5730\u6ee1\u8db3\u5185\u5b58\u9700\u6c42\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u8fd0\u884c\u3002<\/p>\n<p>\u53e6\u4e00\u65b9\u9762\uff0cmmap \u5177\u6709\u96f6\u62f7\u8d1d\u7279\u6027\uff0c\u7279\u522b\u662f\u5728\u6587\u4ef6\u6620\u5c04\u573a\u666f\u4e2d\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u5c06\u6587\u4ef6\u5185\u5bb9\u6620\u5c04\u5230\u5185\u5b58\u4e2d\uff0c\u8fdb\u7a0b\u5bf9\u6587\u4ef6\u7684\u8bfb\u5199\u64cd\u4f5c\u5c31\u5982\u540c\u5bf9\u5185\u5b58\u7684\u8bfb\u5199\u4e00\u6837\uff0c\u51cf\u5c11\u4e86\u6570\u636e\u5728\u7528\u6237\u7a7a\u95f4\u548c\u5185\u6838\u7a7a\u95f4\u4e4b\u95f4\u7684\u642c\u8fd0\u5f00\u9500\u3002\u4f8b\u5982\uff0c\u5728\u6587\u4ef6\u4f20\u8f93\u8fc7\u7a0b\u4e2d\uff0c\u4f20\u7edf\u7684 read\/write \u65b9\u5f0f\u9700\u8981\u591a\u6b21\u6570\u636e\u62f7\u8d1d\u548c\u7cfb\u7edf\u8c03\u7528\uff0c\u800c mmap \u901a\u8fc7\u5185\u5b58\u6620\u5c04\uff0c\u8ba9\u6570\u636e\u76f4\u63a5\u5728\u5185\u5b58\u4e2d\u8fdb\u884c\u5904\u7406\uff0c\u5927\u5927\u63d0\u9ad8\u4e86\u6570\u636e\u4f20\u8f93\u6548\u7387\uff0c\u51cf\u5c11\u4e86 CPU \u7684\u8d1f\u8f7d \u3002\u6b64\u5916\uff0cmmap \u5206\u914d\u7684\u5185\u5b58\u91ca\u653e\u65f6\u4e0d\u4f9d\u8d56\u5806\u533a\u987a\u5e8f\uff0c\u901a\u8fc7 munmap \u51fd\u6570\u53ef\u4ee5\u72ec\u7acb\u5730\u5c06\u5185\u5b58\u5f52\u8fd8\u7ed9\u7cfb\u7edf\uff0c\u65e0\u8bba\u8be5\u5185\u5b58\u5757\u5728\u6620\u5c04\u533a\u57df\u4e2d\u7684\u4f4d\u7f6e\u5982\u4f55\uff0c\u90fd\u80fd\u76f4\u63a5\u91ca\u653e\uff0c\u8fd9\u4f7f\u5f97\u5185\u5b58\u7ba1\u7406\u66f4\u52a0\u7075\u6d3b\uff0c\u8fdb\u4e00\u6b65\u51cf\u5c11\u4e86\u5185\u5b58\u788e\u7247\u5316\u7684\u98ce\u9669\u3002<\/p>\n<p><strong>mmap\u7684\u7279\u70b9\u4e5f\u5341\u5206\u663e\u8457\uff1a<\/strong><\/p>\n<ul data-id=\"u738a58b-jjcRDILZ\">\n<li data-id=\"ld70c578-kJ6pQ4T0\">\u975e\u8fde\u7eed\u5185\u5b58\u5206\u914d\u4e0e\u5927\u5757\u5185\u5b58\u652f\u6301\uff1a\u5b83\u652f\u6301\u975e\u8fde\u7eed\u5185\u5b58\u5206\u914d\uff0c\u7279\u522b\u9002\u5408\u5927\u5757\u5185\u5b58\u7684\u5206\u914d\uff0c\u5728\u5927\u591a\u6570\u7cfb\u7edf\u4e2d\uff0c\u9ed8\u8ba4\u8d85\u8fc7 128KB \u7684\u5185\u5b58\u5206\u914d\u5c31\u4f1a\u4f7f\u7528mmap\u3002\u8fd9\u5c31\u50cf\u4f60\u8981\u5efa\u8bbe\u4e00\u4e2a\u5927\u578b\u5de5\u4e1a\u56ed\u533a\uff0c\u4e0d\u9700\u8981\u5728\u5df2\u6709\u7684\u57ce\u5e02\u533a\u57df\u91cc\u4e00\u70b9\u70b9\u62fc\u51d1\uff0c\u800c\u662f\u53ef\u4ee5\u76f4\u63a5\u5728\u90ca\u533a\u5212\u51fa\u4e00\u5927\u5757\u72ec\u7acb\u7684\u571f\u5730\u6765\u5efa\u8bbe\u3002<\/li>\n<li data-id=\"ld70c578-h0mZTZXY\">\u96f6\u62f7\u8d1d\u7279\u6027\u63d0\u5347\u6548\u7387\uff1ammap\u5177\u6709\u96f6\u62f7\u8d1d\u7279\u6027\uff0c\u5c24\u5176\u662f\u5728\u6587\u4ef6\u6620\u5c04\u573a\u666f\u4e2d\uff0c\u5b83\u51cf\u5c11\u4e86\u6570\u636e\u642c\u8fd0\u5f00\u9500\u3002\u6bd4\u5982\u5728\u8bfb\u53d6\u5927\u6587\u4ef6\u65f6\uff0c\u4f20\u7edf\u7684read\u65b9\u5f0f\u9700\u8981\u5c06\u6570\u636e\u4ece\u5185\u6838\u7f13\u51b2\u533a\u62f7\u8d1d\u5230\u7528\u6237\u7f13\u51b2\u533a\uff0c\u800cmmap\u53ef\u4ee5\u76f4\u63a5\u5c06\u6587\u4ef6\u6620\u5c04\u5230\u7528\u6237\u7a7a\u95f4\uff0c\u8fdb\u7a0b\u76f4\u63a5\u8bbf\u95ee\u6620\u5c04\u5185\u5b58\uff0c\u5c31\u50cf\u4f60\u53ef\u4ee5\u76f4\u63a5\u5728\u5de5\u4e1a\u56ed\u533a\u91cc\u5de5\u4f5c\uff0c\u800c\u4e0d\u9700\u8981\u628a\u5de5\u4e1a\u56ed\u533a\u7684\u4ea7\u54c1\u5148\u642c\u5230\u5bb6\u91cc\u518d\u8fdb\u884c\u5904\u7406\uff0c\u5927\u5927\u63d0\u9ad8\u4e86\u6548\u7387\u3002\u50cf Kafka \u5728 Broker \u8bfb\u5199 index \u6587\u4ef6\u65f6\u5c31\u7528\u4e86 mmap \u96f6\u590d\u5236\u6280\u672f\uff0c\u5927\u5927\u63d0\u5347\u4e86\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u3002<\/li>\n<li data-id=\"ld70c578-Q82XfChY\">\u72ec\u7acb\u91ca\u653e\u907f\u514d\u788e\u7247\u5316\uff1ammap\u901a\u8fc7munmap\u53ef\u4ee5\u72ec\u7acb\u91ca\u653e\u5185\u5b58\uff0c\u907f\u514d\u4e86\u5185\u5b58\u788e\u7247\u5316\u95ee\u9898\u3002\u6bcf\u4e2a\u901a\u8fc7mmap\u5206\u914d\u7684\u5185\u5b58\u533a\u57df\u5c31\u50cf\u4e00\u4e2a\u72ec\u7acb\u7684\u5c0f\u533a\uff0c\u4f60\u53ef\u4ee5\u968f\u65f6\u62c6\u9664\uff08\u91ca\u653e\uff09\u4efb\u4f55\u4e00\u4e2a\u5c0f\u533a\uff0c\u800c\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u533a\u57df\uff0c\u4e0d\u50cfbrk\u5206\u914d\u7684\u5185\u5b58\uff0c\u91ca\u653e\u65f6\u53d7\u5230\u987a\u5e8f\u9650\u5236\uff0c\u5bb9\u6613\u4ea7\u751f\u788e\u7247\u3002<\/li>\n<\/ul>\n<h3>1.3 brk\u4e0e mmap \u4e8c\u8005\u4e4b\u95f4\u533a\u522b<\/h3>\n<p>brk\uff08\u53ca sbrk\uff09\u548c mmap \u662f\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u4e24\u79cd\u5185\u5b58\u5206\u914d\u76f8\u5173\u7684\u7cfb\u7edf\u8c03\u7528\uff0c\u4e3b\u8981\u533a\u522b\u4f53\u73b0\u5728\u4f5c\u7528\u8303\u56f4\u3001\u9002\u7528\u573a\u666f\u3001\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f\u7b49\u65b9\u9762\u3002<\/p>\n<ul data-id=\"u738a58b-kJJHR0hk\">\n<li data-id=\"ld70c578-TK6JE3Ye\">\u64cd\u4f5c\u7684\u5185\u5b58\u533a\u57df\u4e0d\u540c\u3002brk\/sbrk \u4ec5\u7528\u4e8e\u8c03\u6574\u8fdb\u7a0b\u5806\u7684\u8fb9\u754c\uff0c\u901a\u8fc7\u4fee\u6539\u5806\u9876\u6307\u9488\u6539\u53d8\u5806\u7684\u5927\u5c0f\uff1b\u800c mmap \u7528\u4e8e\u5728\u865a\u62df\u5730\u5740\u7a7a\u95f4\u4e2d\u521b\u5efa\u72ec\u7acb\u7684\u5185\u5b58\u533a\u57df\uff0c\u8be5\u533a\u57df\u4e0e\u5806\u3001\u6808\u7b49\u73b0\u6709\u533a\u57df\u4e0d\u8fde\u7eed\u3002<\/li>\n<li data-id=\"ld70c578-sCGLgqUV\">\u9002\u7528\u7684\u5185\u5b58\u5927\u5c0f\u573a\u666f\u4e0d\u540c\u3002brk\/sbrk \u9002\u5408\u5c0f\u5757\u5185\u5b58\u5206\u914d\uff08\u5982\u51e0 KB \u5230\u51e0\u5341 KB\uff09\uff0c\u56e0\u5806\u5185\u5b58\u8fde\u7eed\uff0c\u5206\u914d\u91ca\u653e\u5f00\u9500\u5c0f\uff0c\u4e14\u91ca\u653e\u540e\u53ef\u88ab malloc \u7f13\u5b58\u590d\u7528\uff1bmmap \u66f4\u9002\u5408\u5927\u5757\u5185\u5b58\u5206\u914d\uff08\u901a\u5e38\u8d85 128KB\uff09\uff0c\u80fd\u907f\u514d\u5806\u5185\u5b58\u788e\u7247\uff0c\u4e14\u91ca\u653e\u540e\u76f4\u63a5\u5f52\u8fd8\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u3002<\/li>\n<li data-id=\"ld70c578-o2ocVB4C\">\u5185\u5b58\u91ca\u653e\u4e0e\u56de\u6536\u673a\u5236\u4e0d\u540c\u3002brk \u5206\u914d\u7684\u5806\u5185\u5b58\u91ca\u653e\u540e\uff0c\u4f1a\u8fdb\u5165 malloc \u7684\u7a7a\u95f2\u5217\u8868\u4f9b\u540e\u7eed\u590d\u7528\uff0c\u4ec5\u5f53\u5806\u9876\u8fde\u7eed\u7a7a\u95f2\u5185\u5b58\u8db3\u591f\u5927\u65f6\u624d\u5f52\u8fd8\u7ed9\u7cfb\u7edf\uff1bmmap \u5206\u914d\u7684\u533f\u540d\u5185\u5b58\u91ca\u653e\u65f6\uff0c\u901a\u8fc7 munmap \u76f4\u63a5\u5f52\u8fd8\u64cd\u4f5c\u7cfb\u7edf\uff0c\u4e0d\u518d\u88ab\u8fdb\u7a0b\u5360\u7528\u3002<\/li>\n<li data-id=\"ld70c578-tSl3Bq6L\">\u5185\u5b58\u5730\u5740\u8fde\u7eed\u6027\u4e0d\u540c\u3002brk \u5206\u914d\u7684\u5185\u5b58\u5c5e\u4e8e\u5806\u7684\u4e00\u90e8\u5206\uff0c\u5730\u5740\u8fde\u7eed\uff1bmmap \u5206\u914d\u7684\u5185\u5b58\u662f\u72ec\u7acb\u533a\u57df\uff0c\u5730\u5740\u4e0e\u5806\u3001\u6808\u7b49\u4e0d\u8fde\u7eed\uff0c\u5404 mmap \u533a\u57df\u95f4\u4e5f\u53ef\u80fd\u79bb\u6563\u3002<\/li>\n<\/ul>\n<h2>\u4e8c\u3001\u6838\u5fc3\u533a\u522b\uff1a\u4e3a\u4ec0\u4e48 malloc \u9009\u62e9\u300c\u5927\u5c0f\u6709\u522b\u300d\uff1f<\/h2>\n<p>\u901a\u8fc7\u5bf9brk\u548cmmap\u539f\u7406\u7684\u5256\u6790\uff0c\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u5230\u5b83\u4eec\u5404\u81ea\u7684\u7279\u70b9\u548c\u5de5\u4f5c\u65b9\u5f0f\u3002\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u6df1\u5165\u63a2\u8ba8\u5b83\u4eec\u4e4b\u95f4\u7684\u6838\u5fc3\u533a\u522b\uff0c\u4ee5\u53ca\u4e3a\u4ec0\u4e48malloc\u4f1a\u6839\u636e\u5185\u5b58\u5927\u5c0f\u6765\u9009\u62e9\u4e0d\u540c\u7684\u5e95\u5c42\u5b9e\u73b0\u673a\u5236\u3002\u8fd9\u5c31\u50cf\u662f\u4e00\u4e2a\u7cbe\u5bc6\u7684\u4eea\u5668\uff0c\u4e0d\u540c\u7684\u90e8\u4ef6\u5728\u4e0d\u540c\u7684\u60c5\u51b5\u4e0b\u53d1\u6325\u7740\u6700\u4f73\u7684\u4f5c\u7528\uff0c\u800cmalloc\u5c31\u50cf\u662f\u8fd9\u4e2a\u4eea\u5668\u7684\u667a\u80fd\u63a7\u5236\u7cfb\u7edf\uff0c\u6839\u636e\u4e0d\u540c\u7684 \u201c\u4efb\u52a1\u201d\uff08\u5185\u5b58\u5206\u914d\u9700\u6c42\uff09\u6765\u9009\u62e9\u6700\u5408\u9002\u7684 \u201c\u5de5\u5177\u201d\uff08brk\u6216mmap\uff09 \u3002<\/p>\n<h3>2.1 \u5185\u5b58\u5e03\u5c40\u4e0e\u788e\u7247\u5316\u5bf9\u6bd4<\/h3>\n<p>\u4ece\u5185\u5b58\u5e03\u5c40\u7684\u89d2\u5ea6\u6765\u770b\uff0cbrk\u548cmmap\u6709\u7740\u663e\u8457\u7684\u533a\u522b\u3002brk\u5206\u914d\u7684\u5185\u5b58\u4f4d\u4e8e\u5806\u533a\uff0c\u662f\u5728\u6570\u636e\u6bb5\u672b\u7aef\u8fdb\u884c\u7ebf\u6027\u6269\u5c55\u7684\uff0c\u5c31\u50cf\u4e00\u6761\u4e0d\u65ad\u5ef6\u4f38\u7684\u76f4\u7ebf\uff0c\u6240\u6709\u5206\u914d\u7684\u5185\u5b58\u5757\u7d27\u5bc6\u76f8\u8fde\uff0c\u4f9d\u8d56\u5806\u9876\u6307\u9488\u6765\u7ba1\u7406\u5185\u5b58\u7684\u8fb9\u754c\u3002\u8fd9\u79cd\u65b9\u5f0f\u5728\u5185\u5b58\u91ca\u653e\u65f6\uff0c\u5fc5\u987b\u6309\u7167\u5206\u914d\u7684\u987a\u5e8f\uff0c\u4ece\u9ad8\u5730\u5740\u7684\u5185\u5b58\u5757\u5f00\u59cb\u91ca\u653e\u3002\u8fd9\u5c31\u597d\u6bd4\u4f60\u5728\u4e66\u67b6\u4e0a\u4f9d\u6b21\u6446\u653e\u4e66\u7c4d\uff0c\u8981\u62ff\u8d70\u4e66\u7c4d\u65f6\uff0c\u4e5f\u5f97\u4ece\u6700\u540e\u653e\u4e0a\u53bb\u7684\u90a3\u672c\u5f00\u59cb\u62ff\u3002\u5982\u679c\u4e2d\u95f4\u91ca\u653e\u4e86\u67d0\u4e2a\u5185\u5b58\u5757\uff0c\u5c31\u4f1a\u5728\u5806\u533a\u7559\u4e0b\u4e00\u4e2a\u7a7a\u6d1e\uff0c\u540e\u7eed\u7684\u5185\u5b58\u5206\u914d\u5982\u679c\u5927\u5c0f\u4e0d\u5408\u9002\uff0c\u5c31\u65e0\u6cd5\u5229\u7528\u8fd9\u4e2a\u7a7a\u6d1e\uff0c\u4ece\u800c\u5bfc\u81f4\u5185\u5b58\u788e\u7247\u5316\u3002\u5c31\u50cf\u4e66\u67b6\u4e0a\u62ff\u8d70\u4e86\u4e2d\u95f4\u7684\u51e0\u672c\u4e66\uff0c\u7559\u4e0b\u4e86\u51e0\u4e2a\u4e0d\u89c4\u5219\u7684\u7a7a\u4f4d\uff0c\u5f88\u96be\u518d\u627e\u5230\u5408\u9002\u5927\u5c0f\u7684\u4e66\u7c4d\u6765\u586b\u8865\u3002<\/p>\n<p>\u800cmmap\u5219\u5728\u6587\u4ef6\u6620\u5c04\u533a\u521b\u5efa\u72ec\u7acb\u7684\u5185\u5b58\u6620\u5c04\u533a\u57df\uff0c\u6bcf\u4e2a\u533a\u57df\u5c31\u50cf\u4e00\u4e2a\u72ec\u7acb\u7684\u5c0f\u623f\u95f4\uff0c\u5b83\u4eec\u4e4b\u95f4\u53ef\u4ee5\u662f\u975e\u8fde\u7eed\u7684\u3002\u5728\u91ca\u653e\u5185\u5b58\u65f6\uff0c\u6bcf\u4e2a\u533a\u57df\u53ef\u4ee5\u72ec\u7acb\u8fdb\u884c\uff0c\u4e0d\u4f1a\u53d7\u5230\u5176\u4ed6\u533a\u57df\u7684\u5f71\u54cd\u3002\u8fd9\u5c31\u597d\u6bd4\u4f60\u6709\u591a\u4e2a\u72ec\u7acb\u7684\u5c0f\u4ed3\u5e93\uff0c\u4f60\u53ef\u4ee5\u968f\u65f6\u5173\u95ed\uff08\u91ca\u653e\uff09\u4efb\u4f55\u4e00\u4e2a\u4ed3\u5e93\uff0c\u800c\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u4ed3\u5e93\u7684\u4f7f\u7528\uff0c\u5927\u5927\u964d\u4f4e\u4e86\u5185\u5b58\u788e\u7247\u5316\u7684\u98ce\u9669\u3002 \u5c31\u50cf\u4f60\u5728\u4e0d\u540c\u7684\u5730\u65b9\u6709\u591a\u4e2a\u5c0f\u4e66\u67b6\uff0c\u6bcf\u4e2a\u4e66\u67b6\u90fd\u53ef\u4ee5\u72ec\u7acb\u7ba1\u7406\uff0c\u62ff\u8d70\u67d0\u4e2a\u4e66\u67b6\u4e0a\u7684\u4e66\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u4e66\u67b6\u7684\u5e03\u5c40\uff0c\u907f\u514d\u4e86\u51fa\u73b0\u50cfbrk\u90a3\u6837\u7684\u6574\u4f53\u5e03\u5c40\u6df7\u4e71\uff08\u5185\u5b58\u788e\u7247\u5316\uff09\u95ee\u9898\u3002<\/p>\n<table class=\"data-table\" data-transient-attributes=\"class\" data-width=\"872.443px\">\n<colgroup data-id=\"c7104f7d-7Y87AWb3\">\n<col span=\"1\" width=\"217\" data-id=\"ce1c038d-D4EkWIT9\" \/>\n<col span=\"1\" width=\"217\" data-id=\"ce1c038d-0PSeEiKX\" \/>\n<col span=\"1\" width=\"217\" data-id=\"ce1c038d-kHNMfQJd\" \/><\/colgroup>\n<tbody data-id=\"t6d5e859-N6MDIYjM\">\n<tr data-id=\"t31e458f-iUB5T78H\">\n<td data-id=\"t8c0e6ab-gY2cBbM1\" data-transient-attributes=\"table-cell-selection\">\u5bf9\u6bd4\u7ef4\u5ea6<\/td>\n<td data-id=\"t8c0e6ab-PhhRJY5G\" data-transient-attributes=\"table-cell-selection\">\u5806\u533a\uff08brk\uff09<\/td>\n<td class=\"table-last-row\" data-id=\"t1b895c3-Wa03BiXH\" data-transient-attributes=\"table-cell-selection\">\u6587\u4ef6\u6620\u5c04\u533a\uff08mmap\uff09<\/td>\n<\/tr>\n<tr data-id=\"t31e458f-ZeVam0i3\">\n<td data-id=\"t8c0e6ab-nmZ1T2nm\" data-transient-attributes=\"table-cell-selection\">\u5185\u5b58\u91ca\u653e\u987a\u5e8f<\/td>\n<td data-id=\"t8c0e6ab-UE20Upnh\" data-transient-attributes=\"table-cell-selection\">\u987a\u5e8f\u91ca\u653e\uff08\u9ad8\u5730\u5740\u4f18\u5148\uff09<\/td>\n<td class=\"table-last-row\" data-id=\"t1b895c3-f8ZXB24X\" data-transient-attributes=\"table-cell-selection\">\u72ec\u7acb\u91ca\u653e\uff08\u4efb\u610f\u533a\u57df\uff09<\/td>\n<\/tr>\n<tr data-id=\"t31e458f-PPNYcLNQ\">\n<td data-id=\"t8c0e6ab-ZgIKUXTM\" data-transient-attributes=\"table-cell-selection\">\u5185\u5b58\u788e\u7247\u5316\u7a0b\u5ea6<\/td>\n<td data-id=\"t8c0e6ab-SWgPfNUW\" data-transient-attributes=\"table-cell-selection\">\u9ad8\uff08\u4e2d\u95f4\u91ca\u653e\u5f62\u6210\u7a7a\u6d1e\uff09<\/td>\n<td class=\"table-last-row\" data-id=\"t1b895c3-bn7f04K0\" data-transient-attributes=\"table-cell-selection\">\u4f4e\uff08\u5355\u533a\u57df\u91ca\u653e\u65e0\u5f71\u54cd\uff09<\/td>\n<\/tr>\n<tr data-id=\"t31e458f-mjlGJAi6\">\n<td class=\"table-last-column\" data-id=\"t1c8f800-WY61Se9R\" data-transient-attributes=\"table-cell-selection\">\u5185\u5b58\u5206\u914d\u5f00\u9500<\/td>\n<td class=\"table-last-column\" data-id=\"t1c8f800-5NeU22a5\" data-transient-attributes=\"table-cell-selection\">\u4f4e\uff08\u4ec5\u4fee\u6539\u6307\u9488\uff09<\/td>\n<td class=\"table-last-column table-last-row\" data-id=\"t16089ba-WOJWlRAG\" data-transient-attributes=\"table-cell-selection\">\u4e2d\uff08\u521b\u5efa\u6620\u5c04\u7ed3\u6784\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>2.2 \u7cfb\u7edf\u8c03\u7528\u5f00\u9500\u4e0e\u9002\u7528\u573a\u666f<\/h3>\n<p>\u5728\u7cfb\u7edf\u8c03\u7528\u5f00\u9500\u65b9\u9762\uff0cbrk\u548cmmap\u4e5f\u6709\u7740\u5404\u81ea\u7684\u7279\u70b9\u3002brk\u6bcf\u6b21\u8c03\u7528\u4ec5\u4ec5\u4fee\u6539\u4e00\u4e2a\u6307\u9488\u503c\uff0c\u5c31\u50cf\u662f\u4f60\u53ea\u9700\u8981\u5728\u5730\u56fe\u4e0a\u79fb\u52a8\u4e00\u4e2a\u6807\u8bb0\u6765\u8868\u793a\u65b0\u7684\u8fb9\u754c\uff0c\u8fd9\u79cd\u64cd\u4f5c\u975e\u5e38\u7b80\u5355\u5feb\u6377\uff0c\u7cfb\u7edf\u8c03\u7528\u5f00\u9500\u6781\u4f4e\uff0c\u5927\u7ea6\u53ea\u9700\u8981 100 \u7eb3\u79d2\u3002\u8fd9\u4f7f\u5f97\u5b83\u975e\u5e38\u9002\u5408\u9ad8\u9891\u3001\u5c0f\u5757\u5185\u5b58\u7684\u5206\u914d\u573a\u666f\uff0c\u6bd4\u5982\u94fe\u8868\u8282\u70b9\u7684\u521b\u5efa\uff0c\u8fd9\u4e9b\u8282\u70b9\u901a\u5e38\u53ea\u9700\u8981\u5f88\u5c0f\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u800c\u4e14\u53ef\u80fd\u4f1a\u9891\u7e41\u5730\u521b\u5efa\u548c\u9500\u6bc1\uff1b\u8fd8\u6709\u4e34\u65f6\u7f13\u51b2\u533a\u7684\u5206\u914d\uff0c\u8fd9\u4e9b\u7f13\u51b2\u533a\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u4e34\u65f6\u4f7f\u7528\uff0c\u5927\u5c0f\u901a\u5e38\u4e5f\u4e0d\u5927\uff0c\u4f7f\u7528brk\u53ef\u4ee5\u5feb\u901f\u5730\u5206\u914d\u548c\u91ca\u653e\u5185\u5b58\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u8fd0\u884c\u6548\u7387\u3002<\/p>\n<p>\u76f8\u6bd4\u4e4b\u4e0b\uff0cmmap\u5728\u521b\u5efa\u5185\u5b58\u6620\u5c04\u65f6\uff0c\u9700\u8981\u521b\u5efavm_area_struct\u7ed3\u6784\u5e76\u5efa\u7acb\u590d\u6742\u7684\u6620\u5c04\u5173\u7cfb\uff0c\u8fd9\u5c31\u50cf\u662f\u4f60\u8981\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u793e\u533a\uff0c\u9700\u8981\u8fdb\u884c\u8be6\u7ec6\u7684\u89c4\u5212\u548c\u5efa\u8bbe\u3002\u8fd9\u79cd\u521d\u59cb\u5316\u8fc7\u7a0b\u7684\u5f00\u9500\u8f83\u9ad8\uff0c\u5927\u7ea6\u9700\u8981 500 \u7eb3\u79d2\u3002\u4f46\u662f\uff0cmmap\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u7684\u5f3a\u5927\u529f\u80fd\u548c\u7a33\u5b9a\u6027\u3002\u5b83\u652f\u6301\u7075\u6d3b\u7684\u5185\u5b58\u91ca\u653e\u65b9\u5f0f\uff0c\u9002\u5408\u5927\u5757\u5185\u5b58\u7684\u5206\u914d\uff0c\u6bd4\u5982\u7f13\u51b2\u533a\u7684\u521b\u5efa\uff0c\u5f53\u4f60\u9700\u8981\u4e00\u4e2a\u8f83\u5927\u7684\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\u6765\u5b58\u50a8\u5927\u91cf\u6570\u636e\u65f6\uff0cmmap\u53ef\u4ee5\u5f88\u597d\u5730\u6ee1\u8db3\u9700\u6c42\uff1b\u5728\u52a8\u6001\u5e93\u52a0\u8f7d\u65f6\uff0c\u4e5f\u901a\u5e38\u4f1a\u4f7f\u7528mmap\uff0c\u5b83\u53ef\u4ee5\u5c06\u52a8\u6001\u5e93\u6587\u4ef6\u6620\u5c04\u5230\u8fdb\u7a0b\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u5171\u4eab\u548c\u8c03\u7528\u3002\u6b64\u5916\uff0cmmap\u5728\u6587\u4ef6\u6620\u5c04\u573a\u666f\u4e2d\u8868\u73b0\u51fa\u8272\uff0c\u6bd4\u5982\u6570\u636e\u5e93\u7d22\u5f15\u7684\u52a0\u8f7d\uff0c\u901a\u8fc7mmap\u53ef\u4ee5\u76f4\u63a5\u5c06\u7d22\u5f15\u6587\u4ef6\u6620\u5c04\u5230\u5185\u5b58\u4e2d\uff0c\u8fdb\u7a0b\u53ef\u4ee5\u50cf\u8bbf\u95ee\u5185\u5b58\u4e00\u6837\u5feb\u901f\u8bbf\u95ee\u7d22\u5f15\u6570\u636e\uff0c\u5927\u5927\u63d0\u9ad8\u4e86\u6570\u636e\u5e93\u7684\u67e5\u8be2\u6548\u7387\u3002<\/p>\n<h3>2.3 malloc \u7684\u300c\u7b56\u7565\u9009\u62e9\u300d<\/h3>\n<p>\u5728 glibc \u7684malloc\u5b9e\u73b0\u4e2d\uff0c\u5185\u5b58\u5206\u914d\u5927\u5c0f\u4e0e\u4e00\u4e2a\u5173\u952e\u9608\u503c\uff08\u9ed8\u8ba4\u662f 128KB\uff09\u7684\u6bd4\u8f83\u51b3\u5b9a\u4e86\u5e95\u5c42\u91c7\u7528\u7684\u5185\u5b58\u5206\u914d\u673a\u5236\u3002\u53c2\u8003\u8fd9\u7bc7\u300a<a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=Mzg4NDQ0OTI4Ng==&amp;mid=2247492221&amp;idx=1&amp;sn=950c4fb9e8e6d881ad54c2550ded8491&amp;scene=21#wechat_redirect\" target=\"_blank\" rel=\"noopener\">glibc\u5806\u5185\u5b58\u7ba1\u7406\uff1a\u539f\u7406\u3001\u673a\u5236\u4e0e\u5b9e\u6218<\/a>\u300b<\/p>\n<p>\u5f53\u5206\u914d\u7684\u5185\u5b58\u8f83\u5c0f\uff08\u5c0f\u4e8e 128KB\uff09\u65f6\uff0cmalloc\u4f1a\u9009\u62e9\u8d70brk\u6269\u5c55\u5806\u533a\u7684\u65b9\u5f0f\u3002\u8fd9\u662f\u56e0\u4e3a\u5c0f\u5757\u5185\u5b58\u7684\u5206\u914d\u548c\u91ca\u653e\u64cd\u4f5c\u53ef\u80fd\u4f1a\u975e\u5e38\u9891\u7e41\uff0c\u800cbrk\u7684\u4f4e\u7cfb\u7edf\u8c03\u7528\u5f00\u9500\u53ef\u4ee5\u5f88\u597d\u5730\u5e94\u5bf9\u8fd9\u79cd\u9ad8\u9891\u64cd\u4f5c\u3002\u540c\u65f6\uff0cmalloc\u5229\u7528\u7a7a\u95f2\u5757\u94fe\u8868\u6765\u590d\u7528\u5185\u5b58\uff0c\u6bd4\u5982 ptmalloc \u7684 fastbin \u673a\u5236\uff0c\u5b83\u4f1a\u5c06\u4e00\u4e9b\u5c0f\u5757\u5185\u5b58\u5757\u7ec4\u7ec7\u6210\u4e00\u4e2a\u5feb\u901f\u5206\u914d\u94fe\u8868\uff0c\u5f53\u6709\u65b0\u7684\u5c0f\u5757\u5185\u5b58\u5206\u914d\u8bf7\u6c42\u65f6\uff0c\u4f18\u5148\u4ece\u8fd9\u4e2a\u94fe\u8868\u4e2d\u67e5\u627e\u5408\u9002\u7684\u5185\u5b58\u5757\u8fdb\u884c\u5206\u914d\uff0c\u907f\u514d\u4e86\u9891\u7e41\u7684\u7cfb\u7edf\u8c03\u7528\uff0c\u8fdb\u4e00\u6b65\u63d0\u9ad8\u4e86\u5206\u914d\u6548\u7387\u3002\u8fd9\u5c31\u50cf\u662f\u4f60\u6709\u4e00\u4e2a\u5c0f\u5de5\u5177\u76d2\uff0c\u91cc\u9762\u6709\u4e00\u4e9b\u5e38\u7528\u7684\u5c0f\u5de5\u5177\uff0c\u6bcf\u6b21\u9700\u8981\u4f7f\u7528\u5c0f\u5de5\u5177\u65f6\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u4ece\u5de5\u5177\u76d2\u91cc\u5feb\u901f\u627e\u5230\uff0c\u800c\u4e0d\u9700\u8981\u6bcf\u6b21\u90fd\u53bb\u5927\u4ed3\u5e93\uff08\u7cfb\u7edf\u5185\u5b58\uff09\u91cc\u5bfb\u627e\u3002<\/p>\n<p>\u5f53\u5206\u914d\u7684\u5185\u5b58\u8f83\u5927\uff08\u5927\u4e8e\u7b49\u4e8e 128KB\uff09\u65f6\uff0cmalloc\u4f1a\u76f4\u63a5\u8c03\u7528mmap\u6765\u521b\u5efa\u72ec\u7acb\u7684\u5185\u5b58\u6620\u5c04\u3002\u8fd9\u662f\u56e0\u4e3a\u5927\u5757\u5185\u5b58\u7684\u5206\u914d\u5982\u679c\u4f7f\u7528brk\uff0c\u5f88\u5bb9\u6613\u5bfc\u81f4\u5806\u533a\u7684\u788e\u7247\u5316\uff0c\u5f71\u54cd\u540e\u7eed\u7684\u5185\u5b58\u5206\u914d\u6548\u7387\u3002\u800cmmap\u7684\u72ec\u7acb\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f\u53ef\u4ee5\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff0c\u867d\u7136\u5b83\u7684\u521d\u59cb\u5316\u5f00\u9500\u8f83\u9ad8\uff0c\u4f46\u5bf9\u4e8e\u5927\u5757\u5185\u5b58\u7684\u4e00\u6b21\u6027\u5206\u914d\u6765\u8bf4\uff0c\u8fd9\u70b9\u5f00\u9500\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\u3002\u8fd9\u5c31\u597d\u6bd4\u4f60\u8981\u5efa\u9020\u4e00\u5ea7\u5927\u578b\u5efa\u7b51\uff0c\u867d\u7136\u524d\u671f\u7684\u89c4\u5212\u548c\u51c6\u5907\u5de5\u4f5c\uff08\u521d\u59cb\u5316\u5f00\u9500\uff09\u6bd4\u8f83\u7e41\u7410\uff0c\u4f46\u5efa\u6210\u540e\u53ef\u4ee5\u72ec\u7acb\u4f7f\u7528\uff0c\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u533a\u57df\uff0c\u4e5f\u4e0d\u4f1a\u56e0\u4e3a\u540e\u7eed\u7684\u4e00\u4e9b\u5c0f\u6539\u52a8\uff08\u5185\u5b58\u91ca\u653e\u548c\u518d\u5206\u914d\uff09\u800c\u5bfc\u81f4\u6574\u4f53\u7ed3\u6784\u7684\u6df7\u4e71\uff08\u5185\u5b58\u788e\u7247\u5316\uff09\u3002<\/p>\n<h2>\u4e09\u3001\u5b9e\u6218\u6307\u5357\uff1a\u5982\u4f55\u6b63\u786e\u4f7f\u7528 brk \u4e0e mmap\uff1f<\/h2>\n<p>\u5728\u4e86\u89e3\u4e86brk\u548cmmap\u7684\u539f\u7406\u4ee5\u53ca\u5b83\u4eec\u5728malloc\u4e2d\u7684\u5e94\u7528\u4e4b\u540e\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u8fdb\u5165\u5b9e\u6218\u73af\u8282\uff0c\u770b\u770b\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u5982\u4f55\u6b63\u786e\u5730\u4f7f\u7528\u5b83\u4eec\uff0c\u4ee5\u53ca\u5728\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u6709\u54ea\u4e9b\u6027\u80fd\u4f18\u5316\u6280\u5de7\u548c\u5e38\u89c1\u9677\u9631\u9700\u8981\u6ce8\u610f\u3002\u8fd9\u5c31\u50cf\u662f\u6211\u4eec\u5b66\u4f1a\u4e86\u7406\u8bba\u77e5\u8bc6\u4e4b\u540e\uff0c\u8981\u4eb2\u81ea\u4e0a\u624b\u5b9e\u8df5\uff0c\u5728\u5b9e\u8df5\u4e2d\u638c\u63e1\u8fd9\u4e9b\u5185\u5b58\u5206\u914d\u5de5\u5177\u7684\u4f7f\u7528\u6280\u5de7\uff0c\u8ba9\u5b83\u4eec\u4e3a\u6211\u4eec\u7684\u7a0b\u5e8f\u9ad8\u6548\u8fd0\u884c\u4fdd\u9a7e\u62a4\u822a\u3002<\/p>\n<h3>3.1 \u57fa\u7840 API \u4f7f\u7528\u793a\u4f8b<\/h3>\n<h4>\uff081\uff09brk\/sbrk \u8c03\u7528<\/h4>\n<p>\u5728 C \u8bed\u8a00\u4e2d\uff0cbrk\u548csbrk\u51fd\u6570\u7528\u4e8e\u64cd\u4f5c\u5806\u5185\u5b58\u3002brk\u51fd\u6570\u76f4\u63a5\u8bbe\u7f6e\u5806\u9876\u6307\u9488\uff0c\u800csbrk\u51fd\u6570\u5219\u662f\u5728\u5f53\u524d\u5806\u9876\u6307\u9488\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u589e\u91cf\u8c03\u6574\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528sbrk\u6765\u5206\u914d\u548c\u91ca\u653e\u5185\u5b58\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_0\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\"><span class=\"token comment\">#include &lt;stdio.h&gt;<\/span>\r\n<span class=\"token comment\">#include &lt;unistd.h&gt;<\/span>\r\n<span class=\"token comment\">#include &lt;stdlib.h&gt;<\/span>\r\n\r\n<span class=\"token keyword\">int<\/span> main<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> {\r\n    <span class=\"token comment\">\/\/ \u4f7f\u7528sbrk\u5206\u914d1024\u5b57\u8282\u5185\u5b58<\/span>\r\n    <span class=\"token keyword\">char<\/span> <span class=\"token operator\">*<\/span>p <span class=\"token operator\">=<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">char<\/span> <span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span>sbrk<span class=\"token punctuation\">(<\/span><span class=\"token number\">1024<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>p <span class=\"token operator\">=<\/span><span class=\"token operator\">=<\/span> <span class=\"token punctuation\">(<\/span>void <span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">-<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> {\r\n        perror<span class=\"token punctuation\">(<\/span><span class=\"token string\">\"sbrk\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token keyword\">return<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span>\r\n    }\r\n\r\n    <span class=\"token comment\">\/\/ \u4f7f\u7528\u5206\u914d\u7684\u5185\u5b58<\/span>\r\n    <span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">=<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">1024<\/span><span class=\"token punctuation\">;<\/span> i<span class=\"token operator\">+<\/span><span class=\"token operator\">+<\/span><span class=\"token punctuation\">)<\/span> {\r\n        p<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">=<\/span> i<span class=\"token punctuation\">;<\/span>\r\n    }\r\n\r\n    <span class=\"token comment\">\/\/ \u8f93\u51fa\u5185\u5b58\u4e2d\u7684\u524d10\u4e2a\u5b57\u8282<\/span>\r\n    <span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">=<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">10<\/span><span class=\"token punctuation\">;<\/span> i<span class=\"token operator\">+<\/span><span class=\"token operator\">+<\/span><span class=\"token punctuation\">)<\/span> {\r\n        printf<span class=\"token punctuation\">(<\/span><span class=\"token string\">\"%d \"<\/span><span class=\"token punctuation\">,<\/span> p<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n    }\r\n    printf<span class=\"token punctuation\">(<\/span><span class=\"token string\">\"\\n\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n\r\n    <span class=\"token comment\">\/\/ \u4f7f\u7528sbrk\u91ca\u653e\u5185\u5b58\uff08\u5c06\u5806\u9876\u6307\u9488\u56de\u90001024\u5b57\u8282\uff09<\/span>\r\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>sbrk<span class=\"token punctuation\">(<\/span><span class=\"token operator\">-<\/span><span class=\"token number\">1024<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">=<\/span><span class=\"token operator\">=<\/span> <span class=\"token punctuation\">(<\/span>void <span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">-<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> {\r\n        perror<span class=\"token punctuation\">(<\/span><span class=\"token string\">\"sbrk\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token keyword\">return<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span>\r\n    }\r\n\r\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span>\r\n}<\/code><\/pre>\n<ul id=\"code_id_0\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<li>29.<\/li>\n<li>30.<\/li>\n<li>31.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528sbrk(1024)\u5206\u914d\u4e86 1024 \u5b57\u8282\u7684\u5185\u5b58\uff0c\u7136\u540e\u5bf9\u8fd9\u5757\u5185\u5b58\u8fdb\u884c\u4e86\u521d\u59cb\u5316\u64cd\u4f5c\uff0c\u6700\u540e\u901a\u8fc7sbrk(-1024)\u5c06\u5806\u9876\u6307\u9488\u56de\u9000 1024 \u5b57\u8282\uff0c\u5b9e\u73b0\u4e86\u5185\u5b58\u7684\u91ca\u653e\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u76f4\u63a5\u4f7f\u7528brk\u548csbrk\u8fdb\u884c\u5185\u5b58\u7ba1\u7406\u7684\u60c5\u51b5\u6bd4\u8f83\u5c11\u89c1\uff0c\u56e0\u4e3a\u5b83\u4eec\u7684\u64cd\u4f5c\u76f8\u5bf9\u5e95\u5c42\uff0c\u5bb9\u6613\u51fa\u9519\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528\u66f4\u9ad8\u7ea7\u7684\u5185\u5b58\u5206\u914d\u51fd\u6570\uff0c\u5982malloc\u548cfree \u3002<\/p>\n<h4>\uff082\uff09mmap\/munmap \u8c03\u7528<\/h4>\n<p>mmap\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u5185\u5b58\u6620\u5c04\uff0cmunmap\u51fd\u6570\u5219\u7528\u4e8e\u53d6\u6d88\u5185\u5b58\u6620\u5c04\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528mmap\u6620\u5c04\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_1\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\"><span class=\"token comment\">#include &lt;stdio.h&gt;<\/span>\r\n<span class=\"token comment\">#include &lt;stdlib.h&gt;<\/span>\r\n<span class=\"token comment\">#include &lt;sys\/mman.h&gt;<\/span>\r\n<span class=\"token comment\">#include &lt;sys\/stat.h&gt;<\/span>\r\n<span class=\"token comment\">#include &lt;fcntl.h&gt;<\/span>\r\n<span class=\"token comment\">#include &lt;unistd.h&gt;<\/span>\r\n<span class=\"token comment\">#include &lt;string.h&gt;<\/span>\r\n\r\n<span class=\"token keyword\">int<\/span> main<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> {\r\n    <span class=\"token keyword\">int<\/span> fd<span class=\"token punctuation\">;<\/span>\r\n    <span class=\"token keyword\">char<\/span> <span class=\"token operator\">*<\/span>file_content<span class=\"token punctuation\">;<\/span>\r\n    struct stat file_stat<span class=\"token punctuation\">;<\/span>\r\n\r\n    <span class=\"token comment\">\/\/ \u6253\u5f00\u6587\u4ef6<\/span>\r\n    fd <span class=\"token operator\">=<\/span> <span class=\"token keyword\">open<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"test.txt\"<\/span><span class=\"token punctuation\">,<\/span> O_RDWR<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>fd <span class=\"token operator\">=<\/span><span class=\"token operator\">=<\/span> <span class=\"token operator\">-<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> {\r\n        perror<span class=\"token punctuation\">(<\/span><span class=\"token string\">\"open\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token keyword\">return<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span>\r\n    }\r\n\r\n    <span class=\"token comment\">\/\/ \u83b7\u53d6\u6587\u4ef6\u5927\u5c0f<\/span>\r\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>fstat<span class=\"token punctuation\">(<\/span>fd<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>file_stat<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">=<\/span><span class=\"token operator\">=<\/span> <span class=\"token operator\">-<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> {\r\n        perror<span class=\"token punctuation\">(<\/span><span class=\"token string\">\"fstat\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token keyword\">close<\/span><span class=\"token punctuation\">(<\/span>fd<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token keyword\">return<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span>\r\n    }\r\n\r\n    <span class=\"token comment\">\/\/ \u4f7f\u7528mmap\u6620\u5c04\u6587\u4ef6<\/span>\r\n    file_content <span class=\"token operator\">=<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">char<\/span> <span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span>mmap<span class=\"token punctuation\">(<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> file_stat<span class=\"token punctuation\">.<\/span>st_size<span class=\"token punctuation\">,<\/span> PROT_READ <span class=\"token operator\">|<\/span> PROT_WRITE<span class=\"token punctuation\">,<\/span> MAP_SHARED<span class=\"token punctuation\">,<\/span> fd<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>file_content <span class=\"token operator\">=<\/span><span class=\"token operator\">=<\/span> MAP_FAILED<span class=\"token punctuation\">)<\/span> {\r\n        perror<span class=\"token punctuation\">(<\/span><span class=\"token string\">\"mmap\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token keyword\">close<\/span><span class=\"token punctuation\">(<\/span>fd<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token keyword\">return<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span>\r\n    }\r\n\r\n    <span class=\"token comment\">\/\/ \u8f93\u51fa\u6587\u4ef6\u5185\u5bb9<\/span>\r\n    printf<span class=\"token punctuation\">(<\/span><span class=\"token string\">\"File content:\\n%s\\n\"<\/span><span class=\"token punctuation\">,<\/span> file_content<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n\r\n    <span class=\"token comment\">\/\/ \u4fee\u6539\u6587\u4ef6\u5185\u5bb9<\/span>\r\n    strcpy<span class=\"token punctuation\">(<\/span>file_content<span class=\"token punctuation\">,<\/span> <span class=\"token string\">\"This is a modified content.\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n    printf<span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Modified file content:\\n%s\\n\"<\/span><span class=\"token punctuation\">,<\/span> file_content<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n\r\n    <span class=\"token comment\">\/\/ \u53d6\u6d88\u6620\u5c04<\/span>\r\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>munmap<span class=\"token punctuation\">(<\/span>file_content<span class=\"token punctuation\">,<\/span> file_stat<span class=\"token punctuation\">.<\/span>st_size<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">=<\/span><span class=\"token operator\">=<\/span> <span class=\"token operator\">-<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> {\r\n        perror<span class=\"token punctuation\">(<\/span><span class=\"token string\">\"munmap\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n    }\r\n\r\n    <span class=\"token comment\">\/\/ \u5173\u95ed\u6587\u4ef6<\/span>\r\n    <span class=\"token keyword\">close<\/span><span class=\"token punctuation\">(<\/span>fd<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n\r\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span>\r\n}<\/code><\/pre>\n<ul id=\"code_id_1\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<li>13.<\/li>\n<li>14.<\/li>\n<li>15.<\/li>\n<li>16.<\/li>\n<li>17.<\/li>\n<li>18.<\/li>\n<li>19.<\/li>\n<li>20.<\/li>\n<li>21.<\/li>\n<li>22.<\/li>\n<li>23.<\/li>\n<li>24.<\/li>\n<li>25.<\/li>\n<li>26.<\/li>\n<li>27.<\/li>\n<li>28.<\/li>\n<li>29.<\/li>\n<li>30.<\/li>\n<li>31.<\/li>\n<li>32.<\/li>\n<li>33.<\/li>\n<li>34.<\/li>\n<li>35.<\/li>\n<li>36.<\/li>\n<li>37.<\/li>\n<li>38.<\/li>\n<li>39.<\/li>\n<li>40.<\/li>\n<li>41.<\/li>\n<li>42.<\/li>\n<li>43.<\/li>\n<li>44.<\/li>\n<li>45.<\/li>\n<li>46.<\/li>\n<li>47.<\/li>\n<li>48.<\/li>\n<li>49.<\/li>\n<li>50.<\/li>\n<li>51.<\/li>\n<li>52.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u83b7\u53d6\u5176\u5927\u5c0f\uff0c\u7136\u540e\u4f7f\u7528mmap\u5c06\u6587\u4ef6\u5185\u5bb9\u6620\u5c04\u5230\u5185\u5b58\u4e2d\u3002\u901a\u8fc7\u8fd4\u56de\u7684\u6307\u9488\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u8bbf\u95ee\u666e\u901a\u5185\u5b58\u4e00\u6837\u8bbf\u95ee\u6587\u4ef6\u5185\u5bb9\uff0c\u5bf9\u5176\u8fdb\u884c\u8bfb\u53d6\u548c\u4fee\u6539\u3002\u6700\u540e\uff0c\u4f7f\u7528munmap\u53d6\u6d88\u5185\u5b58\u6620\u5c04\uff0c\u5e76\u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86mmap\u5728\u6587\u4ef6\u6620\u5c04\u573a\u666f\u4e2d\u7684\u57fa\u672c\u7528\u6cd5\uff0c\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u6587\u4ef6\u7684\u8bfb\u5199\u6548\u7387\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\u3002<\/p>\n<h3>3.2 \u6027\u80fd\u4f18\u5316\u6280\u5de7<\/h3>\n<p>\u5728\u5b9e\u9645\u4f7f\u7528brk\u548cmmap\u65f6\uff0c\u5408\u7406\u7684\u6027\u80fd\u4f18\u5316\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u7a0b\u5e8f\u7684\u8fd0\u884c\u6548\u7387\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u6027\u80fd\u4f18\u5316\u6280\u5de7\uff1a<\/p>\n<p>\u5c0f\u5757\u5185\u5b58\u590d\u7528\uff1a\u5bf9\u4e8e\u9891\u7e41\u5206\u914d\u91ca\u653e\u7684\u5c0f\u5757\u5185\u5b58\uff0c\u5982\u7f51\u7edc\u8bf7\u6c42\u4e34\u65f6\u6570\u636e\uff0c\u76f4\u63a5\u8c03\u7528brk\u4f1a\u5bfc\u81f4\u5927\u91cf\u7684\u7cfb\u7edf\u8c03\u7528\u5f00\u9500\u3002\u6b64\u65f6\uff0c\u6539\u7528\u5185\u5b58\u6c60\uff08\u5982 tcmalloc \u7684 thread &#8211; local cache\uff09\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002\u5185\u5b58\u6c60\u901a\u8fc7\u9884\u5148\u5206\u914d\u4e00\u5757\u8f83\u5927\u7684\u5185\u5b58\uff0c\u7136\u540e\u5728\u5185\u90e8\u7ef4\u62a4\u4e00\u4e2a\u6216\u591a\u4e2a\u94fe\u8868\uff0c\u7528\u4e8e\u5feb\u901f\u5206\u914d\u548c\u56de\u6536\u5c0f\u5757\u5185\u5b58\u3002\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u7cfb\u7edf\u8c03\u7528\u7684\u6b21\u6570\uff0c\u63d0\u9ad8\u5185\u5b58\u5206\u914d\u548c\u91ca\u653e\u7684\u6548\u7387\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u4e2a\u9ad8\u5e76\u53d1\u7684\u7f51\u7edc\u670d\u52a1\u5668\u4e2d\uff0c\u6bcf\u4e2a\u7f51\u7edc\u8bf7\u6c42\u53ef\u80fd\u53ea\u9700\u8981\u51e0 KB \u7684\u5185\u5b58\u6765\u5b58\u50a8\u4e34\u65f6\u6570\u636e\uff0c\u5982\u679c\u6bcf\u6b21\u90fd\u76f4\u63a5\u8c03\u7528brk\u6765\u5206\u914d\u5185\u5b58\uff0c\u4f1a\u5bfc\u81f4\u5927\u91cf\u7684\u7cfb\u7edf\u8c03\u7528\u5f00\u9500\uff0c\u5f71\u54cd\u670d\u52a1\u5668\u7684\u6027\u80fd\u3002\u800c\u4f7f\u7528\u5185\u5b58\u6c60\uff0c\u5c31\u53ef\u4ee5\u5728\u5185\u5b58\u6c60\u4e2d\u5feb\u901f\u5206\u914d\u548c\u56de\u6536\u8fd9\u4e9b\u5c0f\u5757\u5185\u5b58\uff0c\u907f\u514d\u4e86\u9891\u7e41\u7684\u7cfb\u7edf\u8c03\u7528\u3002<\/p>\n<p>\u5927\u5757\u5185\u5b58\u5bf9\u9f50\uff1a\u5728\u4f7f\u7528mmap\u5206\u914d\u5927\u5757\u5185\u5b58\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9aMAP_POPULATE\u6807\u5fd7\u6765\u9884\u5206\u914d\u7269\u7406\u5185\u5b58\u3002\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u540e\u7eed\u8bbf\u95ee\u8fd9\u4e9b\u533a\u57df\u65f6\u89e6\u53d1\u7f3a\u9875\u4e2d\u65ad\u7684\u53ef\u80fd\u6027\uff0c\u63d0\u9ad8\u8bbf\u95ee\u901f\u5ea6\u3002\u53e6\u5916\uff0c\u5229\u7528\u5927\u9875\uff08Huge Pages\uff09\u4e5f\u662f\u4e00\u79cd\u4f18\u5316\u65b9\u5f0f\u3002\u5927\u9875\u53ef\u4ee5\u51cf\u5c11\u9875\u8868\u7684\u5927\u5c0f\uff0c\u964d\u4f4e TLB\uff08Translation Lookaside Buffer\uff09\u7f3a\u5931\u7684\u6982\u7387\uff0c\u4ece\u800c\u63d0\u5347\u5185\u5b58\u8bbf\u95ee\u6548\u7387\u3002\u5728\u4e00\u4e9b\u5bf9\u5185\u5b58\u8bbf\u95ee\u6027\u80fd\u8981\u6c42\u6781\u9ad8\u7684\u5e94\u7528\u4e2d\uff0c\u5982\u6570\u636e\u5e93\u7cfb\u7edf\uff0c\u7ecf\u5e38\u4f1a\u4f7f\u7528\u5927\u9875\u6765\u63d0\u9ad8\u5185\u5b58\u8bbf\u95ee\u901f\u5ea6\u3002\u4f8b\u5982\uff0c\u5728 MySQL \u6570\u636e\u5e93\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u53c2\u6570\u6765\u542f\u7528\u5927\u9875\uff0c\u63d0\u9ad8\u6570\u636e\u5e93\u7684\u6027\u80fd\u3002<\/p>\n<p>\u788e\u7247\u6574\u7406\uff1a\u5bf9\u4e8ebrk\u5206\u914d\u7684\u5185\u5b58\uff0c\u53ef\u4ee5\u901a\u8fc7mallopt(M_TRIM_THRESHOLD, 0)\u5f3a\u5236brk\u91ca\u653e\u8d85\u8fc7\u9608\u503c\u7684\u7a7a\u95f2\u5185\u5b58\uff0c\u51cf\u5c11\u5185\u5b58\u788e\u7247\u7684\u4ea7\u751f\u3002\u5bf9\u4e8emmap\u5206\u914d\u7684\u5185\u5b58\uff0c\u53ef\u4ee5\u4f7f\u7528madvise\u51fd\u6570\u5bf9\u5185\u5b58\u533a\u57df\u8fdb\u884c\u9884\u53d6 \/ \u4e22\u5f03\u4f18\u5316\u3002\u4f8b\u5982\uff0c\u5f53\u5e94\u7528\u7a0b\u5e8f\u77e5\u9053\u5c06\u6765\u4f1a\u4f7f\u7528\u67d0\u4e9b\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7madvise\u5efa\u8bae\u64cd\u4f5c\u7cfb\u7edf\u63d0\u524d\u52a0\u8f7d\u8fd9\u4e9b\u6570\u636e\u5230\u5185\u5b58\u4e2d\uff0c\u63d0\u9ad8\u6570\u636e\u8bbf\u95ee\u7684\u6548\u7387\uff1b\u5f53\u5e94\u7528\u7a0b\u5e8f\u4e0d\u518d\u9700\u8981\u67d0\u4e9b\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7madvise\u544a\u77e5\u5185\u6838\u91ca\u653e\u5185\u5b58\uff0c\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u3002\u5728\u4e00\u4e2a\u89c6\u9891\u64ad\u653e\u5e94\u7528\u4e2d\uff0c\u5728\u64ad\u653e\u89c6\u9891\u524d\uff0c\u53ef\u4ee5\u901a\u8fc7madvise\u9884\u53d6\u89c6\u9891\u6570\u636e\uff0c\u907f\u514d\u64ad\u653e\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u5361\u987f\uff1b\u5728\u89c6\u9891\u64ad\u653e\u7ed3\u675f\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7madvise\u91ca\u653e\u4e0d\u518d\u9700\u8981\u7684\u5185\u5b58\uff0c\u63d0\u9ad8\u7cfb\u7edf\u7684\u5185\u5b58\u5229\u7528\u7387\u3002<\/p>\n<h3>3.3 \u5e38\u89c1\u9677\u9631\u4e0e\u907f\u5751<\/h3>\n<p>\u5728\u4f7f\u7528brk\u548cmmap\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4e5f\u5b58\u5728\u4e00\u4e9b\u5e38\u89c1\u7684\u9677\u9631\uff0c\u5982\u679c\u4e0d\u6ce8\u610f\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u51fa\u73b0\u5185\u5b58\u6cc4\u6f0f\u3001\u6027\u80fd\u4e0b\u964d\u7b49\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u9677\u9631\u53ca\u907f\u514d\u65b9\u6cd5\uff1a<\/p>\n<p><strong>brk \u7684\u300c\u91ca\u653e\u9650\u5236\u300d<\/strong>\uff1a\u5728\u4f7f\u7528brk\u91ca\u653e\u5806\u5185\u5b58\u65f6\uff0c\u53ea\u80fd\u56de\u9000\u5230\u6700\u8fd1\u4e00\u6b21\u5206\u914d\u7684\u9ad8\u5730\u5740\uff0c\u82e5\u4e2d\u95f4\u6709\u672a\u91ca\u653e\u5757\u5219\u65e0\u6cd5\u7f29\u51cf\u3002\u4f8b\u5982\uff0c\u5148\u8fdb\u884c A \u5206\u914d\uff0c\u518d\u8fdb\u884c B \u5206\u914d\uff0c\u7136\u540e\u91ca\u653e A\uff0c\u6b64\u65f6\u4f1a\u5bfc\u81f4\u5185\u5b58\u7a7a\u6d1e\uff0c\u53ea\u6709\u7b49 B \u91ca\u653e\u540e\u624d\u80fd\u6574\u4f53\u56de\u6536\u3002\u8fd9\u5c31\u50cf\u662f\u4f60\u5728\u4e66\u67b6\u4e0a\u4f9d\u6b21\u653e\u4e66\uff0c\u5148\u653e\u4e86 A \u4e66\uff0c\u518d\u653e\u4e86 B \u4e66\uff0c\u5f53\u4f60\u60f3\u62ff\u8d70 A \u4e66\u65f6\uff0c\u5982\u679c B \u4e66\u8fd8\u5728\uff0c\u5c31\u65e0\u6cd5\u76f4\u63a5\u62ff\u8d70 A \u4e66\u6240\u5728\u7684\u90a3\u4e00\u5c42\u7a7a\u95f4\uff0c\u5fc5\u987b\u5148\u62ff\u8d70 B \u4e66\uff0c\u624d\u80fd\u771f\u6b63\u91ca\u653e A \u4e66\u5360\u7528\u7684\u7a7a\u95f4\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u5728\u8bbe\u8ba1\u5185\u5b58\u5206\u914d\u7b56\u7565\u65f6\uff0c\u9700\u8981\u5145\u5206\u8003\u8651\u5185\u5b58\u7684\u91ca\u653e\u987a\u5e8f\uff0c\u5c3d\u91cf\u907f\u514d\u51fa\u73b0\u4e2d\u95f4\u6709\u672a\u91ca\u653e\u5757\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>mmap \u7684\u300c\u6cc4\u6f0f\u98ce\u9669\u300d<\/strong>\uff1a\u5982\u679c\u5fd8\u8bb0\u8c03\u7528munmap\u53d6\u6d88\u5185\u5b58\u6620\u5c04\uff0c\u4f1a\u5bfc\u81f4\u865a\u62df\u5185\u5b58\u6cc4\u6f0f\u3002\u867d\u7136\u8fd9\u79cd\u6cc4\u6f0f\u4e0d\u4f1a\u5360\u7528\u7269\u7406\u5185\u5b58\uff0c\u4f46\u4f1a\u6d88\u8017\u5730\u5740\u7a7a\u95f4\uff0c\u6700\u7ec8\u53ef\u80fd\u5bfc\u81f4\u8fdb\u7a0b\u65e0\u6cd5\u518d\u5206\u914d\u65b0\u7684\u5185\u5b58\u3002\u53ef\u4ee5\u901a\u8fc7pmap pid\u547d\u4ee4\u67e5\u770b\u8fdb\u7a0b\u7684\u6620\u5c04\u60c5\u51b5\uff0c\u6392\u67e5\u662f\u5426\u5b58\u5728\u672a\u91ca\u653e\u7684\u5185\u5b58\u6620\u5c04\u3002\u8fd9\u5c31\u50cf\u662f\u4f60\u79df\u4e86\u4e00\u95f4\u623f\u5b50\uff0c\u4f4f\u5b8c\u540e\u5374\u5fd8\u8bb0\u9000\u623f\uff0c\u867d\u7136\u623f\u5b50\u91cc\u6ca1\u6709\u4eba\u4f4f\uff0c\u4f46\u522b\u4eba\u4e5f\u65e0\u6cd5\u518d\u79df\u7528\u8fd9\u95f4\u623f\u5b50\uff0c\u9020\u6210\u4e86\u8d44\u6e90\u7684\u6d6a\u8d39\u3002\u5728\u7f16\u5199\u7a0b\u5e8f\u65f6\uff0c\u4e00\u5b9a\u8981\u786e\u4fdd\u5728\u4e0d\u518d\u9700\u8981\u5185\u5b58\u6620\u5c04\u65f6\uff0c\u53ca\u65f6\u8c03\u7528munmap\u8fdb\u884c\u91ca\u653e\u3002<\/p>\n<p><strong>\u6743\u9650\u63a7\u5236<\/strong>\uff1a\u4f7f\u7528mmap\u65f6\uff0c\u53ef\u4ee5\u8bbe\u7f6ePROT_NONE\u6743\u9650\u6765\u521b\u5efa\u4e00\u4e2a\u4fdd\u62a4\u533a\u57df\uff0c\u6355\u83b7\u975e\u6cd5\u8bbf\u95ee\uff0c\u5982\u7f13\u51b2\u533a\u6ea2\u51fa\u3002\u4f46\u662f\uff0c\u8fd9\u9700\u8981\u914d\u5408\u4fe1\u53f7\u5904\u7406\uff08SIGSEGV\uff09\u4f7f\u7528\uff0c\u5f53\u53d1\u751f\u975e\u6cd5\u8bbf\u95ee\u65f6\uff0c\u7cfb\u7edf\u4f1a\u53d1\u9001SIGSEGV\u4fe1\u53f7\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7\u8fd9\u4e2a\u4fe1\u53f7\u6765\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\uff0c\u5982\u8bb0\u5f55\u9519\u8bef\u65e5\u5fd7\u3001\u8fdb\u884c\u9519\u8bef\u6062\u590d\u7b49\u3002\u8fd9\u5c31\u50cf\u662f\u4f60\u5728\u623f\u5b50\u5468\u56f4\u8bbe\u7f6e\u4e86\u4e00\u5708\u8b66\u6212\u7ebf\uff0c\u5f53\u6709\u4eba\u975e\u6cd5\u95ef\u5165\u65f6\uff0c\u5c31\u4f1a\u89e6\u53d1\u8b66\u62a5\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u8b66\u62a5\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002\u5728\u4f7f\u7528mmap\u65f6\uff0c\u5408\u7406\u8bbe\u7f6e\u6743\u9650\u548c\u4fe1\u53f7\u5904\u7406\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<h2>\u56db\u3001\u5185\u5b58\u5206\u914d\u4e4b\u9053<\/h2>\n<h3>4.1 \u64cd\u4f5c\u7cfb\u7edf\u7684\u300c\u5e73\u8861\u4e4b\u9053\u300d<\/h3>\n<p>brk\u4e0emmap\u7684\u8bbe\u8ba1\u7cbe\u5999\u5730\u4f53\u73b0\u4e86\u64cd\u4f5c\u7cfb\u7edf\u5728\u300c\u6548\u7387\u300d\u4e0e\u300c\u7075\u6d3b\u300d\u4e4b\u95f4\u7684\u6743\u8861\u667a\u6167\u3002brk\u901a\u8fc7\u7b80\u5355\u5730\u79fb\u52a8\u5806\u9876\u6307\u9488\u6765\u5206\u914d\u5185\u5b58\uff0c\u8fd9\u79cd\u65b9\u5f0f\u867d\u7136\u5728\u7075\u6d3b\u6027\u4e0a\u6709\u6240\u6b20\u7f3a\uff0c\u4f8b\u5982\u5b83\u53ea\u80fd\u5206\u914d\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u91ca\u653e\u5185\u5b58\u65f6\u4e5f\u53d7\u5230\u4e25\u683c\u7684\u987a\u5e8f\u9650\u5236\uff0c\u4f46\u5b83\u5374\u5728\u9ad8\u9891\u5185\u5b58\u5206\u914d\u64cd\u4f5c\u4e2d\u5c55\u73b0\u51fa\u4e86\u6781\u9ad8\u7684\u6548\u7387\u3002\u5c31\u50cf\u5728\u4e00\u4e2a\u5c0f\u578b\u5de5\u5382\u91cc\uff0c\u6240\u6709\u7684\u751f\u4ea7\u6d41\u7a0b\u90fd\u975e\u5e38\u7b80\u5355\u76f4\u63a5\uff0c\u867d\u7136\u4e0d\u80fd\u751f\u4ea7\u51fa\u975e\u5e38\u590d\u6742\u591a\u6837\u7684\u4ea7\u54c1\uff0c\u4f46\u662f\u5bf9\u4e8e\u4e00\u4e9b\u5e38\u89c4\u7684\u3001\u5927\u91cf\u9700\u6c42\u7684\u7b80\u5355\u4ea7\u54c1\uff0c\u5374\u80fd\u4ee5\u6700\u5feb\u7684\u901f\u5ea6\u751f\u4ea7\u51fa\u6765\u3002\u8fd9\u79cd\u7279\u6027\u4f7f\u5f97brk\u7279\u522b\u9002\u5408\u90a3\u4e9b\u5bf9\u5185\u5b58\u5206\u914d\u901f\u5ea6\u8981\u6c42\u6781\u9ad8\uff0c\u4e14\u5185\u5b58\u9700\u6c42\u76f8\u5bf9\u8f83\u5c0f\u4e14\u8fde\u7eed\u7684\u573a\u666f\uff0c\u6bd4\u5982\u5728\u4e00\u4e2a\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u5c0f\u5bf9\u8c61\u7684\u7a0b\u5e8f\u4e2d\uff0cbrk\u80fd\u591f\u5feb\u901f\u5730\u4e3a\u8fd9\u4e9b\u5c0f\u5bf9\u8c61\u5206\u914d\u5185\u5b58\uff0c\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u9ad8\u6548\u8fd0\u884c\u3002<\/p>\n<p>\u800cmmap\u5219\u8d70\u5411\u4e86\u53e6\u4e00\u4e2a\u65b9\u5411\uff0c\u5b83\u653e\u5f03\u4e86\u5355\u4e00\u8fde\u7eed\u7a7a\u95f4\u7684\u9650\u5236\uff0c\u5141\u8bb8\u521b\u5efa\u72ec\u7acb\u7684\u5185\u5b58\u6620\u5c04\u533a\u57df\u3002\u8fd9\u5c31\u597d\u6bd4\u4e00\u4e2a\u5927\u578b\u7684\u7efc\u5408\u6027\u5de5\u5382\uff0c\u5b83\u53ef\u4ee5\u751f\u4ea7\u5404\u79cd\u590d\u6742\u7684\u3001\u591a\u6837\u5316\u7684\u4ea7\u54c1\uff0c\u6bcf\u4e2a\u4ea7\u54c1\u7684\u751f\u4ea7\u6d41\u7a0b\u90fd\u53ef\u4ee5\u72ec\u7acb\u8fdb\u884c\u3002mmap\u7684\u8fd9\u79cd\u7279\u6027\u8d4b\u4e88\u4e86\u5b83\u5f3a\u5927\u7684\u7075\u6d3b\u6027\uff0c\u5b83\u53ef\u4ee5\u5b9e\u73b0\u5185\u5b58\u7684\u72ec\u7acb\u7ba1\u7406\u548c\u5171\u4eab\uff0c\u7279\u522b\u9002\u5408\u90a3\u4e9b\u5bf9\u5185\u5b58\u7ba1\u7406\u7075\u6d3b\u6027\u8981\u6c42\u8f83\u9ad8\uff0c\u4e14\u5185\u5b58\u9700\u6c42\u8f83\u5927\u7684\u573a\u666f\uff0c\u6bd4\u5982\u5728\u8de8\u8fdb\u7a0b\u901a\u4fe1\u4e2d\uff0cmmap\u53ef\u4ee5\u521b\u5efa\u5171\u4eab\u5185\u5b58\u533a\u57df\uff0c\u8ba9\u591a\u4e2a\u8fdb\u7a0b\u80fd\u591f\u9ad8\u6548\u5730\u5171\u4eab\u6570\u636e\uff1b\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0cmmap\u53ef\u4ee5\u5c06\u6587\u4ef6\u76f4\u63a5\u6620\u5c04\u5230\u5185\u5b58\u4e2d\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u3002<\/p>\n<p>\u8fd9\u79cd\u5728\u4e0d\u540c\u7ef4\u5ea6\u4e0a\u7684\u6743\u8861\u4e0e\u8bbe\u8ba1\uff0c\u4e0d\u4ec5\u4ec5\u4f53\u73b0\u5728\u5185\u5b58\u5206\u914d\u9886\u57df\uff0c\u5728\u6574\u4e2a\u64cd\u4f5c\u7cfb\u7edf\u7684\u8bbe\u8ba1\u4e2d\u90fd\u6709\u7740\u5e7f\u6cdb\u7684\u4f53\u73b0\u3002\u4ee5 TCP\/IP \u534f\u8bae\u6808\u4e3a\u4f8b\uff0cBSD socket \u548c raw socket \u5c31\u662f\u8fd9\u79cd\u5206\u5c42\u8bbe\u8ba1\u601d\u60f3\u7684\u5178\u578b\u4f53\u73b0\u3002BSD socket \u4e3a\u5e94\u7528\u5c42\u63d0\u4f9b\u4e86\u4e00\u4e2a\u76f8\u5bf9\u9ad8\u5c42\u3001\u62bd\u8c61\u7684\u63a5\u53e3\uff0c\u5b83\u9690\u85cf\u4e86\u5e95\u5c42\u7f51\u7edc\u534f\u8bae\u7684\u8bb8\u591a\u7ec6\u8282\uff0c\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u65b9\u4fbf\u5feb\u6377\u5730\u8fdb\u884c\u7f51\u7edc\u901a\u4fe1\uff0c\u5c31\u50cf\u4f7f\u7528\u4e00\u4e2a\u5df2\u7ecf\u7ec4\u88c5\u597d\u7684\u5de5\u5177\uff0c\u53ea\u9700\u8981\u7b80\u5355\u64cd\u4f5c\u5c31\u80fd\u5b8c\u6210\u4efb\u52a1\uff0c\u8fd9\u4f53\u73b0\u4e86\u5bf9\u6548\u7387\u7684\u8ffd\u6c42\u3002\u800c raw socket \u5219\u5141\u8bb8\u5f00\u53d1\u8005\u76f4\u63a5\u8bbf\u95ee\u5e95\u5c42\u7684\u7f51\u7edc\u534f\u8bae\uff0c\u80fd\u591f\u5bf9\u7f51\u7edc\u6570\u636e\u5305\u8fdb\u884c\u66f4\u52a0\u7cbe\u7ec6\u7684\u63a7\u5236\uff0c\u867d\u7136\u4f7f\u7528\u8d77\u6765\u76f8\u5bf9\u590d\u6742\uff0c\u4f46\u5374\u63d0\u4f9b\u4e86\u6781\u5927\u7684\u7075\u6d3b\u6027\uff0c\u9002\u7528\u4e8e\u4e00\u4e9b\u5bf9\u7f51\u7edc\u901a\u4fe1\u6709\u7279\u6b8a\u9700\u6c42\u7684\u573a\u666f\uff0c\u6bd4\u5982\u7f51\u7edc\u534f\u8bae\u5206\u6790\u5de5\u5177\u7684\u5f00\u53d1\u3002<\/p>\n<p>\u540c\u6837\uff0c\u5728\u6587\u4ef6\u7cfb\u7edf\u7684\u8bbe\u8ba1\u4e2d\uff0cext4 \u548c f2fs \u4e5f\u5c55\u73b0\u4e86\u7c7b\u4f3c\u7684\u5206\u5c42\u8bbe\u8ba1\u601d\u60f3\u3002ext4 \u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u6587\u4ef6\u7cfb\u7edf\uff0c\u5b83\u5728\u8bbe\u8ba1\u4e0a\u6ce8\u91cd\u517c\u5bb9\u6027\u548c\u7a33\u5b9a\u6027\uff0c\u91c7\u7528\u4e86\u4f20\u7edf\u7684\u6587\u4ef6\u7cfb\u7edf\u7ed3\u6784\uff0c\u5bf9\u4e8e\u5927\u591a\u6570\u5e38\u89c4\u7684\u6587\u4ef6\u5b58\u50a8\u548c\u8bbf\u95ee\u9700\u6c42\uff0c\u90fd\u80fd\u63d0\u4f9b\u9ad8\u6548\u7684\u652f\u6301\uff0c\u8fd9\u4f53\u73b0\u4e86\u5bf9\u6548\u7387\u7684\u4fdd\u969c\u3002\u800c f2fs \u5219\u662f\u4e00\u79cd\u4e13\u95e8\u4e3a\u95ea\u5b58\u8bbe\u5907\u8bbe\u8ba1\u7684\u6587\u4ef6\u7cfb\u7edf\uff0c\u5b83\u9488\u5bf9\u95ea\u5b58\u7684\u7279\u6027\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u91c7\u7528\u4e86\u66f4\u52a0\u7075\u6d3b\u7684\u7ed3\u6784\uff0c\u80fd\u591f\u66f4\u597d\u5730\u9002\u5e94\u95ea\u5b58\u7684\u8bfb\u5199\u7279\u70b9\uff0c\u63d0\u9ad8\u95ea\u5b58\u8bbe\u5907\u7684\u4f7f\u7528\u5bff\u547d\u548c\u6027\u80fd\uff0c\u8fd9\u4f53\u73b0\u4e86\u5bf9\u7279\u5b9a\u573a\u666f\u4e0b\u7075\u6d3b\u6027\u7684\u8ffd\u6c42\u3002<\/p>\n<h3>4.2 \u73b0\u4ee3\u5185\u5b58\u5206\u914d\u5668\u7684\u300c\u878d\u5408\u521b\u65b0\u300d<\/h3>\n<p>\u4ee5 glibc \u7684 ptmalloc\u3001Google \u7684 tcmalloc \u4e3a\u4ee3\u8868\u7684\u73b0\u4ee3\u5185\u5b58\u5206\u914d\u5668\uff0c\u5de7\u5999\u5730\u91c7\u7528\u4e86\u300cbrk + mmap \u6df7\u5408\u7b56\u7565\u300d\uff0c\u5c06brk\u548cmmap\u7684\u4f18\u52bf\u53d1\u6325\u5230\u4e86\u6781\u81f4\u3002<\/p>\n<p>\u5bf9\u4e8e\u5c0f\u5bf9\u8c61\uff08\u4e00\u822c\u5c0f\u4e8e 64KB\uff09 \uff0c\u8fd9\u4e9b\u5206\u914d\u5668\u901a\u5e38\u4f1a\u5229\u7528\u7ebf\u7a0b\u672c\u5730\u7f13\u5b58\u6765\u8fdb\u884c\u5206\u914d\u3002\u4ee5 TCMalloc \u7684 thread cache \u4e3a\u4f8b\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u81ea\u5df1\u72ec\u7acb\u7684\u7f13\u5b58\uff0c\u5f53\u7ebf\u7a0b\u9700\u8981\u5206\u914d\u5c0f\u5bf9\u8c61\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u4ece\u81ea\u5df1\u7684\u7f13\u5b58\u4e2d\u83b7\u53d6\u5185\u5b58\uff0c\u907f\u514d\u4e86\u9501\u7ade\u4e89\u3002\u8fd9\u5c31\u597d\u6bd4\u6bcf\u4e2a\u5458\u5de5\u90fd\u6709\u81ea\u5df1\u7684\u5c0f\u5de5\u5177\u76d2\uff0c\u5f53\u9700\u8981\u4f7f\u7528\u5c0f\u5de5\u5177\u65f6\uff0c\u76f4\u63a5\u4ece\u81ea\u5df1\u7684\u5de5\u5177\u76d2\u91cc\u62ff\u53d6\uff0c\u4e0d\u9700\u8981\u548c\u5176\u4ed6\u5458\u5de5\u4e89\u62a2\u5927\u4ed3\u5e93\u91cc\u7684\u5de5\u5177\uff0c\u5927\u5927\u63d0\u9ad8\u4e86\u5206\u914d\u7684\u6548\u7387\u3002<\/p>\n<p>\u5f53\u9762\u5bf9\u4e2d\u5bf9\u8c61\uff0864KB &#8211; 128KB\uff09\u65f6\uff0c\u5206\u914d\u5668\u4f1a\u901a\u8fc7brk\u4ece\u5806\u533a\u5206\u914d\u5185\u5b58\u3002\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u5206\u914d\u5668\u4f1a\u5229\u7528\u7a7a\u95f2\u5757\u5408\u5e76\u7684\u6280\u672f\uff0c\u5c06\u76f8\u90bb\u7684\u7a7a\u95f2\u5185\u5b58\u5757\u5408\u5e76\u6210\u66f4\u5927\u7684\u5185\u5b58\u5757\uff0c\u51cf\u5c11\u5185\u5b58\u788e\u7247\u7684\u4ea7\u751f\u3002\u8fd9\u5c31\u50cf\u662f\u5728\u6574\u7406\u4ed3\u5e93\u65f6\uff0c\u5c06\u76f8\u90bb\u7684\u5c0f\u7a7a\u4f4d\u5408\u5e76\u6210\u4e00\u4e2a\u5927\u7a7a\u4f4d\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u5229\u7528\u7a7a\u95f4\u3002\u4f8b\u5982\uff0cptmalloc \u4f1a\u7ef4\u62a4\u4e0d\u540c\u5927\u5c0f\u7684\u7a7a\u95f2\u5757\u94fe\u8868\uff0c\u5f53\u6709\u65b0\u7684\u5185\u5b58\u5206\u914d\u8bf7\u6c42\u65f6\uff0c\u4f1a\u9996\u5148\u5728\u5408\u9002\u7684\u94fe\u8868\u4e2d\u67e5\u627e\u662f\u5426\u6709\u53ef\u7528\u7684\u7a7a\u95f2\u5757\uff0c\u5982\u679c\u6709\u5219\u76f4\u63a5\u5206\u914d\uff0c\u5426\u5219\u4f1a\u5c1d\u8bd5\u5408\u5e76\u76f8\u90bb\u7684\u7a7a\u95f2\u5757\u6765\u6ee1\u8db3\u8bf7\u6c42\u3002<\/p>\n<p>\u800c\u5bf9\u4e8e\u5927\u5bf9\u8c61\uff08\u5927\u4e8e 128KB\uff09\uff0c\u5206\u914d\u5668\u4f1a\u76f4\u63a5\u4f7f\u7528mmap\u6765\u5206\u914d\u5185\u5b58\u3002\u7531\u4e8e\u5927\u5bf9\u8c61\u7684\u5185\u5b58\u9700\u6c42\u8f83\u5927\uff0c\u5982\u679c\u4f7f\u7528brk\u5206\u914d\uff0c\u5f88\u5bb9\u6613\u5bfc\u81f4\u5806\u533a\u7684\u788e\u7247\u5316\uff0c\u5f71\u54cd\u540e\u7eed\u7684\u5185\u5b58\u5206\u914d\u6548\u7387\u3002\u800cmmap\u7684\u72ec\u7acb\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f\u53ef\u4ee5\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff0c\u867d\u7136\u5b83\u7684\u521d\u59cb\u5316\u5f00\u9500\u8f83\u9ad8\uff0c\u4f46\u5bf9\u4e8e\u5927\u5bf9\u8c61\u7684\u4e00\u6b21\u6027\u5206\u914d\u6765\u8bf4\uff0c\u8fd9\u70b9\u5f00\u9500\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\u3002\u8fd9\u5c31\u597d\u6bd4\u5efa\u9020\u5927\u578b\u5efa\u7b51\uff0c\u867d\u7136\u524d\u671f\u7684\u89c4\u5212\u548c\u51c6\u5907\u5de5\u4f5c\u6bd4\u8f83\u7e41\u7410\uff0c\u4f46\u5efa\u6210\u540e\u53ef\u4ee5\u72ec\u7acb\u4f7f\u7528\uff0c\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u533a\u57df\uff0c\u4e5f\u4e0d\u4f1a\u56e0\u4e3a\u540e\u7eed\u7684\u4e00\u4e9b\u5c0f\u6539\u52a8\u800c\u5bfc\u81f4\u6574\u4f53\u7ed3\u6784\u7684\u6df7\u4e71\u3002<\/p>\n<p>\u8fd9\u79cd\u6df7\u5408\u7b56\u7565\u7684\u8bbe\u8ba1\uff0c\u4e0d\u4ec5\u517c\u987e\u4e86\u6027\u80fd\u4e0e\u7a33\u5b9a\u6027\uff0c\u8fd8\u5c06\u5e95\u5c42\u7cfb\u7edf\u8c03\u7528\u7684\u7ec6\u8282\u5c01\u88c5\u8d77\u6765\uff0c\u5411\u4e0a\u5c42\u5e94\u7528\u63d0\u4f9b\u4e86\u7edf\u4e00\u7684malloc\/free\u63a5\u53e3\u3002\u5e94\u7528\u7a0b\u5e8f\u5728\u8fdb\u884c\u5185\u5b58\u5206\u914d\u65f6\uff0c\u4e0d\u9700\u8981\u5173\u5fc3\u5e95\u5c42\u5230\u5e95\u662f\u4f7f\u7528brk\u8fd8\u662fmmap\uff0c\u53ea\u9700\u8981\u8c03\u7528malloc\u51fd\u6570\u5373\u53ef\uff0c\u8fd9\u5927\u5927\u7b80\u5316\u4e86\u5f00\u53d1\u8005\u7684\u5de5\u4f5c\uff0c\u540c\u65f6\u4e5f\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u7684\u53ef\u79fb\u690d\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<h3>4.3 \u5f00\u53d1\u8005\u7684\u300c\u9009\u62e9\u539f\u5219\u300d<\/h3>\n<p>\u5728\u5b9e\u9645\u7684\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u5f00\u53d1\u8005\u9700\u8981\u6839\u636e\u4e0d\u540c\u7684\u4e1a\u52a1\u573a\u666f\u548c\u9700\u6c42\uff0c\u5408\u7406\u5730\u9009\u62e9\u5185\u5b58\u5206\u914d\u65b9\u5f0f\u3002<\/p>\n<p><strong>\u4f18\u5148\u4f7f\u7528\u6807\u51c6\u5e93\u63a5\u53e3<\/strong>\uff1a\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5f00\u53d1\u8005\u5e94\u8be5\u4f18\u5148\u4f7f\u7528\u6807\u51c6\u5e93\u63d0\u4f9b\u7684malloc\/free\u63a5\u53e3\u3002\u8fd9\u4e9b\u63a5\u53e3\u7ecf\u8fc7\u4e86\u5927\u91cf\u7684\u6d4b\u8bd5\u548c\u4f18\u5316\uff0c\u80fd\u591f\u6839\u636e\u5185\u5b58\u5206\u914d\u7684\u5927\u5c0f\u81ea\u52a8\u9009\u62e9\u5408\u9002\u7684\u5e95\u5c42\u5b9e\u73b0\u673a\u5236\uff08brk\u6216mmap\uff09 \u3002\u9664\u975e\u5f00\u53d1\u8005\u6709\u7279\u6b8a\u7684\u9700\u6c42\uff0c\u4f8b\u5982\u9700\u8981\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u5185\u5b58\u5206\u914d\u5668\uff0c\u5982\u5728\u6e38\u620f\u5f15\u64ce\u4e2d\uff0c\u4e3a\u4e86\u63d0\u9ad8\u5185\u5b58\u7ba1\u7406\u7684\u6548\u7387\u548c\u6027\u80fd\uff0c\u5e38\u5e38\u4f1a\u5b9e\u73b0\u81ea\u5df1\u7684\u5185\u5b58\u6c60\uff0c\u5426\u5219\u76f4\u63a5\u4f7f\u7528\u6807\u51c6\u5e93\u63a5\u53e3\u662f\u6700\u7b80\u4fbf\u3001\u6700\u5b89\u5168\u7684\u9009\u62e9\u3002<\/p>\n<p><strong>\u5173\u6ce8\u4e1a\u52a1\u573a\u666f<\/strong>\uff1a\u4e1a\u52a1\u573a\u666f\u662f\u9009\u62e9\u5185\u5b58\u5206\u914d\u65b9\u5f0f\u7684\u91cd\u8981\u4f9d\u636e\u3002\u5bf9\u4e8e\u9ad8\u9891\u5c0f\u5757\u5185\u5b58\u7684\u5206\u914d\u573a\u666f\uff0c\u4f8b\u5982\u5728\u4e00\u4e2a\u7f51\u7edc\u670d\u52a1\u5668\u4e2d\uff0c\u6bcf\u4e2a\u7f51\u7edc\u8bf7\u6c42\u53ef\u80fd\u53ea\u9700\u8981\u51e0 KB \u7684\u5185\u5b58\u6765\u5b58\u50a8\u4e34\u65f6\u6570\u636e\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u4f7f\u7528mmap\uff0c\u56e0\u4e3ammap\u7684\u9ad8\u5f00\u9500\u4f1a\u4e25\u91cd\u5f71\u54cd\u7cfb\u7edf\u7684\u6027\u80fd\uff0c\u800cbrk\u5219\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u76f8\u53cd\uff0c\u5bf9\u4e8e\u5927\u5757\u5185\u5b58\u7684\u5206\u914d\u6216\u8005\u9700\u8981\u5171\u4eab\u5185\u5b58\u7684\u573a\u666f\uff0c\u6bd4\u5982\u5728\u8de8\u8fdb\u7a0b\u901a\u4fe1\u4e2d\uff0c\u9700\u8981\u521b\u5efa\u5171\u4eab\u5185\u5b58\u533a\u57df\u6765\u5b9e\u73b0\u6570\u636e\u7684\u5171\u4eab\uff0c\u8fd9\u65f6\u5c31\u5e94\u8be5\u4f18\u5148\u4f7f\u7528mmap\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u63d0\u4f9b\u72ec\u7acb\u7684\u5185\u5b58\u7ba1\u7406\u548c\u5171\u4eab\u80fd\u529b\uff0c\u6ee1\u8db3\u4e1a\u52a1\u7684\u9700\u6c42\u3002<\/p>\n<p><strong>\u6027\u80fd profiling<\/strong>\uff1a\u4e3a\u4e86\u786e\u4fdd\u5185\u5b58\u5206\u914d\u7684\u6548\u7387\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7\u6027\u80fd\u5206\u6790\u5de5\u5177\u6765\u76d1\u63a7\u7cfb\u7edf\u8c03\u7528\u7684\u9891\u7387\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528perf trace -e mmap\u6216strace -f -e brk,mmap\u547d\u4ee4\u6765\u76d1\u63a7mmap\u548cbrk\u7cfb\u7edf\u8c03\u7528\u7684\u9891\u7387\uff0c\u4ece\u800c\u5b9a\u4f4d\u5230\u5185\u5b58\u5206\u914d\u7684\u4f4e\u6548\u70b9\u3002\u901a\u8fc7\u5206\u6790\u8fd9\u4e9b\u6570\u636e\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u9488\u5bf9\u6027\u5730\u4f18\u5316\u5185\u5b58\u5206\u914d\u7b56\u7565\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u6bd4\u5982\uff0c\u5982\u679c\u53d1\u73b0\u67d0\u4e2a\u6a21\u5757\u4e2d\u9891\u7e41\u5730\u8c03\u7528mmap\u6765\u5206\u914d\u5c0f\u5757\u5185\u5b58\uff0c\u5c31\u53ef\u4ee5\u8003\u8651\u4f18\u5316\u8be5\u6a21\u5757\u7684\u5185\u5b58\u5206\u914d\u65b9\u5f0f\uff0c\u6539\u4e3a\u4f7f\u7528brk\u6216\u8005\u5185\u5b58\u6c60\uff0c\u4ee5\u964d\u4f4e\u7cfb\u7edf\u8c03\u7528\u7684\u5f00\u9500\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u8fd0\u884c\u6548\u7387\u3002<\/p>\n<p>\u6587\u7ae0\u6765\u81ea\uff1a51CTO<\/p>\n<\/div>\n<\/div>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_27289\" class=\"pvc_stats total_only  \" data-element-id=\"27289\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" version=\"1.0\" viewBox=\"0 0 502 315\" preserveAspectRatio=\"xMidYMid meet\"><g transform=\"translate(0,332) scale(0.1,-0.1)\" fill=\"\" stroke=\"none\"><path d=\"M2394 3279 l-29 -30 -3 -207 c-2 -182 0 -211 15 -242 39 -76 157 -76 196 0 15 31 17 60 15 243 l-3 209 -33 29 c-26 23 -41 29 -80 29 -41 0 -53 -5 -78 -31z\"\/><path d=\"M3085 3251 c-45 -19 -58 -50 -96 -229 -47 -217 -49 -260 -13 -295 52 -53 146 -42 177 20 16 31 87 366 87 410 0 70 -86 122 -155 94z\"\/><path d=\"M1751 3234 c-13 -9 -29 -31 -37 -50 -12 -29 -10 -49 21 -204 19 -94 39 -189 45 -210 14 -50 54 -80 110 -80 34 0 48 6 76 34 21 21 34 44 34 59 0 14 -18 113 -40 219 -37 178 -43 195 -70 221 -36 32 -101 37 -139 11z\"\/><path d=\"M1163 3073 c-36 -7 -73 -59 -73 -102 0 -56 133 -378 171 -413 34 -32 83 -37 129 -13 70 36 67 87 -16 290 -86 209 -89 214 -129 231 -35 14 -42 15 -82 7z\"\/><path d=\"M3689 3066 c-15 -9 -33 -30 -42 -48 -48 -103 -147 -355 -147 -375 0 -98 131 -148 192 -74 13 15 57 108 97 206 80 196 84 226 37 273 -30 30 -99 39 -137 18z\"\/><path d=\"M583 2784 c-38 -19 -67 -74 -58 -113 9 -42 211 -354 242 -373 16 -10 45 -18 66 -18 51 0 107 52 107 100 0 39 -1 41 -124 234 -80 126 -108 162 -133 173 -41 17 -61 16 -100 -3z\"\/><path d=\"M4250 2784 c-14 -9 -74 -91 -133 -183 -95 -150 -107 -173 -107 -213 0 -55 33 -94 87 -104 67 -13 90 8 211 198 130 202 137 225 78 284 -27 27 -42 34 -72 34 -22 0 -50 -8 -64 -16z\"\/><path d=\"M2275 2693 c-553 -48 -1095 -270 -1585 -649 -135 -104 -459 -423 -483 -476 -23 -49 -22 -139 2 -186 73 -142 361 -457 571 -626 285 -228 642 -407 990 -497 242 -63 336 -73 660 -74 310 0 370 5 595 52 535 111 1045 392 1455 803 122 121 250 273 275 326 19 41 19 137 0 174 -41 79 -309 363 -465 492 -447 370 -946 591 -1479 653 -113 14 -422 18 -536 8z m395 -428 c171 -34 330 -124 456 -258 112 -119 167 -219 211 -378 27 -96 24 -300 -5 -401 -72 -255 -236 -447 -474 -557 -132 -62 -201 -76 -368 -76 -167 0 -236 14 -368 76 -213 98 -373 271 -451 485 -162 444 86 934 547 1084 153 49 292 57 452 25z m909 -232 c222 -123 408 -262 593 -441 76 -74 138 -139 138 -144 0 -16 -233 -242 -330 -319 -155 -123 -309 -223 -461 -299 l-81 -41 32 46 c18 26 49 83 70 128 143 306 141 649 -6 957 -25 52 -61 116 -79 142 l-34 47 45 -20 c26 -10 76 -36 113 -56z m-2057 25 c-40 -58 -105 -190 -130 -263 -110 -324 -59 -707 132 -981 25 -35 42 -64 37 -64 -19 0 -241 119 -326 174 -188 122 -406 314 -532 468 l-58 71 108 103 c185 178 428 349 672 473 66 33 121 60 123 61 2 0 -10 -19 -26 -42z\"\/><path d=\"M2375 1950 c-198 -44 -350 -190 -395 -379 -18 -76 -8 -221 19 -290 114 -284 457 -406 731 -260 98 52 188 154 231 260 27 69 37 214 19 290 -38 163 -166 304 -326 360 -67 23 -215 33 -279 19z\"\/><\/g><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/aif.amtbbs.org\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6df1\u5165\u63a2\u8ba8brk\u4e0emmap\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u6765\u660e\u786e\u4e00\u4e2a\u6982\u5ff5\uff1a\u5728 Linux \u7cfb\u7edf\u4e2d\uff0c\u5185\u5b58\u5206\u914d\u7684\u6838\u5fc3\u7cfb\u7edf\u8c03\u7528\u4e3b\u8981\u5c31\u662fb [&hellip;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_27289\" class=\"pvc_stats total_only  \" data-element-id=\"27289\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" version=\"1.0\" viewBox=\"0 0 502 315\" preserveAspectRatio=\"xMidYMid meet\"><g transform=\"translate(0,332) scale(0.1,-0.1)\" fill=\"\" stroke=\"none\"><path d=\"M2394 3279 l-29 -30 -3 -207 c-2 -182 0 -211 15 -242 39 -76 157 -76 196 0 15 31 17 60 15 243 l-3 209 -33 29 c-26 23 -41 29 -80 29 -41 0 -53 -5 -78 -31z\"\/><path d=\"M3085 3251 c-45 -19 -58 -50 -96 -229 -47 -217 -49 -260 -13 -295 52 -53 146 -42 177 20 16 31 87 366 87 410 0 70 -86 122 -155 94z\"\/><path d=\"M1751 3234 c-13 -9 -29 -31 -37 -50 -12 -29 -10 -49 21 -204 19 -94 39 -189 45 -210 14 -50 54 -80 110 -80 34 0 48 6 76 34 21 21 34 44 34 59 0 14 -18 113 -40 219 -37 178 -43 195 -70 221 -36 32 -101 37 -139 11z\"\/><path d=\"M1163 3073 c-36 -7 -73 -59 -73 -102 0 -56 133 -378 171 -413 34 -32 83 -37 129 -13 70 36 67 87 -16 290 -86 209 -89 214 -129 231 -35 14 -42 15 -82 7z\"\/><path d=\"M3689 3066 c-15 -9 -33 -30 -42 -48 -48 -103 -147 -355 -147 -375 0 -98 131 -148 192 -74 13 15 57 108 97 206 80 196 84 226 37 273 -30 30 -99 39 -137 18z\"\/><path d=\"M583 2784 c-38 -19 -67 -74 -58 -113 9 -42 211 -354 242 -373 16 -10 45 -18 66 -18 51 0 107 52 107 100 0 39 -1 41 -124 234 -80 126 -108 162 -133 173 -41 17 -61 16 -100 -3z\"\/><path d=\"M4250 2784 c-14 -9 -74 -91 -133 -183 -95 -150 -107 -173 -107 -213 0 -55 33 -94 87 -104 67 -13 90 8 211 198 130 202 137 225 78 284 -27 27 -42 34 -72 34 -22 0 -50 -8 -64 -16z\"\/><path d=\"M2275 2693 c-553 -48 -1095 -270 -1585 -649 -135 -104 -459 -423 -483 -476 -23 -49 -22 -139 2 -186 73 -142 361 -457 571 -626 285 -228 642 -407 990 -497 242 -63 336 -73 660 -74 310 0 370 5 595 52 535 111 1045 392 1455 803 122 121 250 273 275 326 19 41 19 137 0 174 -41 79 -309 363 -465 492 -447 370 -946 591 -1479 653 -113 14 -422 18 -536 8z m395 -428 c171 -34 330 -124 456 -258 112 -119 167 -219 211 -378 27 -96 24 -300 -5 -401 -72 -255 -236 -447 -474 -557 -132 -62 -201 -76 -368 -76 -167 0 -236 14 -368 76 -213 98 -373 271 -451 485 -162 444 86 934 547 1084 153 49 292 57 452 25z m909 -232 c222 -123 408 -262 593 -441 76 -74 138 -139 138 -144 0 -16 -233 -242 -330 -319 -155 -123 -309 -223 -461 -299 l-81 -41 32 46 c18 26 49 83 70 128 143 306 141 649 -6 957 -25 52 -61 116 -79 142 l-34 47 45 -20 c26 -10 76 -36 113 -56z m-2057 25 c-40 -58 -105 -190 -130 -263 -110 -324 -59 -707 132 -981 25 -35 42 -64 37 -64 -19 0 -241 119 -326 174 -188 122 -406 314 -532 468 l-58 71 108 103 c185 178 428 349 672 473 66 33 121 60 123 61 2 0 -10 -19 -26 -42z\"\/><path d=\"M2375 1950 c-198 -44 -350 -190 -395 -379 -18 -76 -8 -221 19 -290 114 -284 457 -406 731 -260 98 52 188 154 231 260 27 69 37 214 19 290 -38 163 -166 304 -326 360 -67 23 -215 33 -279 19z\"\/><\/g><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/aif.amtbbs.org\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":56,"featured_media":27290,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,20,80],"tags":[1876],"class_list":["post-27289","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-23","category-20","category-80","tag-mallocbrk-mmap"],"_links":{"self":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/27289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/users\/56"}],"replies":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/comments?post=27289"}],"version-history":[{"count":1,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/27289\/revisions"}],"predecessor-version":[{"id":27292,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/27289\/revisions\/27292"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/media\/27290"}],"wp:attachment":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/media?parent=27289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/categories?post=27289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/tags?post=27289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}