{"id":278,"date":"2024-10-03T21:25:12","date_gmt":"2024-10-03T13:25:12","guid":{"rendered":"https:\/\/www.zackzzp.top\/?p=278"},"modified":"2024-10-03T21:25:13","modified_gmt":"2024-10-03T13:25:13","slug":"%e4%b8%80%e6%96%87%e7%90%86%e8%a7%a3jvm%e7%9b%b4%e6%8e%a5%e5%86%85%e5%ad%98","status":"publish","type":"post","link":"http:\/\/www.zackzzp.top\/index.php\/2024\/10\/03\/%e4%b8%80%e6%96%87%e7%90%86%e8%a7%a3jvm%e7%9b%b4%e6%8e%a5%e5%86%85%e5%ad%98\/","title":{"rendered":"\u4e00\u6587\u7406\u89e3JVM\u76f4\u63a5\u5185\u5b58"},"content":{"rendered":"<h2>\u6982\u8ff0<\/h2>\n<p>\u76f4\u63a5\u5185\u5b58(Direct Memory)\u662f\u5728Java\u4e2d\u4f7f\u7528\u8f83\u4e3a\u5e95\u5c42\u7684\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f\uff0c\u5b83\u901a\u8fc7Java\u7684NIO\u5305\u4e2d\u7684ByteBuffer\u7c7b\u6765\u5b9e\u73b0\uff0c\u8ba9Java\u80fd\u591f\u5728\u5806\u5916\u5206\u914d\u5185\u5b58\u3002\u8fd9\u79cd\u65b9\u5f0f\u901a\u5e38\u7528\u4e8e\u9700\u8981\u5927\u91cf\u3001\u9891\u7e41\u7684IO\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u51cf\u5c11\u5728Java\u5806\u548c\u672c\u5730\u5806\u4e4b\u95f4\u590d\u5236\u6570\u636e\u7684\u6b21\u6570\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u5173\u4e8e\u76f4\u63a5\u5185\u5b58\u7684\u4e00\u4e9b\u5206\u6790\u548c\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<hr \/>\n<h2>\u4e0d\u7528\u76f4\u63a5\u5185\u5b58<\/h2>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/img.picgo.net\/2024\/10\/03\/imageda5465513f39eab0.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/img.picgo.net\/2024\/10\/03\/imageda5465513f39eab0.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image\" border=\"0\" \/><\/div><\/p>\n<hr \/>\n<h2>\u4f7f\u7528\u76f4\u63a5\u5185\u5b58<\/h2>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/img.picgo.net\/2024\/10\/03\/imagefa613e337a725412.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/img.picgo.net\/2024\/10\/03\/imagefa613e337a725412.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image\" border=\"0\" \/><\/div><\/p>\n<hr \/>\n<h2>\u76f4\u63a5\u5185\u5b58\u7684\u7279\u70b9<\/h2>\n<ul>\n<li>\u5e38\u89c1\u4e8e NIO \u64cd\u4f5c\u65f6\uff0c\u7528\u4e8e\u6570\u636e\u7f13\u51b2\u533a\uff0c\u63d0\u9ad8IO\u6027\u80fd<\/li>\n<li>\u5206\u914d\u56de\u6536\u6210\u672c\u8f83\u9ad8\uff0c\u4f46\u8bfb\u5199\u6027\u80fd\u9ad8<\/li>\n<li>\u4e0d\u53d7JVM \u5185\u5b58\u56de\u6536\u7ba1\u7406\uff08\u7cfb\u7edf\u5206\u914d\u7684\u5185\u5b58\uff0c\u4e0d\u53d7JVM\u7ba1\u7406\uff09<\/li>\n<\/ul>\n<hr \/>\n<h2>\u4ee3\u7801\u6f14\u793a<\/h2>\n<p>\u5728Java\u4e2d\u4f7f\u7528\u76f4\u63a5\u5185\u5b58\u9700\u8981\u901a\u8fc7ByteBuffer.allocateDirect()\u65b9\u6cd5\u8fdb\u884c\u5185\u5b58\u5206\u914d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<p>import java.nio.ByteBuffer;<\/p>\n<p>public class DirectMemoryTest {<br \/>\npublic static void main(String[] args) {<br \/>\n\/\/ \u5206\u914d\u76f4\u63a5\u5185\u5b58<br \/>\nByteBuffer directBuffer = ByteBuffer.allocateDirect(1024);<\/p>\n<p>\/\/ \u4f7f\u7528\u76f4\u63a5\u5185\u5b58<br \/>\ndirectBuffer.put((byte) 123); \/\/ \u793a\u4f8b\uff1a\u5199\u5165\u6570\u636e<br \/>\ndirectBuffer.flip(); \/\/ \u8f6c\u6362\u4e3a\u8bfb\u6a21\u5f0f<br \/>\nbyte value = directBuffer.get(); \/\/ \u793a\u4f8b\uff1a\u8bfb\u53d6\u6570\u636e<\/p>\n<p>System.out.println(&#8220;\u8bfb\u53d6\u7684\u6570\u636e\u4e3a: &#8221; + value);<\/p>\n<p>\/\/ \u63d0\u793aJVM\u8fdb\u884c\u5783\u573e\u6536\u96c6\uff0c\u6ce8\u610f\uff1a\u8fd9\u4e0d\u4f1a\u7acb\u5373\u56de\u6536\u76f4\u63a5\u5185\u5b58\uff0c\u76f4\u5230 directBuffer \u4e0d\u53ef\u8fbe\u540e\u624d\u4f1a\u5f00\u59cb\u56de\u6536\u76f4\u63a5\u5185\u5b58<br \/>\nSystem.gc();<br \/>\n}<br \/>\n}<\/p>\n<hr \/>\n<h2>NIO\u7f13\u51b2\u533a\u5206\u6790<\/h2>\n<p>NIO\u7684\u7f13\u51b2\u533a\u662f\u4e00\u5757\u53ef\u4ee5\u5199\u5165\u6570\u636e\uff0c\u7136\u540e\u53ef\u4ee5\u4ece\u4e2d\u8bfb\u53d6\u6570\u636e\u7684\u5185\u5b58\u533a\u57df\u3002Buffer\u662f\u4e00\u4e2a\u62bd\u8c61\u7c7b\uff0c\u5b83\u6700\u5e38\u7528\u7684\u7c7b\u578b\u662fByteBuffer\u3002\u9664\u4e86ByteBuffer\uff0c\u8fd8\u6709CharBuffer\u3001ShortBuffer\u3001IntBuffer\u3001LongBuffer\u3001FloatBuffer\u548cDoubleBuffer\u7b49\u3002<\/p>\n<p>\u5f53\u901a\u8fc7 allocateDirect() \u65b9\u6cd5\u521b\u5efa\u76f4\u63a5\u7f13\u51b2\u533a\u65f6\uff0cJava\u4f1a\u5728\u64cd\u4f5c\u7cfb\u7edf\u7684\u672c\u5730\u5806\u4e2d\u5206\u914d\u5185\u5b58\u3002\u7531\u4e8e\u907f\u5f00\u4e86JVM\u5806\uff0c\u56e0\u6b64\u63d0\u9ad8\u4e86\u6548\u7387\uff0c\u5c24\u5176\u662f\u5728\u5927\u91cf\u6570\u636e\u8bfb\u5199\u7684\u60c5\u51b5\u4e0b\u3002\u4f46\u662f\u9700\u8981\u6ce8\u610f\uff0c\u76f4\u63a5\u7f13\u51b2\u533a\u7684\u521b\u5efa\u548c\u9500\u6bc1\u7684\u6210\u672c\u8f83\u9ad8\uff0c\u4e0d\u9002\u7528\u4e8e\u751f\u547d\u5468\u671f\u77ed\u6682\u7684\u60c5\u51b5\u3002<\/p>\n<hr \/>\n<h2>\u76f4\u63a5\u5185\u5b58\u5783\u573e\u56de\u6536<\/h2>\n<p>\u5728Java\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528ByteBuffer\u7684cleaner() \u65b9\u6cd5\u6765\u83b7\u53d6\u4e00\u4e2a Cleaner \u5bf9\u8c61\uff0c\u7136\u540e\u901a\u8fc7\u5b83\u6765\u624b\u52a8\u91ca\u653e\u76f4\u63a5\u5185\u5b58\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<p>import sun.misc.Cleaner;<br \/>\nimport sun.misc.Unsafe;<br \/>\nimport java.lang.reflect.Field;<br \/>\nimport java.nio.ByteBuffer;<\/p>\n<p>public class DirectMemoryExample {<br \/>\npublic static void main(String[] args) {<br \/>\nByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024); \/\/ \u5206\u914d\u76f4\u63a5\u5185\u5b58<\/p>\n<p>\/\/ \u83b7\u53d6Unsafe\u5b9e\u4f8b<br \/>\ntry {<br \/>\nField field = Unsafe.class.getDeclaredField(&#8220;theUnsafe&#8221;);<br \/>\nfield.setAccessible(true);<br \/>\nUnsafe unsafe = (Unsafe) field.get(null);<\/p>\n<p>\/\/ \u83b7\u53d6\u76f4\u63a5\u5185\u5b58\u5730\u5740<br \/>\nlong address = unsafe.getLong(byteBuffer, byteBuffer.position());<\/p>\n<p>\/\/ \u521b\u5efaCleaner\u5b9e\u4f8b\uff0c\u5e76\u6ce8\u518c\u6e05\u7406\u4efb\u52a1<br \/>\nCleaner cleaner = Cleaner.create(byteBuffer, () -&gt; {<br \/>\n\/\/ \u5728\u6b64\u5904\u6267\u884c\u76f4\u63a5\u5185\u5b58\u91ca\u653e\u64cd\u4f5c<br \/>\nunsafe.freeMemory(address);<br \/>\n});<\/p>\n<p>\/\/ \u5f53ByteBuffer\u5bf9\u8c61\u53d8\u4e3a\u4e0d\u53ef\u8fbe\u65f6\uff0cCleaner\u4f1a\u81ea\u52a8\u6267\u884c\u91ca\u653e\u64cd\u4f5c<br \/>\n\/\/ \u4e3a\u4e86\u6f14\u793a\uff0c\u6211\u4eec\u5728\u8fd9\u91cc\u663e\u5f0f\u5730\u8ba9ByteBuffer\u53d8\u4e3a\u4e0d\u53ef\u8fbe<br \/>\nbyteBuffer = null;<br \/>\nSystem.gc(); \/\/ \u5efa\u8baeJVM\u6267\u884c\u5783\u573e\u56de\u6536<br \/>\n} catch (Exception e) {<br \/>\ne.printStackTrace();<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5f53 ByteBuffer \u5bf9\u8c61\u4e0d\u518d\u88ab\u5f15\u7528\u65f6\uff0cCleaner \u5bf9\u8c61\u4f1a\u8c03\u7528\u6307\u5b9a\u7684 Runnable \u6765\u6267\u884c\u76f4\u63a5\u5185\u5b58\u7684\u91ca\u653e\u64cd\u4f5c\u3002\u8fd9\u79cd\u65b9\u5f0f\u867d\u7136\u4e0d\u662f\u7531Java\u865a\u62df\u673a\u7684\u5783\u573e\u56de\u6536\u5668\u6765\u7ba1\u7406\u76f4\u63a5\u5185\u5b58\uff0c\u4f46\u662f\u53ef\u4ee5\u5e2e\u52a9\u786e\u4fdd\u76f4\u63a5\u5185\u5b58\u5f97\u5230\u53ca\u65f6\u91ca\u653e\uff0c\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<hr \/>\n<h2>\u6ce8\u610f\u4e8b\u9879<\/h2>\n<p>\u4f7f\u7528\u76f4\u63a5\u5185\u5b58\u9700\u8981\u6ce8\u610f\u51e0\u4e2a\u5173\u952e\u70b9\uff1a<\/p>\n<ul>\n<li>\u5408\u7406\u5927\u5c0f\uff1a\u5206\u914d\u592a\u591a\u76f4\u63a5\u5185\u5b58\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7cfb\u7edf\u7ea7\u522b\u7684\u5185\u5b58\u4e0d\u8db3\u3002<\/li>\n<li>\u91ca\u653e\u8d44\u6e90\uff1a\u4f7f\u7528\u5b8c\u6bd5\u540e\u5e94\u53ca\u65f6\u91ca\u653e\u76f4\u63a5\u5185\u5b58\uff0c\u867d\u7136Java\u4f1a\u6700\u7ec8\u91ca\u653e\uff0c\u4f46\u4e0d\u53ca\u65f6\u91ca\u653e\u53ef\u80fd\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u9732\u3002<\/li>\n<li>\u5185\u5b58\u6620\u5c04\u6587\u4ef6\uff1a\u76f4\u63a5\u5185\u5b58\u901a\u5e38\u4e0e\u5185\u5b58\u6620\u5c04\u6587\u4ef6\uff08Memory-Mapped File\uff09\u914d\u5408\u4f7f\u7528\uff0c\u9002\u7528\u4e8e\u6709\u5927\u91cf\u6570\u636e\u8bfb\u5199\u78c1\u76d8\u7684\u573a\u666f\u3002\n<p>\u901a\u8fc7\u6b63\u786e\u4f7f\u7528\u76f4\u63a5\u5185\u5b58\uff0c\u53ef\u4ee5\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u663e\u8457\u63d0\u5347Java\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u7136\u800c\uff0c\u4e5f\u8981\u8c28\u614e\u7ba1\u7406\uff0c\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u548c\u5176\u4ed6\u8d44\u6e90\u7ba1\u7406\u9519\u8bef\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8ff0 \u76f4\u63a5\u5185\u5b58(Direct Memory)\u662f\u5728Java\u4e2d\u4f7f\u7528\u8f83\u4e3a\u5e95\u5c42\u7684\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f\uff0c\u5b83\u901a\u8fc7Java\u7684NIO\u5305 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":279,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-278","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/posts\/278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/comments?post=278"}],"version-history":[{"count":2,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/posts\/278\/revisions"}],"predecessor-version":[{"id":280,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/posts\/278\/revisions\/280"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/media\/279"}],"wp:attachment":[{"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/media?parent=278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/categories?post=278"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/tags?post=278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}