{"id":122582,"date":"2026-11-05T07:32:44","date_gmt":"2026-11-04T23:32:44","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/122582.html"},"modified":"2026-11-05T07:32:44","modified_gmt":"2026-11-04T23:32:44","slug":"netty-%e5%86%85%e5%ad%98%e7%ae%a1%e7%90%86%e8%af%a6%e8%a7%a3%ef%bc%9a%e4%bb%8e-bytebuf-%e5%88%b0%e5%86%85%e5%ad%98%e6%b1%a0%ef%bc%8c%e9%ab%98%e9%a2%91%e9%9d%a2%e8%af%95%e8%80%83%e7%82%b9%e4%b8%80","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/122582.html","title":{"rendered":"Netty \u5185\u5b58\u7ba1\u7406\u8be6\u89e3\uff1a\u4ece ByteBuf \u5230\u5185\u5b58\u6c60\uff0c\u9ad8\u9891\u9762\u8bd5\u8003\u70b9\u4e00\u6b21\u8bb2\u900f"},"content":{"rendered":"<h3>\u4e00\u3001\u4e3a\u4ec0\u4e48 Netty \u9700\u8981\u81ea\u5df1\u7684\u5185\u5b58\u7ba1\u7406<\/h3>\n<p>\u5728\u56de\u7b54\u300cNetty \u5185\u5b58\u7ba1\u7406\u300d\u4e4b\u524d&#xff0c;\u5148\u8981\u7406\u89e3\u4e00\u4e2a\u6700\u6839\u672c\u7684\u95ee\u9898&#xff1a;JDK \u660e\u660e\u5df2\u7ecf\u66ff\u6211\u4eec\u7ba1\u7406\u4e86\u5806\u5185\u5b58&#xff0c;\u4e3a\u4ec0\u4e48 Netty \u8fd8\u8981\u81ea\u5df1\u518d\u641e\u4e00\u5957\u5185\u5b58\u7ba1\u7406\u673a\u5236&#xff1f;\u8fd9\u4e2a\u95ee\u9898\u6070\u6070\u662f\u9762\u8bd5\u5b98\u6700\u60f3\u542c\u5230\u7684\u5207\u5165\u70b9\u3002<\/p>\n<p>JDK \u7684\u5806\u5185\u5b58\u7531 JVM \u81ea\u52a8\u7ba1\u7406&#xff0c;\u901a\u8fc7\u65b0\u751f\u4ee3\u3001\u8001\u5e74\u4ee3\u3001GC \u56de\u6536\u7b49\u65b9\u5f0f\u4fdd\u8bc1\u7a0b\u5e8f\u5458\u300c\u4e0d\u7528\u5173\u5fc3\u5185\u5b58\u91ca\u653e\u300d\u3002\u4f46\u8fd9\u79cd\u4fbf\u5229\u5728 NIO \u573a\u666f\u4e0b\u4f1a\u5e26\u6765\u4e09\u4e2a\u75db\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u5806\u5916\u5185\u5b58\u65e0\u6cd5\u88ab GC \u6709\u6548\u7ba1\u7406&#xff1a;Netty \u5e95\u5c42\u57fa\u4e8e Java NIO \u7684 Channel \u505a\u7f51\u7edc\u8bfb\u5199&#xff0c;NIO \u5728\u6536\u53d1\u6570\u636e\u65f6\u7ecf\u5e38\u4f7f\u7528 DirectByteBuffer&#xff08;\u5806\u5916\u5185\u5b58&#xff09;\u3002\u5806\u5916\u5185\u5b58\u4e0d\u5f52 JVM \u5806\u7ba1\u7406&#xff0c;\u53ea\u80fd\u901a\u8fc7\u865a\u5f15\u7528\u7b49\u65b9\u5f0f\u56de\u6536&#xff0c;\u56de\u6536\u65f6\u673a\u4e0d\u53ef\u63a7\u3002\u5982\u679c\u9891\u7e41\u521b\u5efa\u5927\u5757\u5806\u5916\u7f13\u51b2\u533a&#xff0c;\u5f88\u5bb9\u6613\u51fa\u73b0 OOM&#xff0c;\u800c\u4e14\u6392\u67e5\u56f0\u96be\u3002<\/li>\n<li>\u9891\u7e41\u5206\u914d\u548c\u56de\u6536\u7684\u6027\u80fd\u5f00\u9500&#xff1a;\u7f51\u7edc\u901a\u4fe1\u662f\u9ad8\u5e76\u53d1\u3001\u9ad8\u541e\u5410\u573a\u666f&#xff0c;\u6bcf\u6b21\u8bfb\u5199\u90fd\u4f1a\u521b\u5efa\u7f13\u51b2\u533a\u3002\u5982\u679c\u6bcf\u6b21\u90fd\u5411\u64cd\u4f5c\u7cfb\u7edf\u7533\u8bf7\u5185\u5b58&#xff0c;\u4f1a\u89e6\u53d1 malloc \u7cfb\u7edf\u8c03\u7528&#xff0c;\u6027\u80fd\u975e\u5e38\u5dee\u3002Netty \u9700\u8981\u50cf\u8fde\u63a5\u6c60\u4e00\u6837\u628a\u5185\u5b58\u300c\u7f13\u5b58\u300d\u8d77\u6765\u590d\u7528\u3002<\/li>\n<li>GC \u505c\u987f\u4e0e\u590d\u5236\u5f00\u9500&#xff1a;\u5806\u5185\u5b58\u5bf9\u8c61\u4f1a\u88ab GC \u79fb\u52a8\u3001\u590d\u5236\u3002\u5bf9\u4e8e\u5927\u91cf\u5c0f\u5bf9\u8c61\u7ec4\u6210\u7684\u7f51\u7edc\u6d88\u606f&#xff0c;\u9891\u7e41 GC \u4f1a\u9020\u6210\u541e\u5410\u4e0b\u964d\u3002\u5806\u5916\u5185\u5b58\u5219\u4e0d\u53d7 GC \u5f71\u54cd&#xff0c;\u8bfb\u5199\u66f4\u7a33\u5b9a\u3002<\/li>\n<\/ul>\n<p>\u56e0\u6b64 Netty \u501f\u9274\u4e86\u4e1a\u754c\u6210\u719f\u7684\u5185\u5b58\u5206\u914d\u5668 jemalloc \u7684\u8bbe\u8ba1\u601d\u60f3&#xff0c;\u81ea\u5df1\u5b9e\u73b0\u4e86\u4e00\u5957\u5b8c\u6574\u7684\u5185\u5b58\u7ba1\u7406\u7cfb\u7edf&#xff1a;\u901a\u8fc7\u6c60\u5316\u51cf\u5c11\u7cfb\u7edf\u8c03\u7528\u3001\u901a\u8fc7\u5185\u5b58\u89c4\u683c\u5316\u51cf\u5c11\u788e\u7247\u3001\u901a\u8fc7\u5f15\u7528\u8ba1\u6570\u7cbe\u786e\u63a7\u5236\u751f\u547d\u5468\u671f\u3002\u7406\u89e3\u4e86\u8fd9\u4e09\u4e2a\u52a8\u673a&#xff0c;\u540e\u6587\u7684\u6240\u6709\u8bbe\u8ba1\u5c31\u90fd\u6709\u4e86\u903b\u8f91\u652f\u70b9\u3002<\/p>\n<h3>\u4e8c\u3001\u5806\u5185\u5b58\u4e0e\u5806\u5916\u5185\u5b58\u7684\u6289\u62e9<\/h3>\n<p>Netty \u5185\u5b58\u7ba1\u7406\u7684\u7b2c\u4e00\u5c42&#xff0c;\u662f HeapBuffer \u548c DirectBuffer \u7684\u9009\u62e9\u3002\u4ee5 ByteBuf \u7684\u5e95\u5c42\u5b58\u50a8\u6765\u770b&#xff0c;\u4e3b\u8981\u6709\u4e09\u79cd&#xff1a;<\/p>\n<ul>\n<li>\u5806\u7f13\u51b2\u533a HeapBuffer&#xff1a;\u5e95\u5c42\u662f JVM \u5806\u5185\u90e8\u7684 byte[]&#xff0c;\u53d7 GC \u7ba1\u7406&#xff0c;\u5206\u914d\u5feb\u3001\u8bbf\u95ee\u5feb&#xff0c;\u4f46\u8fdb\u884c Socket \u4f20\u8f93\u65f6\u8981\u5148\u590d\u5236\u5230\u5806\u5916&#xff0c;\u591a\u4e00\u6b21\u62f7\u8d1d\u3002<\/li>\n<li>\u76f4\u63a5\u7f13\u51b2\u533a DirectBuffer&#xff1a;\u5e95\u5c42\u662f\u5806\u5916\u5185\u5b58&#xff0c;NIO \u7684 Channel \u53ef\u4ee5\u76f4\u63a5\u8bfb\u5199&#xff0c;\u7701\u6389\u4e00\u6b21\u590d\u5236&#xff0c;\u4f46\u5206\u914d\u548c\u91ca\u653e\u6210\u672c\u76f8\u5bf9\u9ad8&#xff0c;\u4e14\u4e0d\u53d7 GC \u76f4\u63a5\u6258\u7ba1\u3002<\/li>\n<li>\u590d\u5408\u7f13\u51b2\u533a CompositeByteBuf&#xff1a;\u5c06\u591a\u4e2a ByteBuf \u7ec4\u88c5\u6210\u4e00\u4e2a\u903b\u8f91\u4e0a\u7684\u7f13\u51b2\u533a&#xff0c;\u9002\u5408\u628a\u6d88\u606f\u5934\u548c\u6d88\u606f\u4f53\u5408\u5e76\u53d1\u9001&#xff0c;\u907f\u514d\u591a\u6b21\u62f7\u8d1d\u3002<\/li>\n<\/ul>\n<p>Netty \u63d0\u4f9b PooledByteBufAllocator.DEFAULT \u4f5c\u4e3a\u9ed8\u8ba4\u5206\u914d\u5668&#xff0c;\u9ed8\u8ba4\u4f18\u5148\u4f7f\u7528\u5806\u5916\u76f4\u63a5\u5185\u5b58\u3002\u9762\u8bd5\u4e2d\u5e38\u89c1\u7684\u8ffd\u95ee\u662f\u300c\u4e3a\u4ec0\u4e48\u8981\u7528\u5806\u5916\u5185\u5b58\u300d\u300c\u5806\u5916\u5185\u5b58\u4ec0\u4e48\u65f6\u5019\u88ab\u56de\u6536\u300d\u3002\u8981\u70b9\u5728\u4e8e&#xff1a;<\/p>\n<ul>\n<li>\u5806\u5916\u5185\u5b58\u4f1a\u5728 ByteBuffer \u88ab GC \u56de\u6536\u65f6\u901a\u8fc7 Cleaner \u673a\u5236\u89e6\u53d1\u91ca\u653e&#xff0c;\u4f46\u91ca\u653e\u65f6\u673a\u53d6\u51b3\u4e8e GC&#xff0c;\u4e0d\u5177\u5907\u786e\u5b9a\u6027\u3002<\/li>\n<li>Netty \u901a\u8fc7\u5f15\u7528\u8ba1\u6570 &#043; release() \u663e\u5f0f\u5f52\u8fd8\u5230\u5185\u5b58\u6c60&#xff0c;\u4ece\u800c\u8ba9\u5806\u5916\u5185\u5b58\u7684\u590d\u7528\u53d8\u5f97\u786e\u5b9a\u3001\u53ef\u63a7\u3002<\/li>\n<li>\u76f4\u63a5\u5185\u5b58\u5927\u5c0f\u4f1a\u53d7\u5230 -XX:MaxDirectMemorySize \u53c2\u6570\u9650\u5236&#xff0c;\u9ed8\u8ba4\u7ea6\u7b49\u4e8e\u5806\u7684\u6700\u5927\u503c&#xff0c;\u8d85\u9650\u4f1a\u629b OOM\u3002<\/li>\n<\/ul>\n<h3>\u4e09\u3001ByteBuf \u4f53\u7cfb&#xff1a;\u4f18\u4e8e JDK ByteBuffer \u7684\u8bbe\u8ba1<\/h3>\n<p>JDK \u7684 ByteBuffer \u4f7f\u7528\u5355\u4e00 position \u6307\u9488&#xff0c;\u8bfb\u5199\u5207\u6362\u9700\u8981\u8c03\u7528 flip()&#xff0c;\u5f88\u5bb9\u6613\u7528\u9519\u3002Netty \u7684 ByteBuf \u5219\u5f15\u5165\u53cc\u6307\u9488\u6a21\u578b&#xff0c;\u6210\u4e3a\u6574\u4e2a\u5185\u5b58\u7ba1\u7406\u7684\u8f7d\u4f53\u3002<\/p>\n<p>&#043;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-&#043;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#043;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#043;<br \/>\n|  discardable bytes |  readable bytes  |  writable bytes  |<br \/>\n|      0 &#8211; reader    | reader &#8211; writer   | writer &#8211; capacity|<br \/>\n&#043;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-&#043;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#043;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#043;<br \/>\n         readerIndex         writerIndex         capacity<\/p>\n<p>\u6838\u5fc3\u7d22\u5f15\u6982\u5ff5\u5982\u4e0b&#xff1a;<\/p>\n<ul>\n<li>readerIndex&#xff1a;\u8bfb\u6307\u9488&#xff0c;\u8bfb\u64cd\u4f5c\u4ece\u8fd9\u91cc\u5f00\u59cb&#xff0c;\u6bcf\u8bfb\u4e00\u4e2a\u5b57\u8282\u5411\u540e\u79fb\u52a8\u3002<\/li>\n<li>writerIndex&#xff1a;\u5199\u6307\u9488&#xff0c;\u5199\u64cd\u4f5c\u4ece\u8fd9\u91cc\u5f00\u59cb&#xff0c;\u6bcf\u5199\u4e00\u4e2a\u5b57\u8282\u5411\u540e\u79fb\u52a8\u3002<\/li>\n<li>capacity&#xff1a;\u7f13\u51b2\u533a\u603b\u5bb9\u91cf&#xff0c;\u53ef\u52a8\u6001\u6269\u5bb9\u3002<\/li>\n<li>\u53ef\u8bfb\u5b57\u8282\u6570&#xff1a;writerIndex &#8211; readerIndex\u3002<\/li>\n<li>\u53ef\u5199\u5b57\u8282\u6570&#xff1a;capacity &#8211; writerIndex\u3002<\/li>\n<\/ul>\n<p>\u76f8\u6bd4 JDK ByteBuffer&#xff0c;ByteBuf \u7684\u5173\u952e\u4f18\u52bf\u5728\u4e8e&#xff1a;\u8bfb\u5199\u5206\u79bb\u3001\u5bb9\u91cf\u52a8\u6001\u6269\u5c55\u3001\u652f\u6301\u6c60\u5316\u3001\u652f\u6301\u5f15\u7528\u8ba1\u6570\u3001\u652f\u6301\u96f6\u62f7\u8d1d\u7684\u6d3e\u751f\u7f13\u51b2\u533a&#xff08;slice\u3001duplicate&#xff09;\u3002\u6b64\u5916 Netty \u8fd8\u629b\u5f03\u4e86 JDK flip() \u90a3\u79cd\u5bb9\u6613\u9519\u7684\u72b6\u6001\u5207\u6362&#xff0c;\u8ba9\u8bfb\u5199\u64cd\u4f5c\u5929\u7136\u5b89\u5168\u3002<\/p>\n<h4>3.1 \u96f6\u62f7\u8d1d\u76f8\u5173\u7684\u6d3e\u751f\u7f13\u51b2\u533a<\/h4>\n<p>\u300c\u96f6\u62f7\u8d1d\u300d\u662f Netty \u9762\u8bd5\u7684\u53e6\u4e00\u4e2a\u9ad8\u9891\u8bcd&#xff0c;\u5b83\u5728\u5185\u5b58\u7ba1\u7406\u8bed\u5883\u4e0b\u4e3b\u8981\u4f53\u73b0\u5728 ByteBuf \u7684\u6d3e\u751f\u64cd\u4f5c&#xff1a;<\/p>\n<ul>\n<li>slice()&#xff1a;\u5171\u4eab\u540c\u4e00\u5757\u5e95\u5c42\u6570\u636e&#xff0c;\u521b\u5efa\u903b\u8f91\u4e0a\u7684\u5b50\u7f13\u51b2\u533a&#xff0c;\u4e0d\u590d\u5236\u6570\u636e\u3002<\/li>\n<li>duplicate()&#xff1a;\u5171\u4eab\u540c\u4e00\u5757\u6570\u636e&#xff0c;\u521b\u5efa\u72ec\u7acb\u7684\u8bfb\u5199\u7d22\u5f15\u89c6\u56fe\u3002<\/li>\n<li>readSlice()&#xff1a;\u8bfb\u53d6\u4e00\u6bb5\u5e76\u8fd4\u56de slice&#xff0c;\u6307\u9488\u81ea\u52a8\u524d\u8fdb&#xff0c;\u5e38\u7528\u4e8e\u62c6\u5305\u3002<\/li>\n<li>CompositeByteBuf&#xff1a;\u628a\u591a\u4e2a ByteBuf \u7ec4\u5408\u6210\u4e00\u4e2a\u7f13\u51b2\u533a&#xff0c;\u907f\u514d\u5728\u5408\u5e76\u6d88\u606f\u65f6\u628a\u6570\u636e\u62f7\u6765\u62f7\u53bb\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u64cd\u4f5c\u5171\u4eab\u5185\u5b58\u4f1a\u5e26\u6765\u4e00\u4e2a\u98ce\u9669&#xff1a;\u591a\u4e2a ByteBuf \u5171\u540c\u6301\u6709\u540c\u4e00\u4efd\u6570\u636e&#xff0c;\u91ca\u653e\u4e86\u5176\u4e2d\u4e00\u4e2a\u800c\u4e0d\u5c0f\u5fc3\u8fd8\u5728\u7528\u53e6\u4e00\u4e2a&#xff0c;\u5c31\u4f1a\u8bfb\u5230\u5df2\u56de\u6536\u7684\u6570\u636e\u3002\u56e0\u6b64 Netty \u5f15\u5165\u5f15\u7528\u8ba1\u6570&#xff0c;\u6bcf\u6d3e\u751f\u4e00\u6b21\u5f15\u7528\u8ba1\u6570\u52a0\u4e00&#xff0c;release() \u65f6\u8ba1\u6570\u51cf\u4e00&#xff0c;\u5f52\u96f6\u540e\u624d\u771f\u6b63\u5f52\u8fd8\u5185\u5b58\u3002<\/p>\n<h3>\u56db\u3001\u5f15\u7528\u8ba1\u6570&#xff1a;\u5185\u5b58\u751f\u547d\u5468\u671f\u7684\u5173\u952e<\/h3>\n<p>Netty \u5185\u5b58\u7ba1\u7406\u4e2d\u6700\u5bb9\u6613\u88ab\u5ffd\u89c6\u3001\u4f46\u9762\u8bd5\u6700\u5bb9\u6613\u88ab\u8ffd\u95ee\u7684\u673a\u5236&#xff0c;\u5c31\u662f\u5f15\u7528\u8ba1\u6570&#xff08;Reference Counting&#xff09;\u3002<\/p>\n<p>\u7edd\u5927\u591a\u6570\u6c60\u5316\u7f13\u51b2\u533a\u90fd\u5b9e\u73b0\u4e86 ReferenceCounted \u63a5\u53e3\u3002\u7b80\u5355\u6765\u8bf4&#xff1a;<\/p>\n<ul>\n<li>\u5bf9\u8c61\u521b\u5efa\u65f6\u5f15\u7528\u8ba1\u6570\u521d\u59cb\u503c\u4e3a 1\u3002<\/li>\n<li>\u6bcf\u6b21 retain()&#xff0c;\u8ba1\u6570\u52a0 1&#xff0c;\u8868\u793a\u300c\u591a\u4e00\u4e2a\u4f7f\u7528\u8005\u300d\u3002<\/li>\n<li>\u6bcf\u6b21 release()&#xff0c;\u8ba1\u6570\u51cf 1&#xff0c;\u9664\u975e\u7279\u6b8a\u5b9e\u73b0&#xff08;\u5982\u67d0\u4e9b\u76f4\u63a5\u8fd4\u56de boolean \u7684\u7248\u672c&#xff09;&#xff0c;\u5426\u5219\u4e0d\u629b\u5f02\u5e38\u3002<\/li>\n<li>\u5f53\u8ba1\u6570\u964d\u5230 0 \u65f6&#xff0c;\u5e95\u5c42\u5185\u5b58\u624d\u771f\u6b63\u88ab\u56de\u6536\u5e76\u653e\u56de\u5185\u5b58\u6c60\u3002<\/li>\n<\/ul>\n<p>ByteBuf buf &#061; PooledByteBufAllocator.DEFAULT.buffer(1024);<br \/>\nbuf.writeBytes(&#034;hello&#034;.getBytes());<br \/>\n\/\/ \u4ea4\u7ed9\u53e6\u4e00\u4e2a\u7ec4\u4ef6\u7ee7\u7eed\u4f7f\u7528&#xff0c;\u5f15\u7528\u8ba1\u6570 &#043;1<br \/>\nbuf.retain();<br \/>\n\/\/ \u5f53\u524d\u7ec4\u4ef6\u4f7f\u7528\u5b8c\u6bd5<br \/>\nbuf.release();<br \/>\n\/\/ \u53e6\u4e00\u4e2a\u7ec4\u4ef6\u4f7f\u7528\u5b8c\u6bd5\u540e\u518d\u6b21 release&#xff0c;\u8ba1\u6570\u5f52\u96f6&#xff0c;\u5185\u5b58\u5f52\u8fd8\u6c60\u4e2d<br \/>\nbuf.release();<\/p>\n<p>\u9762\u8bd5\u4e2d\u9ad8\u9891\u7684\u8ffd\u95ee\u662f\u300cNetty \u4e2d\u8c01\u8d1f\u8d23 release\u300d\u3002\u7b54\u6848\u662f&#xff1a;\u6700\u540e\u4e00\u4e2a\u4f7f\u7528\u8be5 ByteBuf \u7684\u7ec4\u4ef6\u8d1f\u8d23\u91ca\u653e\u3002\u5728\u5165\u7ad9\u65b9\u5411&#xff0c;SimpleChannelInboundHandler \u4f1a\u81ea\u52a8\u91ca\u653e\u6d88\u606f&#xff1b;\u5982\u679c\u4f7f\u7528\u666e\u901a\u7684 ChannelInboundHandlerAdapter&#xff0c;\u5219\u9700\u8981\u81ea\u5df1\u8c03\u7528 ReferenceCountUtil.release(msg)\u3002\u5728\u51fa\u7ad9\u65b9\u5411&#xff0c;\u5199\u51fa\u64cd\u4f5c\u4f1a\u5728\u53d1\u9001\u5b8c\u6210\u540e\u81ea\u52a8\u91ca\u653e\u5bf9\u5e94\u7684 Buffer\u3002<\/p>\n<p>\u5f15\u7528\u8ba1\u6570\u8fd8\u6709\u4e00\u4e2a\u9690\u853d\u7684\u5751&#xff1a;slice()\u3001duplicate() \u751f\u6210\u7684\u6d3e\u751f\u7f13\u51b2\u533a\u4e0e\u539f\u7f13\u51b2\u533a\u5171\u4eab\u8ba1\u6570&#xff0c;\u4f7f\u7528\u4e0d\u5f53\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u6216\u8fc7\u65e9\u91ca\u653e\u3002\u56e0\u6b64\u6700\u4f73\u5b9e\u8df5\u662f\u300c\u8c01\u6700\u540e\u6d88\u8d39&#xff0c;\u8c01 release\u300d&#xff0c;\u5e76\u4e14\u5c3d\u91cf\u8ba9\u7f13\u51b2\u533a\u804c\u8d23\u5355\u4e00\u3002<\/p>\n<h3>\u4e94\u3001Netty \u5185\u5b58\u89c4\u683c\u5316&#xff1a;\u628a\u5927\u5c0f\u5206\u95e8\u522b\u7c7b<\/h3>\n<p>\u5982\u679c\u8981\u7ed9 Netty \u5185\u5b58\u7ba1\u7406\u753b\u4e00\u5f20\u5168\u666f\u56fe&#xff0c;\u6700\u6838\u5fc3\u7684\u4e00\u5f20\u56fe\u5c31\u662f\u300c\u5185\u5b58\u89c4\u683c &#043; \u5c42\u7ea7\u5206\u914d\u300d&#xff1a;<\/p>\n<ul>\n<li>\u6309\u5927\u5c0f\u5206\u7c7b&#xff08;SizeClass&#xff09;&#xff1a;\u628a\u5185\u5b58\u8bf7\u6c42\u5206\u6210 Tiny\u3001Small\u3001Normal\u3001Huge \u56db\u7c7b\u3002<\/li>\n<li>\u6309\u89c4\u683c\u6570\u91cf\u5206\u6863&#xff08;SizeClasses&#xff09;&#xff1a;\u6bcf\u4e00\u7c7b\u5185\u90e8\u518d\u7ec6\u5206\u4e3a\u591a\u4e2a\u89c4\u683c&#xff0c;\u5206\u914d\u65f6\u5411\u4e0a\u53d6\u6574\u5230\u6700\u63a5\u8fd1\u7684\u89c4\u683c&#xff0c;\u51cf\u5c11\u5916\u90e8\u788e\u7247\u3002<\/li>\n<li>\u6309\u5206\u914d\u5c42\u7ea7\u5206\u5de5&#xff1a;\u5c0f\u5185\u5b58\u8d70 PoolSubpage&#xff0c;\u4e2d\u5185\u5b58\u8d70 PoolChunk \u7684\u4f19\u4f34\u5206\u914d&#xff0c;\u5927\u5185\u5b58\u8d70\u72ec\u7acb\u7684 Chunk&#xff0c;\u8d85\u5927\u5185\u5b58\u76f4\u63a5\u843d\u5728\u5806\u5916\u4e0d\u6c60\u5316\u3002<\/li>\n<\/ul>\n<h4>5.1 \u56db\u7c7b\u5185\u5b58\u89c4\u683c<\/h4>\n<p>Netty \u9ed8\u8ba4\u628a\u6bcf\u6b21\u7533\u8bf7\u7684\u5185\u5b58\u5206\u7c7b\u5982\u4e0b&#xff08;\u4e0d\u540c\u7248\u672c\u9608\u503c\u53ef\u80fd\u7565\u6709\u5dee\u5f02&#xff0c;\u9700\u7ed3\u5408\u5177\u4f53\u7248\u672c\u8bf4\u660e&#xff09;&#xff1a;<\/p>\n<table>\n<tr>\u5206\u7c7b\u5927\u5c0f\u8303\u56f4&#xff08;\u9ed8\u8ba4&#xff09;\u7ba1\u7406\u65b9\u5f0f<\/tr>\n<tbody>\n<tr>\n<td>Tiny<\/td>\n<td>\u5c0f\u4e8e 512B<\/td>\n<td>\u7531 PoolSubpage \u7cbe\u7ec6\u7ba1\u7406&#xff0c;\u6700\u5c0f\u6863\u901a\u5e38\u4e3a 16B<\/td>\n<\/tr>\n<tr>\n<td>Small<\/td>\n<td>512B \u5230\u7ea6 8KB&#xff08;\u90e8\u5206\u573a\u666f\u5230 32KB \u9644\u8fd1&#xff09;<\/td>\n<td>\u7531 PoolSubpage \u7ba1\u7406\u5c0f\u9875<\/td>\n<\/tr>\n<tr>\n<td>Normal<\/td>\n<td>\u7ea6 8KB \u5230 16MB&#xff08;\u4e00\u4e2a Chunk \u7684\u9ed8\u8ba4\u5927\u5c0f&#xff09;<\/td>\n<td>\u7531 PoolChunk \u7528\u4f19\u4f34\u7b97\u6cd5\u5207\u5206 Page<\/td>\n<\/tr>\n<tr>\n<td>Huge<\/td>\n<td>\u5927\u4e8e Chunk \u5927\u5c0f\u7684\u8bf7\u6c42<\/td>\n<td>\u4e0d\u6c60\u5316&#xff0c;\u76f4\u63a5\u7533\u8bf7&#xff0c;\u7528\u5b8c\u76f4\u63a5\u91ca\u653e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u6837\u5206\u7c7b\u7684\u6838\u5fc3\u76ee\u7684\u662f\u7528\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\u9002\u914d\u4e0d\u540c\u7684\u5206\u914d\u89c4\u6a21&#xff1a;\u5c0f\u5757\u5185\u5b58\u7528\u4f4d\u56fe\u7ba1\u7406\u7701\u7a7a\u95f4&#xff0c;\u5927\u5757\u5185\u5b58\u7528\u4f19\u4f34\u6811\u51cf\u5c11\u788e\u7247\u3002\u533a\u5206 Tiny\/Small \u8fd8\u80fd\u8ba9\u7ebf\u7a0b\u7f13\u5b58\u66f4\u9ad8\u6548\u5730\u547d\u4e2d\u5e38\u7528\u7684\u5c0f\u5bf9\u8c61\u3002<\/p>\n<h4>5.2 \u89c4\u683c\u5411\u4e0a\u53d6\u6574<\/h4>\n<p>\u5047\u8bbe\u7528\u6237\u7533\u8bf7 100 \u5b57\u8282&#xff0c;Netty \u4e0d\u4f1a\u771f\u7684\u4ece\u64cd\u4f5c\u7cfb\u7edf\u5207\u51fa 100 \u5b57\u8282&#xff0c;\u800c\u662f\u628a\u5b83\u89c4\u6574\u5230 128B \u8fd9\u4e00\u6863\u3002\u8fd9\u6837\u7684\u597d\u5904\u662f\u5185\u5b58\u5757\u5927\u5c0f\u89c4\u6574&#xff0c;\u65b9\u4fbf\u590d\u7528\u548c\u5408\u5e76&#xff1b;\u4ee3\u4ef7\u662f\u53ef\u80fd\u4ea7\u751f\u300c\u5185\u90e8\u788e\u7247\u300d&#xff0c;\u4f46\u6362\u6765\u5206\u914d\u901f\u5ea6\u7684\u63d0\u5347\u548c\u6574\u4f53\u788e\u7247\u7684\u6709\u6548\u63a7\u5236\u3002<\/p>\n<h3>\u516d\u3001\u5185\u5b58\u6c60\u6838\u5fc3\u7ec4\u4ef6&#xff1a;PooledByteBufAllocator<\/h3>\n<p>Netty \u6c60\u5316\u5185\u5b58\u7684\u5165\u53e3\u662f PooledByteBufAllocator&#xff0c;\u5b83\u662f\u6574\u4e2a\u5185\u5b58\u7ba1\u7406\u7cfb\u7edf\u7684\u95e8\u9762\u3002\u4f7f\u7528\u8005\u53ea\u9700\u8981\u8c03\u7528&#xff1a;<\/p>\n<p>ByteBuf buf &#061; PooledByteBufAllocator.DEFAULT.buffer(1024);<\/p>\n<p>\u8fd9\u4e00\u884c\u4ee3\u7801\u80cc\u540e&#xff0c;Netty \u6309\u7167\u300c\u7ebf\u7a0b\u4eb2\u548c\u300d\u7684\u601d\u8def\u5b8c\u6210\u4e86\u4e00\u6574\u5957\u5206\u914d\u3002\u6838\u5fc3\u7ec4\u4ef6\u53ca\u5176\u804c\u8d23\u5982\u4e0b&#xff1a;<\/p>\n<ul>\n<li>PooledByteBufAllocator&#xff1a;\u5168\u5c40\u5206\u914d\u5668&#xff0c;\u6301\u6709\u5185\u90e8\u7684 PoolArena[]\u3002\u5806\u5185\u3001\u5806\u5916\u5404\u6709\u4e00\u7ec4 Arena&#xff0c;\u7ebf\u7a0b\u901a\u8fc7\u53d6\u6a21\/\u4f4d\u8fd0\u7b97\u7ed1\u5b9a\u5230\u67d0\u4e2a Arena\u3002<\/li>\n<li>PoolArena&#xff1a;\u5185\u5b58\u5212\u5206\u7684\u57fa\u672c\u5355\u5143&#xff0c;\u4e00\u4e2a Arena \u7ba1\u7406\u4e00\u6279 PoolChunkList \u548c\u5c0f\u578b\u5185\u5b58\u7684 PoolSubpage\u3002\u5b83\u662f\u7ebf\u7a0b\u771f\u6b63\u6253\u4ea4\u9053\u7684\u5206\u914d\u5165\u53e3\u3002<\/li>\n<li>PoolChunkList&#xff1a;\u6309 Chunk \u4f7f\u7528\u7387\u628a Chunk \u4e32\u8054\u6210\u53cc\u5411\u94fe\u8868&#xff0c;\u65b9\u4fbf\u5feb\u901f\u627e\u5230\u5408\u9002\u7684 Chunk\u3002<\/li>\n<li>PoolChunk&#xff1a;\u9ed8\u8ba4 16MB&#xff08;\u53ef\u914d\u7f6e&#xff09;\u7684\u8fde\u7eed\u5185\u5b58\u5757&#xff0c;\u5185\u90e8\u7528\u4e00\u68f5\u5b8c\u5168\u4e8c\u53c9\u6811\u8fdb\u884c\u4f19\u4f34\u5207\u5206\u3002<\/li>\n<li>PoolSubpage&#xff1a;\u628a\u4e00\u4e2a Page&#xff08;\u9ed8\u8ba4 8KB&#xff09;\u7ee7\u7eed\u7b49\u5206\u4e3a\u5c0f\u5757&#xff0c;\u4e13\u95e8\u8d1f\u8d23 Tiny \u548c Small \u5185\u5b58\u3002<\/li>\n<li>PoolThreadCache&#xff1a;\u7ebf\u7a0b\u672c\u5730\u7f13\u5b58&#xff0c;\u4f18\u5148\u4ece\u7f13\u5b58\u4e2d\u5206\u914d\u548c\u56de\u6536&#xff0c;\u51cf\u5c11\u5bf9 Arena \u7684\u9501\u7ade\u4e89\u3002<\/li>\n<\/ul>\n<p>\u7406\u89e3\u8fd9\u5957\u7ec4\u4ef6\u4e4b\u95f4\u7684\u5173\u7cfb&#xff0c;\u662f\u7b54\u597d\u300cNetty \u5185\u5b58\u6c60\u600e\u4e48\u5de5\u4f5c\u300d\u7684\u5173\u952e\u3002\u53ef\u4ee5\u7528\u4e00\u53e5\u8bdd\u6982\u62ec&#xff1a;Allocator \u8d1f\u8d23\u8def\u7531&#xff0c;Arena \u8d1f\u8d23\u6258\u7ba1&#xff0c;Chunk \u8d1f\u8d23\u5207\u5927\u5757&#xff0c;Subpage \u8d1f\u8d23\u5207\u5c0f\u5757&#xff0c;ThreadCache \u8d1f\u8d23\u63d0\u901f\u3002<\/p>\n<h3>\u4e03\u3001PoolArena&#xff1a;\u7ebf\u7a0b\u4e0e\u5185\u5b58\u7684\u5929\u7136\u7ed1\u5b9a<\/h3>\n<p>PoolArena \u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u300c\u5206\u914d\u7ade\u6280\u573a\u300d\u3002Netty \u9ed8\u8ba4\u4f1a\u521b\u5efa 2 * CPU \u6838\u6570 \u4e2a Arena&#xff0c;\u5806\u5185\u548c\u5806\u5916\u5404\u4e00\u534a\u3002\u6bcf\u4e2a\u7ebf\u7a0b\u6839\u636e PoolThreadCache \u4e2d\u7f13\u5b58\u7684 Arena \u7d22\u5f15\u6765\u7ed1\u5b9a&#xff0c;\u6216\u8005\u6309\u7ebf\u7a0b\u54c8\u5e0c\u6620\u5c04\u5230\u67d0\u4e2a Arena\u3002<\/p>\n<p>\u8fd9\u6837\u8bbe\u8ba1\u7684\u597d\u5904\u662f&#xff1a;\u7ebf\u7a0b\u901a\u5e38\u53ea\u5728\u81ea\u5df1\u7ed1\u5b9a\u7684 Arena \u4e0a\u5206\u914d&#xff0c;\u6781\u5927\u964d\u4f4e\u9501\u7ade\u4e89\u3002\u53ea\u6709\u5f53\u7ebf\u7a0b\u7f13\u5b58\u672a\u547d\u4e2d\u65f6\u624d\u8d70\u5230 Arena \u7684\u5206\u914d\u903b\u8f91&#xff0c;\u800c Arena \u5185\u90e8\u4ecd\u4f1a\u7528\u7ec6\u7c92\u5ea6\u540c\u6b65\u4fdd\u62a4\u5171\u4eab\u7ed3\u6784\u3002<\/p>\n<p>\u503c\u5f97\u5f3a\u8c03\u7684\u4e00\u70b9\u662f\u300c\u7ebf\u7a0b\u4e0e Arena \u5e76\u975e\u4e25\u683c\u4e00\u5bf9\u4e00\u300d\u3002\u7ebf\u7a0b\u4f1a\u5c3d\u91cf\u590d\u7528\u4e0a\u6b21\u7ed1\u5b9a\u7684 Arena&#xff0c;\u4f46\u67d0\u4e9b\u60c5\u51b5\u4e0b&#xff08;\u5982\u9996\u6b21\u5206\u914d\u3001\u7ebf\u7a0b\u9500\u6bc1\u91cd\u5efa&#xff09;\u4f1a\u91cd\u65b0\u9009\u62e9\u3002\u56e0\u6b64 Netty \u5e76\u6ca1\u6709\u5b8c\u5168\u6d88\u706d\u9501&#xff0c;\u800c\u662f\u901a\u8fc7\u300c\u5148\u7f13\u5b58\u540e\u5171\u4eab\u300d\u7684\u7b56\u7565\u628a\u9501\u7ade\u4e89\u538b\u7f29\u5230\u6700\u4f4e\u3002<\/p>\n<h3>\u516b\u3001PoolChunk&#xff1a;\u4f19\u4f34\u7b97\u6cd5\u7684\u4e8c\u53c9\u6811<\/h3>\n<p>PoolChunk \u662f Netty \u7ba1\u7406 Normal \u5185\u5b58\u7684\u57fa\u77f3&#xff0c;\u5b83\u501f\u9274\u4e86 jemalloc \u7684\u4f19\u4f34\u5206\u914d\u601d\u60f3\u3002\u4e00\u4e2a\u9ed8\u8ba4\u7684 Chunk \u662f 16MB&#xff0c;\u5185\u90e8\u88ab\u5212\u5206\u4e3a 2048 \u4e2a Page&#xff0c;\u6bcf\u4e2a Page 8KB\u3002<\/p>\n<p>\u4e3a\u4e86\u5feb\u901f\u627e\u5230\u5408\u9002\u7684\u8fde\u7eed Page&#xff0c;Chunk \u7ef4\u62a4\u4e86\u4e00\u68f5\u5b8c\u5168\u4e8c\u53c9\u6811&#xff0c;\u53f6\u5b50\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a Page&#xff1a;<\/p>\n<ul>\n<li>\u6839\u8282\u70b9\u4ee3\u8868\u6574\u4e2a Chunk \u7684 16MB\u3002<\/li>\n<li>\u6bcf\u4e00\u5c42\u5f80\u4e0b&#xff0c;\u8282\u70b9\u8868\u793a\u7684\u5185\u5b58\u533a\u95f4\u4e00\u5206\u4e3a\u4e8c&#xff0c;\u8fd9\u6b63\u662f\u300c\u4f19\u4f34\u300d\u7684\u542b\u4e49\u3002<\/li>\n<li>\u8282\u70b9\u4e0a\u8bb0\u5f55\u8be5\u533a\u95f4\u5185\u6700\u5927\u8fde\u7eed\u53ef\u5206\u914d\u7684 Page \u6570\u91cf\u3002<\/li>\n<\/ul>\n<p>                     [2048 pages]<br \/>\n                       \/        \\\\<br \/>\n              [1024 pages]     [1024 pages]<br \/>\n               \/      \\\\          \/      \\\\<br \/>\n          [512p]   [512p]     [512p]   [512p]<br \/>\n           &#8230;      &#8230;        &#8230;      &#8230;<br \/>\n          [1p] [1p] [1p] [1p]  &#8230;  [1p] [1p]<\/p>\n<p>\u5206\u914d\u65f6&#xff0c;\u4ece\u6839\u5f00\u59cb\u6309\u6df1\u5ea6\u4f18\u5148\u627e\u5230\u7b2c\u4e00\u4e2a\u6ee1\u8db3\u9700\u6c42\u7684\u8282\u70b9&#xff1b;\u5982\u679c\u9700\u6c42\u6070\u597d\u80fd\u7531\u67d0\u4e2a\u8282\u70b9\u5b8c\u5168\u627f\u8f7d&#xff0c;\u5219\u8be5\u8282\u70b9\u6574\u5757\u5206\u914d&#xff0c;\u5426\u5219\u7ee7\u7eed\u5411\u4e0b\u62c6\u3002\u91ca\u653e\u65f6&#xff0c;\u53ea\u6709\u5f53\u4e24\u4e2a\u76f8\u90bb\u5144\u5f1f\u8282\u70b9\u90fd\u7a7a\u95f2&#xff0c;\u624d\u4f1a\u628a\u5b83\u4eec\u5408\u5e76\u56de\u7236\u8282\u70b9&#xff0c;\u907f\u514d\u5916\u90e8\u788e\u7247\u3002<\/p>\n<p>\u4f8b\u5982\u7533\u8bf7 32KB&#xff08;4 \u4e2a Page&#xff09;&#xff0c;Netty \u4f1a\u6cbf\u6811\u627e\u5230\u6df1\u5ea6\u7b49\u4e8e log2(4) \u7684\u7a7a\u95f2\u8282\u70b9&#xff1b;\u82e5\u8be5\u5c42\u6ca1\u6709\u5408\u9002\u8282\u70b9&#xff0c;\u5c31\u5411\u4e0a\u56de\u6eaf\u3002\u8fd9\u5957\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8fd1\u4f3c O(logN)&#xff0c;\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<h3>\u4e5d\u3001PoolSubpage&#xff1a;\u5c0f\u5757\u5185\u5b58\u7684\u7cbe\u7ec6\u5316\u7ba1\u7406<\/h3>\n<p>\u5982\u679c\u6bcf\u4e2a Tiny\/Small \u8bf7\u6c42\u90fd\u76f4\u63a5\u5207 Page&#xff0c;\u4f1a\u9020\u6210\u5de8\u5927\u6d6a\u8d39&#xff1a;1 \u5b57\u8282\u7684\u8bf7\u6c42\u5360\u6389 8KB \u7684 Page&#xff0c;\u5185\u90e8\u788e\u7247\u592a\u4e25\u91cd\u3002\u4e8e\u662f Netty \u5f15\u5165 PoolSubpage\u3002<\/p>\n<p>\u4e00\u4e2a Page&#xff08;\u9ed8\u8ba4 8KB&#xff09;\u4f1a\u88ab\u7b49\u5206\u6210\u4e00\u6279\u300c\u5143\u7d20\u300d&#xff0c;\u6bcf\u4e2a\u5143\u7d20\u5927\u5c0f\u76f8\u540c\u3002\u6bd4\u5982 Page \u88ab\u5207\u6210 512 \u4e2a 16B \u7684\u5c0f\u5757&#xff0c;\u6216\u8005 64 \u4e2a 128B \u7684\u5c0f\u5757\u3002PoolSubpage \u5185\u90e8\u7528\u4f4d\u56fe&#xff08;bitmap&#xff09;\u8bb0\u5f55\u54ea\u4e9b\u5143\u7d20\u5df2\u88ab\u5206\u914d&#xff1a;<\/p>\n<ul>\n<li>\u5206\u914d\u65f6&#xff0c;\u627e\u5230\u7b2c\u4e00\u4e2a\u7a7a\u95f2 bit&#xff0c;\u6807\u8bb0\u4e3a\u5df2\u7528\u5e76\u8fd4\u56de\u5bf9\u5e94\u5185\u5b58\u533a\u95f4\u3002<\/li>\n<li>\u91ca\u653e\u65f6&#xff0c;\u628a\u5bf9\u5e94 bit \u6e05\u96f6\u3002<\/li>\n<li>\u5f53\u6574\u4e2a Subpage \u7684\u6240\u6709\u5143\u7d20\u90fd\u7a7a\u95f2\u65f6&#xff0c;Subpage \u53ef\u4ee5\u88ab\u56de\u6536&#xff0c;Page \u5f52\u8fd8\u7ed9 Chunk\u3002<\/li>\n<\/ul>\n<p>PoolSubpage \u901a\u5e38\u6302\u5728 PoolArena \u7684\u5c0f\u578b\u5185\u5b58\u89c6\u56fe\u91cc&#xff0c;\u6309\u5143\u7d20\u5927\u5c0f\u5206\u7ec4\u7ef4\u62a4\u3002Netty \u4f1a\u628a\u76f8\u540c\u89c4\u683c\u7684 Subpage \u4e32\u6210\u94fe\u8868&#xff0c;\u5206\u914d\u65f6\u4f18\u5148\u627e\u300c\u8fd8\u6709\u7a7a\u95f2\u4f4d\u300d\u7684 Subpage&#xff1b;\u5982\u679c\u90fd\u6ca1\u6709\u7a7a\u95f2&#xff0c;\u5c31\u5411 Chunk \u7533\u8bf7\u4e00\u4e2a\u65b0\u7684 Page \u5e76\u5207\u6210\u65b0\u7684 Subpage\u3002<\/p>\n<p>\u8fd9\u79cd\u300cPage \u5185\u7b49\u5206 &#043; \u4f4d\u56fe\u7ba1\u7406\u300d\u7684\u65b9\u5f0f&#xff0c;\u65e2\u4fdd\u8bc1\u4e86\u5c0f\u5185\u5b58\u5206\u914d\u63a5\u8fd1 O(1)&#xff0c;\u53c8\u5145\u5206\u5229\u7528\u4e86 Page&#xff0c;\u5927\u5e45\u964d\u4f4e\u5185\u90e8\u788e\u7247\u3002<\/p>\n<h3>\u5341\u3001PoolThreadCache&#xff1a;\u8ba9\u5feb\u8def\u5f84\u66f4\u5feb\u7684\u7ebf\u7a0b\u7f13\u5b58<\/h3>\n<p>\u5982\u679c\u6bcf\u6b21\u5206\u914d\u90fd\u76f4\u63a5\u8bbf\u95ee Arena&#xff0c;\u9ad8\u9891\u573a\u666f\u4ecd\u4f1a\u6709\u4e00\u5b9a\u7684\u540c\u6b65\u6210\u672c\u548c CPU \u7f13\u5b58\u4e0d\u547d\u4e2d\u3002\u56e0\u6b64 Netty \u589e\u52a0\u4e86\u4e00\u5c42 PoolThreadCache&#xff0c;\u5145\u5f53\u300c\u7ebf\u7a0b\u79c1\u6709\u7684\u5185\u5b58\u5c0f\u4ed3\u5e93\u300d\u3002<\/p>\n<p>PoolThreadCache \u5bf9 Tiny\u3001Small\u3001Normal \u5206\u522b\u7ef4\u62a4\u4e86\u7f13\u5b58\u961f\u5217&#xff0c;\u91ca\u653e\u7684\u5185\u5b58\u4e0d\u4f1a\u9a6c\u4e0a\u8fd8\u7ed9 Arena&#xff0c;\u800c\u662f\u5148\u653e\u5165\u7ebf\u7a0b\u7f13\u5b58&#xff0c;\u4e0b\u4e00\u6b21\u540c\u7ebf\u7a0b\u5206\u914d\u540c\u89c4\u683c\u5185\u5b58\u65f6\u76f4\u63a5\u547d\u4e2d\u3002\u4e3b\u8981\u8bbe\u8ba1\u70b9&#xff1a;<\/p>\n<ul>\n<li>ThreadLocal \u9694\u79bb&#xff1a;\u6bcf\u4e2a\u7ebf\u7a0b\u6301\u6709\u81ea\u5df1\u7684\u7f13\u5b58&#xff0c;\u8bbf\u95ee\u65e0\u9501&#xff0c;\u8fd9\u662f\u5feb\u8def\u5f84\u9ad8\u6548\u7684\u5173\u952e\u3002<\/li>\n<li>\u6309\u89c4\u683c\u5206\u961f\u5217\u7ba1\u7406&#xff1a;\u5bf9 Tiny\u3001Small\u3001Normal \u5206\u522b\u7ef4\u62a4\u7f13\u5b58\u961f\u5217&#xff0c;\u907f\u514d\u4e0d\u540c\u89c4\u683c\u5185\u5b58\u4e92\u76f8\u5e72\u6270&#xff0c;\u4e5f\u4fbf\u4e8e\u6309\u7167\u4e0d\u540c\u56de\u6536\u9608\u503c\u63a7\u5236\u7f13\u5b58\u5927\u5c0f\u3002<\/li>\n<li>\u5206\u914d\u4f18\u5148\u7ea7&#xff1a;\u5206\u914d\u65f6\u5148\u67e5\u7ebf\u7a0b\u7f13\u5b58&#xff0c;\u547d\u4e2d\u5219\u76f4\u63a5\u8fd4\u56de&#xff1b;\u672a\u547d\u4e2d\u518d\u8bbf\u95ee Arena&#xff0c;\u7531 Arena \u7ee7\u7eed\u5411 PoolChunkList\u3001PoolChunk \u6216 PoolSubpage \u7533\u8bf7\u3002<\/li>\n<li>\u91ca\u653e\u7f13\u51b2\u4e0e\u5bb9\u91cf\u4fdd\u62a4&#xff1a;\u91ca\u653e\u65f6\u5148\u653e\u5165\u7ebf\u7a0b\u7f13\u5b58&#xff0c;\u800c\u4e0d\u662f\u7acb\u523b\u5f52\u8fd8 Arena&#xff1b;\u5f53\u7f13\u5b58\u8d85\u8fc7\u5bb9\u91cf\u6216\u8fbe\u5230\u56de\u6536\u9608\u503c\u540e&#xff0c;\u518d\u628a\u591a\u4f59\u5185\u5b58\u5f52\u8fd8 Arena&#xff0c;\u907f\u514d\u7ebf\u7a0b\u7f13\u5b58\u65e0\u9650\u81a8\u80c0\u3002<\/li>\n<\/ul>\n<p>\u8fd8\u9700\u8981\u8865\u5145\u4e00\u4e2a\u5173\u952e\u7ec6\u8282&#xff1a;\u7ebf\u7a0b\u7f13\u5b58\u5e76\u4e0d\u662f\u300c\u53ea\u8fdb\u4e0d\u51fa\u300d\u7684\u9ed1\u76d2\u3002PoolThreadCache \u4f1a\u8bb0\u5f55\u6bcf\u6b21\u5206\u914d\u548c\u91ca\u653e\u7684\u89c4\u683c&#xff0c;\u5e76\u8bbe\u7f6e initialSize\u3001maxSize \u548c freeSweepAllocationThreshold \u7b49\u53c2\u6570\u3002\u5f53\u7ebf\u7a0b\u7684\u5206\u914d\u6b21\u6570\u8fbe\u5230\u4e00\u5b9a\u9608\u503c\u540e&#xff0c;\u4f1a\u89e6\u53d1\u4e00\u6b21\u7f13\u5b58\u6e05\u7406&#xff0c;\u628a\u4e0d\u5e38\u7528\u7684\u89c4\u683c\u5185\u5b58\u5f52\u8fd8\u7ed9 Arena&#xff0c;\u4ece\u800c\u5728\u300c\u5feb\u8def\u5f84\u547d\u4e2d\u7387\u300d\u548c\u300c\u5185\u5b58\u5360\u7528\u300d\u4e4b\u95f4\u53d6\u5f97\u5e73\u8861\u3002<\/p>\n<h3>\u5341\u4e00\u3001PoolChunkList&#xff1a;Chunk \u4f7f\u7528\u7387\u7684\u5206\u7ea7\u7ba1\u7406<\/h3>\n<p>\u5982\u679c PoolArena \u5728\u4e00\u6b21\u5206\u914d\u4e2d\u76f4\u63a5\u904d\u5386\u6240\u6709 PoolChunk&#xff0c;\u968f\u7740\u8fd0\u884c\u65f6\u95f4\u53d8\u957f&#xff0c;Chunk \u6570\u91cf\u589e\u52a0&#xff0c;\u67e5\u627e\u7a7a\u95f2\u5757\u7684\u6548\u7387\u4f1a\u660e\u663e\u4e0b\u964d\u3002Netty \u7684\u4f18\u5316\u624b\u6bb5\u662f\u5f15\u5165 PoolChunkList&#xff0c;\u628a Chunk \u6309\u4f7f\u7528\u7387\u5206\u6210\u591a\u4e2a\u533a\u95f4&#xff0c;\u7528\u53cc\u5411\u94fe\u8868\u7ba1\u7406\u3002<\/p>\n<p>\u5178\u578b\u7684\u4f7f\u7528\u7387\u533a\u95f4\u5982\u4e0b&#xff1a;<\/p>\n<ul>\n<li>q000&#xff1a;\u4f7f\u7528\u7387\u4e3a 0% \u5230 24%&#xff0c;\u57fa\u672c\u5904\u4e8e\u7a7a\u95f2\u72b6\u6001\u3002<\/li>\n<li>q025&#xff1a;\u4f7f\u7528\u7387\u4e3a 25% \u5230 49%\u3002<\/li>\n<li>q050&#xff1a;\u4f7f\u7528\u7387\u4e3a 50% \u5230 74%\u3002<\/li>\n<li>q075&#xff1a;\u4f7f\u7528\u7387\u4e3a 75% \u5230 99%\u3002<\/li>\n<li>q100&#xff1a;\u4f7f\u7528\u7387\u4e3a 100%&#xff0c;\u5df2\u7ecf\u5b8c\u5168\u5206\u914d&#xff0c;\u4e0d\u518d\u53c2\u4e0e\u540e\u7eed\u5206\u914d\u3002<\/li>\n<\/ul>\n<p>\u5206\u914d Normal \u5185\u5b58\u65f6&#xff0c;Netty \u4f1a\u4ece\u4f4e\u4f7f\u7528\u7387\u7684 ChunkList \u5f00\u59cb\u67e5\u627e\u3002\u8fd9\u6837\u505a\u7684\u903b\u8f91\u662f&#xff1a;\u4f18\u5148\u628a\u5185\u5b58\u4ece\u300c\u5df2\u7ecf\u5f88\u6ee1\u300d\u7684 Chunk \u4e2d\u5206\u914d\u51fa\u53bb&#xff0c;\u8ba9\u5269\u4f59\u7a7a\u95f2\u7a7a\u95f4\u66f4\u96c6\u4e2d&#xff0c;\u4fbf\u4e8e\u540e\u7eed\u91ca\u653e\u5f62\u6210\u66f4\u5927\u7684\u8fde\u7eed\u7a7a\u95f4\u3002\u5f53\u4e00\u4e2a Chunk \u7684\u5269\u4f59\u7a7a\u95f4\u65e0\u6cd5\u6ee1\u8db3\u5206\u914d\u9700\u6c42\u65f6&#xff0c;\u518d\u6cbf\u7740\u94fe\u8868\u5411\u4f7f\u7528\u7387\u66f4\u4f4e\u7684 ChunkList \u67e5\u627e\u3002<\/p>\n<p>\u968f\u7740\u5206\u914d\u548c\u91ca\u653e\u8fdb\u884c&#xff0c;Chunk \u7684\u4f7f\u7528\u7387\u4f1a\u4e0d\u65ad\u53d8\u5316&#xff0c;Netty \u4f1a\u628a Chunk \u79fb\u52a8\u5230\u5bf9\u5e94\u7684 ChunkList \u4e2d\u3002\u5f53\u67d0\u4e2a Chunk \u4f7f\u7528\u7387\u964d\u5230 0% \u65f6&#xff0c;\u5b83\u53ef\u4ee5\u88ab\u79fb\u51fa Arena \u5e76\u91ca\u653e\u7ed9\u64cd\u4f5c\u7cfb\u7edf&#xff0c;\u4ece\u800c\u907f\u514d\u957f\u671f\u5360\u7528\u5806\u5916\u5185\u5b58\u3002\u8fd9\u4e2a\u673a\u5236\u4e0e PoolThreadCache \u7684\u56de\u6536\u7b56\u7565\u4e00\u8d77&#xff0c;\u6784\u6210\u4e86 Netty \u5bf9\u300c\u5185\u5b58\u590d\u7528\u300d\u548c\u300c\u53ca\u65f6\u5f52\u8fd8\u300d\u7684\u5e73\u8861\u3002<\/p>\n<h3>\u5341\u4e8c\u3001Netty \u5185\u5b58\u6cc4\u6f0f\u68c0\u6d4b\u4e0e\u5b9a\u4f4d<\/h3>\n<p>\u56e0\u4e3a\u6709\u5f15\u7528\u8ba1\u6570\u548c\u5806\u5916\u5185\u5b58&#xff0c;Netty \u7684\u5185\u5b58\u6cc4\u6f0f\u5982\u679c\u5904\u7406\u4e0d\u5f53&#xff0c;\u5f80\u5f80\u8868\u73b0\u4e3a\u5806\u5916\u5185\u5b58\u6301\u7eed\u589e\u957f&#xff0c;\u6700\u7ec8\u89e6\u53d1 OOM&#xff0c;\u800c JVM \u5806\u672c\u8eab\u770b\u8d77\u6765\u5374\u5f88\u6b63\u5e38\u3002\u8fd9\u662f Netty \u9762\u8bd5\u4e2d\u975e\u5e38\u5bb9\u6613\u88ab\u8ffd\u95ee\u7684\u4e00\u4e2a\u5b9e\u64cd\u95ee\u9898\u3002<\/p>\n<p>Netty \u63d0\u4f9b\u4e86 ResourceLeakDetector \u6765\u68c0\u6d4b\u6cc4\u6f0f&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u7cfb\u7edf\u5c5e\u6027\u6216 JVM \u53c2\u6570\u63a7\u5236\u68c0\u6d4b\u7ea7\u522b&#xff1a;<\/p>\n<ul>\n<li>DISABLED&#xff1a;\u5173\u95ed\u6cc4\u6f0f\u68c0\u6d4b&#xff0c;\u6027\u80fd\u6700\u597d&#xff0c;\u4e00\u822c\u4e0d\u7528\u4e8e\u7ebf\u4e0a\u6392\u67e5\u3002<\/li>\n<li>SIMPLE&#xff1a;\u9ed8\u8ba4\u7ea7\u522b&#xff0c;\u53ea\u505a\u7b80\u5355\u68c0\u6d4b&#xff0c;\u53d1\u73b0\u6cc4\u6f0f\u65f6\u6253\u5370\u4e00\u6b21\u65e5\u5fd7\u3002<\/li>\n<li>ADVANCED&#xff1a;\u8bb0\u5f55\u5bf9\u8c61\u521b\u5efa\u4f4d\u7f6e\u548c\u8bbf\u95ee\u8def\u5f84&#xff0c;\u5b9a\u4f4d\u66f4\u51c6\u786e&#xff0c;\u5f00\u9500\u66f4\u9ad8\u3002<\/li>\n<li>PARANOID&#xff1a;\u68c0\u6d4b\u6700\u4e25\u683c&#xff0c;\u6bcf\u6b21\u7f13\u51b2\u533a\u88ab GC \u65f6\u90fd\u6267\u884c\u5b8c\u6574\u68c0\u67e5&#xff0c;\u53ea\u9002\u5408\u6d4b\u8bd5\u73af\u5883\u3002<\/li>\n<\/ul>\n<p>-Dio.netty.leakDetection.level&#061;PARANOID<\/p>\n<p>\u5f53\u68c0\u6d4b\u5230\u7591\u4f3c\u6cc4\u6f0f\u65f6&#xff0c;\u65e5\u5fd7\u901a\u5e38\u4f1a\u63d0\u793a\u7c7b\u4f3c LEAK: ByteBuf.release() was not called before it&#039;s garbage-collected \u7684\u4fe1\u606f&#xff0c;\u5e76\u7ed9\u51fa\u8be5 ByteBuf \u7684\u521b\u5efa\u4f4d\u7f6e\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u8fd9\u6bb5\u5806\u6808\u4fe1\u606f&#xff0c;\u5b9a\u4f4d\u5230\u662f\u54ea\u4e00\u6bb5\u4e1a\u52a1\u4ee3\u7801\u5fd8\u8bb0\u8c03\u7528 release() \u6216 ReferenceCountUtil.release(msg)\u3002<\/p>\n<p>\u6700\u5e38\u89c1\u7684\u6cc4\u6f0f\u539f\u56e0\u6709\u4e09\u4e2a&#xff1a;<\/p>\n<ul>\n<li>\u4f7f\u7528 ChannelInboundHandlerAdapter \u4f46\u5fd8\u8bb0\u624b\u52a8 release&#xff1a;\u5165\u7ad9\u6d88\u606f\u5982\u679c\u4e0d\u5728 Handler \u4e2d\u6d88\u8d39\u6389\u5f15\u7528\u8ba1\u6570&#xff0c;\u5c31\u4f1a\u53d8\u6210\u6cc4\u6f0f\u6e90\u3002\u4f18\u5148\u4f7f\u7528 SimpleChannelInboundHandler&#xff0c;\u5b83\u4f1a\u81ea\u52a8\u91ca\u653e\u3002<\/li>\n<li>\u6d3e\u751f\u7f13\u51b2\u533a\u4f7f\u7528\u6df7\u4e71&#xff1a;slice()\u3001duplicate() \u4e0e\u539f\u59cb Buffer \u5171\u4eab\u5e95\u5c42\u5185\u5b58\u548c\u5f15\u7528\u8ba1\u6570&#xff0c;\u91ca\u653e\u5173\u7cfb\u5904\u7406\u4e0d\u5f53\u4f1a\u5bfc\u81f4\u6cc4\u6f0f\u6216\u63d0\u524d\u91ca\u653e\u3002<\/li>\n<li>\u51fa\u7ad9 Buffer \u88ab\u53cd\u590d write \u540e\u6ca1\u6709\u53ca\u65f6\u91ca\u653e&#xff1a;\u5199\u5b8c\u4e00\u4e2a ByteBuf \u540e\u5982\u679c\u7ee7\u7eed\u6301\u6709\u5f15\u7528\u800c\u4e0d release&#xff0c;\u4e5f\u4f1a\u51fa\u73b0\u6cc4\u6f0f\u3002<\/li>\n<\/ul>\n<p>\u5728\u7ebf\u4e0a\u6392\u67e5\u65f6&#xff0c;\u53ef\u4ee5\u5148\u89c2\u5bdf\u76f4\u63a5\u5185\u5b58\u5360\u7528\u66f2\u7ebf&#xff0c;\u7ed3\u5408 jcmd\u3001jmap\u3001gceasy \u4ee5\u53ca Netty \u81ea\u5e26\u7684\u5185\u5b58\u6c60\u6307\u6807\u6765\u8f85\u52a9\u5224\u65ad\u3002\u5f00\u542f PARANOID \u6216 ADVANCED \u7ea7\u522b\u8dd1\u4e00\u6bb5\u6d4b\u8bd5\u6d41\u91cf&#xff0c;\u901a\u5e38\u53ef\u4ee5\u5feb\u901f\u5b9a\u4f4d\u5230\u6cc4\u6f0f\u70b9\u3002<\/p>\n<h3>\u5341\u4e09\u3001Netty \u5185\u5b58\u53c2\u6570\u914d\u7f6e\u4e0e\u6027\u80fd\u8c03\u4f18<\/h3>\n<p>\u7406\u89e3\u4e86\u5185\u90e8\u673a\u5236\u540e&#xff0c;Netty \u8fd8\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u53c2\u6570\u6765\u8c03\u6574\u5185\u5b58\u6c60\u884c\u4e3a\u3002\u5e38\u89c1\u7684\u7cfb\u7edf\u5c5e\u6027\u548c JVM \u53c2\u6570\u5982\u4e0b&#xff1a;<\/p>\n<table>\n<tr>\u53c2\u6570\u542b\u4e49<\/tr>\n<tbody>\n<tr>\n<td>-Dio.netty.allocator.type<\/td>\n<td>\u8bbe\u7f6e\u5206\u914d\u5668\u7c7b\u578b&#xff0c;\u53ef\u9009 pooled \u6216 unpooled&#xff0c;\u9ed8\u8ba4 pooled\u3002<\/td>\n<\/tr>\n<tr>\n<td>-Dio.netty.noPreferDirect<\/td>\n<td>\u662f\u5426\u7981\u7528\u4f18\u5148\u4f7f\u7528\u5806\u5916\u5185\u5b58&#xff0c;\u8bbe\u4e3a true \u540e\u4f18\u5148\u4f7f\u7528\u5806\u5185\u5b58\u3002<\/td>\n<\/tr>\n<tr>\n<td>-Dio.netty.allocator.pageSize<\/td>\n<td>Page \u5927\u5c0f&#xff0c;\u9ed8\u8ba4 8192 \u5b57\u8282&#xff0c;\u5373 8KB\u3002<\/td>\n<\/tr>\n<tr>\n<td>-Dio.netty.allocator.maxOrder<\/td>\n<td>Chunk \u4e2d\u4f19\u4f34\u6811\u7684\u6700\u5927\u6df1\u5ea6&#xff0c;\u9ed8\u8ba4 11&#xff0c;\u5bf9\u5e94 16MB Chunk\u3002<\/td>\n<\/tr>\n<tr>\n<td>-Dio.netty.allocator.numDirectArenas<\/td>\n<td>\u5806\u5916 Arena \u6570\u91cf&#xff0c;\u9ed8\u8ba4\u7ea6\u7b49\u4e8e 2 \u500d CPU \u6838\u6570\u7684\u4e00\u534a\u3002<\/td>\n<\/tr>\n<tr>\n<td>-Dio.netty.allocator.numHeapArenas<\/td>\n<td>\u5806\u5185 Arena \u6570\u91cf\u3002<\/td>\n<\/tr>\n<tr>\n<td>-Dio.netty.allocator.tinyCacheSize<\/td>\n<td>\u7ebf\u7a0b\u7f13\u5b58\u4e2d Tiny \u89c4\u683c\u7684\u7f13\u5b58\u5927\u5c0f\u3002<\/td>\n<\/tr>\n<tr>\n<td>-Dio.netty.allocator.smallCacheSize<\/td>\n<td>\u7ebf\u7a0b\u7f13\u5b58\u4e2d Small \u89c4\u683c\u7684\u7f13\u5b58\u5927\u5c0f\u3002<\/td>\n<\/tr>\n<tr>\n<td>-Dio.netty.allocator.normalCacheSize<\/td>\n<td>\u7ebf\u7a0b\u7f13\u5b58\u4e2d Normal \u89c4\u683c\u7684\u7f13\u5b58\u5927\u5c0f\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8c03\u4f18\u65f6\u7684\u51e0\u4e2a\u7ecf\u9a8c\u539f\u5219&#xff1a;<\/p>\n<ul>\n<li>\u4f4e\u5ef6\u8fdf\u3001\u957f\u8fde\u63a5\u7684 RPC \u573a\u666f&#xff1a;\u4f18\u5148\u4fdd\u6301\u6c60\u5316\u5806\u5916\u5185\u5b58&#xff0c;\u5e76\u9002\u5f53\u8c03\u5927\u7ebf\u7a0b\u7f13\u5b58&#xff0c;\u51cf\u5c11 Arena \u9501\u7ade\u4e89\u3002<\/li>\n<li>\u5806\u5185\u5b58\u5145\u8db3\u3001\u6d88\u606f\u4f53\u8f83\u5c0f\u7684\u573a\u666f&#xff1a;\u53ef\u4ee5\u8bc4\u4f30\u662f\u5426\u4f7f\u7528\u5806\u7f13\u51b2\u533a&#xff0c;\u51cf\u5c11\u5806\u5916\u5185\u5b58\u5360\u7528\u548c\u6392\u67e5\u6210\u672c\u3002<\/li>\n<li>\u5927\u6d88\u606f\u4e0b\u8f7d\u6216\u89c6\u9891\u4f20\u8f93\u573a\u666f&#xff1a;\u5927\u5757 Huge \u5185\u5b58\u672c\u8eab\u4e0d\u6c60\u5316&#xff0c;\u91cd\u70b9\u5e94\u653e\u5728\u63a7\u5236\u5355\u6b21\u6d88\u606f\u5927\u5c0f\u548c\u53ca\u65f6 release&#xff0c;\u907f\u514d\u5806\u5916\u5185\u5b58\u5feb\u901f\u4e0a\u6da8\u3002<\/li>\n<li>\u538b\u6d4b\u6392\u67e5 OOM&#xff1a;\u5148\u786e\u8ba4\u662f JVM \u5806\u4e0d\u591f\u8fd8\u662f MaxDirectMemorySize \u8d85\u9650&#xff0c;\u518d\u51b3\u5b9a\u662f\u8c03\u5927\u53c2\u6570\u8fd8\u662f\u4fee\u590d\u6cc4\u6f0f\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u53c2\u6570\u4e0d\u5efa\u8bae\u5728\u7ebf\u4e0a\u968f\u610f\u8c03\u6574&#xff0c;\u56e0\u4e3a Netty \u9ed8\u8ba4\u503c\u7ecf\u8fc7\u4e86\u5927\u91cf\u573a\u666f\u9a8c\u8bc1\u3002\u53ea\u6709\u5728\u538b\u6d4b\u4e2d\u660e\u786e\u53d1\u73b0\u5185\u5b58\u788e\u7247\u3001\u9501\u7ade\u4e89\u6216\u9891\u7e41 GC \u65f6&#xff0c;\u624d\u7ed3\u5408\u5177\u4f53\u6307\u6807\u505a\u9488\u5bf9\u6027\u8c03\u6574\u3002<\/p>\n<h3>\u5341\u56db\u3001\u9762\u8bd5\u9ad8\u9891\u8ffd\u95ee\u4e0e\u6838\u5fc3\u7ed3\u8bba<\/h3>\n<p>\u628a\u524d\u9762\u6240\u6709\u5185\u5bb9\u4e32\u8d77\u6765&#xff0c;Netty \u5185\u5b58\u7ba1\u7406\u7684\u672c\u8d28\u53ef\u4ee5\u6982\u62ec\u4e3a\u4e00\u53e5\u8bdd&#xff1a;\u7528\u6c60\u5316\u53d6\u4ee3\u9891\u7e41\u7cfb\u7edf\u8c03\u7528&#xff0c;\u7528\u89c4\u683c\u5316\u51cf\u5c11\u788e\u7247&#xff0c;\u7528\u7ebf\u7a0b\u7f13\u5b58\u964d\u4f4e\u9501\u7ade\u4e89&#xff0c;\u7528\u5f15\u7528\u8ba1\u6570\u7cbe\u786e\u63a7\u5236\u751f\u547d\u5468\u671f\u3002<\/p>\n<p>\u9762\u8bd5\u4e2d\u5efa\u8bae\u91cd\u70b9\u51c6\u5907\u4ee5\u4e0b\u51e0\u4e2a\u8ffd\u95ee&#xff1a;<\/p>\n<ul>\n<li>Netty \u4e3a\u4ec0\u4e48\u4e0d\u7528 JVM \u5806\u5185\u5b58&#xff0c;\u800c\u8981\u81ea\u5df1\u7ba1\u7406\u5806\u5916\u5185\u5b58&#xff1a;\u4ece NIO \u539f\u751f\u8bfb\u5199\u3001\u96f6\u62f7\u8d1d\u3001GC \u505c\u987f\u4e09\u4e2a\u89d2\u5ea6\u56de\u7b54\u3002<\/li>\n<li>ByteBuf \u76f8\u6bd4 ByteBuffer \u7684\u4f18\u52bf&#xff1a;\u53cc\u6307\u9488\u8bfb\u5199\u5206\u79bb\u3001\u5bb9\u91cf\u52a8\u6001\u6269\u5c55\u3001\u6c60\u5316\u3001\u5f15\u7528\u8ba1\u6570\u3001\u6d3e\u751f\u7f13\u51b2\u533a\u3002<\/li>\n<li>\u6c60\u5316\u4e0e\u975e\u6c60\u5316\u5206\u914d\u5668\u7684\u533a\u522b&#xff1a;\u6c60\u5316\u51cf\u5c11\u4e86\u7533\u8bf7\u91ca\u653e\u6210\u672c&#xff0c;\u4f46\u5f15\u5165\u66f4\u590d\u6742\u7684\u5185\u5b58\u7ba1\u7406&#xff1b;\u975e\u6c60\u5316\u5b9e\u73b0\u7b80\u5355&#xff0c;\u9002\u5408\u751f\u547d\u5468\u671f\u77ed\u6216\u4f4e\u9891\u5206\u914d\u3002<\/li>\n<li>PoolChunk \u5982\u4f55\u5206\u914d\u548c\u56de\u6536 Page&#xff1a;\u4f19\u4f34\u6811\u6309\u9700\u62c6\u5206&#xff0c;\u5144\u5f1f\u8282\u70b9\u90fd\u7a7a\u95f2\u65f6\u5408\u5e76&#xff0c;\u65f6\u95f4\u590d\u6742\u5ea6\u63a5\u8fd1 O(logN)\u3002<\/li>\n<li>PoolSubpage \u5982\u4f55\u7ba1\u7406\u5c0f\u5757\u5185\u5b58&#xff1a;\u4e00\u4e2a Page \u5185\u7b49\u5206\u4e3a\u591a\u4e2a\u76f8\u540c\u5143\u7d20&#xff0c;\u7528\u4f4d\u56fe\u6807\u8bb0\u5206\u914d\u72b6\u6001&#xff0c;\u63a5\u8fd1 O(1) \u7684\u5c0f\u5757\u5206\u914d\u3002<\/li>\n<li>PoolThreadCache \u6709\u54ea\u4e9b\u961f\u5217&#xff1a;\u6309 Tiny\u3001Small\u3001Normal \u4e09\u7c7b\u5206\u961f\u5217\u7f13\u5b58&#xff0c;\u5148\u7f13\u5b58\u540e\u5f52\u8fd8 Arena\u3002<\/li>\n<li>\u5982\u4f55\u6392\u67e5 Netty \u5185\u5b58\u6cc4\u6f0f&#xff1a;\u5f00\u542f ResourceLeakDetector&#xff0c;\u7ed3\u5408\u65e5\u5fd7\u5806\u6808\u5b9a\u4f4d\u672a release \u7684\u4f4d\u7f6e&#xff0c;\u4f18\u5148\u4f7f\u7528 SimpleChannelInboundHandler \u81ea\u52a8\u91ca\u653e\u3002<\/li>\n<\/ul>\n<p>\u6700\u540e\u7559\u4e00\u53e5\u53ef\u4ee5\u52a0\u5206\u7684\u8bdd&#xff1a;Netty \u5e76\u6ca1\u6709\u8ba9\u5185\u5b58\u7ba1\u7406\u53d8\u6210\u300c\u94f6\u5f39\u300d&#xff0c;\u5b83\u628a jemalloc \u7684\u601d\u60f3\u5e26\u56de JVM \u4e16\u754c&#xff0c;\u6838\u5fc3\u4ef7\u503c\u662f\u5728\u9ad8\u5e76\u53d1\u7f51\u7edc\u8bfb\u5199\u573a\u666f\u4e0b&#xff0c;\u7528\u786e\u5b9a\u6027\u7684\u5f15\u7528\u8ba1\u6570\u548c\u5185\u5b58\u590d\u7528&#xff0c;\u964d\u4f4e GC \u538b\u529b\u4e0e\u7cfb\u7edf\u8c03\u7528\u5f00\u9500\u3002\u9762\u8bd5\u65f6\u80fd\u628a\u300c\u4e3a\u4ec0\u4e48\u8fd9\u6837\u8bbe\u8ba1\u300d\u548c\u300c\u8fd9\u6837\u8bbe\u8ba1\u5e26\u6765\u4ec0\u4e48\u4ee3\u4ef7\u300d\u90fd\u8bb2\u6e05\u695a&#xff0c;\u5c31\u5df2\u7ecf\u8d85\u8fc7\u4e86\u5927\u591a\u6570\u5019\u9009\u4eba\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u4e3a\u4ec0\u4e48 Netty \u9700\u8981\u81ea\u5df1\u7684\u5185\u5b58\u7ba1\u7406\u5728\u56de\u7b54\u300cNetty \u5185\u5b58\u7ba1\u7406\u300d\u4e4b\u524d&#xff0c;\u5148\u8981\u7406\u89e3\u4e00\u4e2a\u6700\u6839\u672c\u7684\u95ee\u9898&#xff1a;JDK \u660e\u660e\u5df2\u7ecf\u66ff\u6211\u4eec\u7ba1\u7406\u4e86\u5806\u5185\u5b58&#xff0c;\u4e3a\u4ec0\u4e48 Netty \u8fd8\u8981\u81ea\u5df1\u518d\u641e\u4e00\u5957\u5185\u5b58\u7ba1\u7406\u673a\u5236&#xff1f;\u8fd9\u4e2a\u95ee\u9898\u6070\u6070\u662f\u9762\u8bd5\u5b98\u6700\u60f3\u542c\u5230\u7684\u5207\u5165\u70b9\u3002JDK \u7684\u5806\u5185\u5b58\u7531 JVM \u81ea\u52a8\u7ba1\u7406&#xff0c;\u901a\u8fc7\u65b0\u751f\u4ee3\u3001\u8001\u5e74\u4ee3\u3001GC \u56de\u6536\u7b49\u65b9\u5f0f\u4fdd\u8bc1\u7a0b\u5e8f\u5458\u300c\u4e0d\u7528\u5173\u5fc3\u5185\u5b58\u91ca\u653e\u300d\u3002\u4f46\u8fd9\u79cd\u4fbf\u5229\u5728 NIO \u573a\u666f\u4e0b\u4f1a\u5e26\u6765\u4e09\u4e2a\u75db\u70b9&#xff1a;\u5806\u5916\u5185\u5b58\u65e0\u6cd5\u88ab G<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[925,176],"topic":[],"class_list":["post-122582","post","type-post","status-publish","format-standard","hentry","category-server","tag-925","tag-176"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Netty \u5185\u5b58\u7ba1\u7406\u8be6\u89e3\uff1a\u4ece ByteBuf \u5230\u5185\u5b58\u6c60\uff0c\u9ad8\u9891\u9762\u8bd5\u8003\u70b9\u4e00\u6b21\u8bb2\u900f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/122582.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Netty \u5185\u5b58\u7ba1\u7406\u8be6\u89e3\uff1a\u4ece ByteBuf \u5230\u5185\u5b58\u6c60\uff0c\u9ad8\u9891\u9762\u8bd5\u8003\u70b9\u4e00\u6b21\u8bb2\u900f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001\u4e3a\u4ec0\u4e48 Netty \u9700\u8981\u81ea\u5df1\u7684\u5185\u5b58\u7ba1\u7406\u5728\u56de\u7b54\u300cNetty \u5185\u5b58\u7ba1\u7406\u300d\u4e4b\u524d&#xff0c;\u5148\u8981\u7406\u89e3\u4e00\u4e2a\u6700\u6839\u672c\u7684\u95ee\u9898&#xff1a;JDK \u660e\u660e\u5df2\u7ecf\u66ff\u6211\u4eec\u7ba1\u7406\u4e86\u5806\u5185\u5b58&#xff0c;\u4e3a\u4ec0\u4e48 Netty \u8fd8\u8981\u81ea\u5df1\u518d\u641e\u4e00\u5957\u5185\u5b58\u7ba1\u7406\u673a\u5236&#xff1f;\u8fd9\u4e2a\u95ee\u9898\u6070\u6070\u662f\u9762\u8bd5\u5b98\u6700\u60f3\u542c\u5230\u7684\u5207\u5165\u70b9\u3002JDK \u7684\u5806\u5185\u5b58\u7531 JVM \u81ea\u52a8\u7ba1\u7406&#xff0c;\u901a\u8fc7\u65b0\u751f\u4ee3\u3001\u8001\u5e74\u4ee3\u3001GC \u56de\u6536\u7b49\u65b9\u5f0f\u4fdd\u8bc1\u7a0b\u5e8f\u5458\u300c\u4e0d\u7528\u5173\u5fc3\u5185\u5b58\u91ca\u653e\u300d\u3002\u4f46\u8fd9\u79cd\u4fbf\u5229\u5728 NIO \u573a\u666f\u4e0b\u4f1a\u5e26\u6765\u4e09\u4e2a\u75db\u70b9&#xff1a;\u5806\u5916\u5185\u5b58\u65e0\u6cd5\u88ab G\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/122582.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-11-04T23:32:44+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/122582.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/122582.html\",\"name\":\"Netty \u5185\u5b58\u7ba1\u7406\u8be6\u89e3\uff1a\u4ece ByteBuf \u5230\u5185\u5b58\u6c60\uff0c\u9ad8\u9891\u9762\u8bd5\u8003\u70b9\u4e00\u6b21\u8bb2\u900f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-11-04T23:32:44+00:00\",\"dateModified\":\"2026-11-04T23:32:44+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/122582.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/122582.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/122582.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Netty \u5185\u5b58\u7ba1\u7406\u8be6\u89e3\uff1a\u4ece ByteBuf \u5230\u5185\u5b58\u6c60\uff0c\u9ad8\u9891\u9762\u8bd5\u8003\u70b9\u4e00\u6b21\u8bb2\u900f\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Netty \u5185\u5b58\u7ba1\u7406\u8be6\u89e3\uff1a\u4ece ByteBuf \u5230\u5185\u5b58\u6c60\uff0c\u9ad8\u9891\u9762\u8bd5\u8003\u70b9\u4e00\u6b21\u8bb2\u900f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/122582.html","og_locale":"zh_CN","og_type":"article","og_title":"Netty \u5185\u5b58\u7ba1\u7406\u8be6\u89e3\uff1a\u4ece ByteBuf \u5230\u5185\u5b58\u6c60\uff0c\u9ad8\u9891\u9762\u8bd5\u8003\u70b9\u4e00\u6b21\u8bb2\u900f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001\u4e3a\u4ec0\u4e48 Netty \u9700\u8981\u81ea\u5df1\u7684\u5185\u5b58\u7ba1\u7406\u5728\u56de\u7b54\u300cNetty \u5185\u5b58\u7ba1\u7406\u300d\u4e4b\u524d&#xff0c;\u5148\u8981\u7406\u89e3\u4e00\u4e2a\u6700\u6839\u672c\u7684\u95ee\u9898&#xff1a;JDK \u660e\u660e\u5df2\u7ecf\u66ff\u6211\u4eec\u7ba1\u7406\u4e86\u5806\u5185\u5b58&#xff0c;\u4e3a\u4ec0\u4e48 Netty \u8fd8\u8981\u81ea\u5df1\u518d\u641e\u4e00\u5957\u5185\u5b58\u7ba1\u7406\u673a\u5236&#xff1f;\u8fd9\u4e2a\u95ee\u9898\u6070\u6070\u662f\u9762\u8bd5\u5b98\u6700\u60f3\u542c\u5230\u7684\u5207\u5165\u70b9\u3002JDK \u7684\u5806\u5185\u5b58\u7531 JVM \u81ea\u52a8\u7ba1\u7406&#xff0c;\u901a\u8fc7\u65b0\u751f\u4ee3\u3001\u8001\u5e74\u4ee3\u3001GC \u56de\u6536\u7b49\u65b9\u5f0f\u4fdd\u8bc1\u7a0b\u5e8f\u5458\u300c\u4e0d\u7528\u5173\u5fc3\u5185\u5b58\u91ca\u653e\u300d\u3002\u4f46\u8fd9\u79cd\u4fbf\u5229\u5728 NIO \u573a\u666f\u4e0b\u4f1a\u5e26\u6765\u4e09\u4e2a\u75db\u70b9&#xff1a;\u5806\u5916\u5185\u5b58\u65e0\u6cd5\u88ab G","og_url":"https:\/\/www.wsisp.com\/helps\/122582.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-11-04T23:32:44+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/122582.html","url":"https:\/\/www.wsisp.com\/helps\/122582.html","name":"Netty \u5185\u5b58\u7ba1\u7406\u8be6\u89e3\uff1a\u4ece ByteBuf \u5230\u5185\u5b58\u6c60\uff0c\u9ad8\u9891\u9762\u8bd5\u8003\u70b9\u4e00\u6b21\u8bb2\u900f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-11-04T23:32:44+00:00","dateModified":"2026-11-04T23:32:44+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/122582.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/122582.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/122582.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Netty \u5185\u5b58\u7ba1\u7406\u8be6\u89e3\uff1a\u4ece ByteBuf \u5230\u5185\u5b58\u6c60\uff0c\u9ad8\u9891\u9762\u8bd5\u8003\u70b9\u4e00\u6b21\u8bb2\u900f"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/122582","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=122582"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/122582\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=122582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=122582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=122582"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=122582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}