{"id":3371,"date":"2018-11-15T16:53:57","date_gmt":"2018-11-15T08:53:57","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3371"},"modified":"2018-11-15T16:53:57","modified_gmt":"2018-11-15T08:53:57","slug":"java-lang-outofmemoryerror-requested-array-size-exceeds-vm-limit","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3371","title":{"rendered":"java.lang.OutOfMemoryError: Requested array size exceeds VM limit"},"content":{"rendered":"<p>\n\t\u6709\u4e9b\u4eba\u53ef\u80fd\u5199\u4e2a\u597d\u591a\u5e74\u7684\u4ee3\u7801\uff0c\u4f46\u662f\u4ed6\u4e0d\u4e00\u5b9a\u9047\u89c1\u8fc7\u8fd9\u4e2a\u9519\u8bef\u3002java.lang.OutOfMemoryError: Requested array size exceeds VM limit \u9519\u8bef\u975e\u5e38\u7684\u5c11\u89c1\uff0c\u4f60\u53ef\u4ee5\u5728\u9762\u8bd5\u4e2d\u95ee\u95ee\uff0c\u80fd\u8bf4\u660e\u767d\u8fd9\u4e2a\u9519\u8bef\u7684\u7edd\u5bf9\u662f\u8ba4\u771f\u770b\u8fc7 JVM \u7684\u3002\u4eca\u5929\u6211\u4eec\u5c31\u8bf4\u8bf4&nbsp;Requested array size exceeds VM limit \uff08\u521b\u5efa\u7684\u6570\u7ec4\u957f\u5ea6\u8d85\u8fc7\u9650\u5236\uff09\u3002\n<\/p>\n<p>\n\tJava \u8bed\u8a00\u5176\u5b9e\u662f\u9650\u5236\u4e86\u6570\u7ec4\u7684\u6700\u5927\u957f\u5ea6\u3002\u8fd9\u4e2a\u4e0d\u662f\u5e95\u5c42 JVM \u9650\u5236\u7684\uff0c\u800c\u662f Java \u8bed\u8a00\u5728\u521b\u5efa\u6570\u7ec4\u65f6\uff0c\u5c31\u9650\u5236\u4e86\u6570\u7ec4\u7684\u957f\u5ea6\u6700\u5927\u4e3a Integer.MAX_VALUE\u3002\u56e0\u4e3a\u6570\u7ec4\u7684\u4e0b\u6807\u5c31\u662f int \u7c7b\u578b\u7684\uff0c\u4f60\u6700\u5927\u4e5f\u4e0d\u53ef\u80fd\u8d85\u8fc7 Integer.MAX_VALUE\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"java.lang.OutOfMemoryError: Requested array size exceeds VM limit\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/jvm-array-size.png\" style=\"width: 712px;height: 231px\" \/>\n<\/p>\n<p>\n\t\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7684\u64cd\u4f5c\u7cfb\u7edf\u548c JDK \u90fd\u9650\u5236\u7684\u6570\u7ec4\u7684\u5ea6\u6700\u5927\u957f\u4e3a Integer.MAX_VALUE\u3002\u6bd4\u5982\u5728 64\u4f4d\u7684 MB Pro \u4e0a\uff0cJava 1.7 \u5e73\u53f0\u53ef\u4ee5\u5206\u914d\u957f\u5ea6\u4e3a 2,147,483,645, \u4ee5\u53ca Integer.MAX_VALUE-2) \u7684\u6570\u7ec4\u3002\u4f46\u5dee\u522b\u4e0d\u662f\u7279\u522b\u7684\u5927\uff0c\u57fa\u672c\u4e0a\u90fd\u5728 1 ~ 21\u4ebf \u4e4b\u95f4\u7684\u8303\u56f4\u3002\n<\/p>\n<p>\n\tjava.lang.OutOfMemoryError: Requested array size exceeds VM limit \u9519\u8bef\u662f\u7531 JVM \u4e2d\u7684\u672c\u5730\u4ee3\u7801\u629b\u51fa\u7684\u3002\n<\/p>\n<p>\n\t\u5728\u4e3a\u6570\u7ec4\u5206\u914d\u5185\u5b58\u4e4b\u524d\uff0cJVM \u4f1a\u6267\u884c\u4e00\u9879\u68c0\u67e5\u3002\u8981\u5206\u914d\u7684\u6570\u636e\u7ed3\u6784\u5728\u8be5\u5e73\u53f0\u662f\u5426\u53ef\u4ee5\u5bfb\u5740(addressable)\u3002\u5982\u679c\u4e0d\u80fd\u5bfb\u5740(addressable)\u5c31\u4f1a\u629b\u51fa\u8fd9\u4e2a\u9519\u8bef\u3002\n<\/p>\n<p>\n\t\u4e0b\u9762\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u793a\u4f8b\u6765\u6f14\u793a&nbsp;java.lang.OutOfMemoryError: Requested array size exceeds VM limit \u9519\u8bef\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nfor (int i = 3; i &gt;= 0; i--) {\r\n  try {\r\n    int[] arr = new int[Integer.MAX_VALUE-i];\r\n    System.out.format(&quot;Successfully initialized an array with %,d elements.\\n&quot;, Integer.MAX_VALUE-i);\r\n  } catch (Throwable t) {\r\n    t.printStackTrace();\r\n  }\r\n}<\/pre>\n<p>\n\t\u6267\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u5728 64\u4f4d Mac OS X \u7684 Hotspot 7 \u5e73\u53f0\u4e0a\uff0c\u5c31\u4f1a\u5f97\u5230\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\u7684\u7ed3\u679c\uff1a\n<\/p>\n<pre class=\"brush:plain;\">\r\njava.lang.OutOfMemoryError: Java heap space\r\n  at eu.plumbr.demo.ArraySize.main(ArraySize.java:8)\r\njava.lang.OutOfMemoryError: Java heap space\r\n  at eu.plumbr.demo.ArraySize.main(ArraySize.java:8)\r\njava.lang.OutOfMemoryError: Requested array size exceeds VM limit\r\n  at eu.plumbr.demo.ArraySize.main(ArraySize.java:8)\r\njava.lang.OutOfMemoryError: Requested array size exceeds VM limit\r\n  at eu.plumbr.demo.ArraySize.main(ArraySize.java:8)<\/pre>\n<p>\n\t\u524d\u9762\u4e24\u6b21\u629b\u51fa\u4e86 Java heap space \u9519\u8bef\u3002\u8fd9\u662f\u56e0\u4e3a 2^31-1 \u4e2a int \u6570\u5360\u7528\u7684\u5185\u5b58\u8d85\u8fc7\u4e86JVM\u9ed8\u8ba4\u76848GB\u5806\u5185\u5b58\u3002\u6240\u4ee5\u8bf4 Requested array size exceeds VM limit \u9519\u8bef\u5f88\u5c11\u88ab\u770b\u5230\u3002\n<\/p>\n<p>\n\t\u53d1\u751f java.lang.OutOfMemoryError: Requested array size exceeds VM limit \u9519\u8bef\u7684\u539f\u56e0\u5f88\u53ef\u80fd\u5c31\u662f\u4f60\u7684\u4ee3\u7801\u95ee\u9898\u3002\n<\/p>\n<p>\n\t\u6570\u7ec4\u592a\u5927\uff0c\u6700\u7ec8\u957f\u5ea6\u8d85\u8fc7\u5e73\u53f0\u9650\u5236\u503c\uff0c\u4f46\u5c0f\u4e8e Integer.MAX_VALUE\u3002\n<\/p>\n<p>\n\t\u89e3\u51b3\u65b9\u6cd5\u5c31\u662f\u68c0\u67e5\u4f60\u7684\u4ee3\u7801\u4e2d\u662f\u5426\u6709\u521b\u5efa\u8d85\u5927\u6570\u7ec4\u7684\u5730\u65b9\u3002\n<\/p>\n<p>\n\t\u5982\u679c\u786e\u5b9e\u9700\u8981\u5904\u7406\u8d85\u5927\u6570\u636e\u96c6\uff0c\u90a3\u5c31\u8981\u8003\u8651\u8c03\u6574\u89e3\u51b3\u65b9\u6848\u4e86\u3002\u4f8b\u5982\u62c6\u5206\u6210\u591a\u4e2a\u5c0f\u5757\uff0c\u6309\u6279\u6b21\u52a0\u8f7d\uff1b\u6216\u8005\u653e\u5f03\u4f7f\u7528\u6807\u51c6\u5e93\uff0c\u800c\u662f\u81ea\u5df1\u5904\u7406\u6570\u636e\u7ed3\u6784\uff0c\u6bd4\u5982\u4f7f\u7528 sun.misc.Unsafe \u7c7b\uff0c\u901a\u8fc7 Unsafe \u5de5\u5177\u7c7b\u53ef\u4ee5\u50cf C \u8bed\u8a00\u4e00\u6837\u76f4\u63a5\u5206\u914d\u5185\u5b58\u3002\n<\/p>\n<h2>\n\t\u53c2\u8003\u8d44\u6599<br \/>\n<\/h2>\n<ul>\n<li>\n\t\t<a href=\"https:\/\/plumbr.io\/outofmemoryerror\/requested-array-size-exceeds-vm-limit\" name=\"java.lang.OutOfMemoryError:Requested array size exceeds VM limit\" target=\"_blank\" title=\"java.lang.OutOfMemoryError:Requested array size exceeds VM limit\">java.lang.OutOfMemoryError:Requested array size exceeds VM limit<\/a>\n\t<\/li>\n<\/ul>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3371\">java.lang.OutOfMemoryError: Requested array size exceeds VM limit<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6709\u4e9b\u4eba\u53ef\u80fd\u5199\u4e2a\u597d\u591a\u5e74\u7684\u4ee3\u7801\uff0c\u4f46\u662f\u4ed6\u4e0d\u4e00\u5b9a\u9047\u89c1\u8fc7\u8fd9\u4e2a\u9519\u8bef\u3002java.lang.OutOfMemoryError: [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,6],"tags":[1740,1732,1733,601,1751],"class_list":["post-3371","post","type-post","status-publish","format-standard","hentry","category-java","category-yyzt","tag-atomicinteger","tag-integer","tag-integercache","tag-outofmemoryerror","tag-unsafe"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3371","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3371"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3371\/revisions"}],"predecessor-version":[{"id":3372,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3371\/revisions\/3372"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}