{"id":966,"date":"2018-07-04T11:39:05","date_gmt":"2018-07-04T03:39:05","guid":{"rendered":"http:\/\/www.rain1024.com\/?p=966"},"modified":"2023-08-07T20:57:25","modified_gmt":"2023-08-07T12:57:25","slug":"article129","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2018\/07\/04\/article129\/","title":{"rendered":"Java\u865a\u62df\u673a\u5185\u5b58\u533a\u57df\u6ea2\u51fa\u6d4b\u8bd5"},"content":{"rendered":"<h1>Java\u865a\u62df\u673a\u5185\u5b58\u533a\u57df\u6ea2\u51fa\u6d4b\u8bd5<\/h1>\n<h3>\u6d4b\u8bd5\u5185\u5b58\u6ea2\u51fa\u524d\uff0c\u5148\u914d\u7f6eDebug configurations\uff0c\u5728Arguments\u4e2d\u7684VM arguments\u6587\u672c\u6846\u4e2d\u8f93\u5165\u5bf9\u5e94\u7684\u865a\u62df\u673a\u542f\u52a8\u53c2\u6570\u3002\u7136\u540e\u5728\u8fdb\u884cRun\u6267\u884c\u3002\u8fd9\u6837\u53ef\u4ee5\u8ba9\u865a\u62df\u673a\u5728\u51fa\u73b0\u5185\u5b58\u6ea2\u51fa\u5f02\u5e38\u65f6Dump\u5f53\u524d\u7684\u5185\u5b58\u5806\u8f6c\u50a8\u5feb\u7167\u4ee5\u4fbf\u4e8b\u540e\u8fdb\u884c\u5206\u6790\u3002<\/h3>\n<h2>Java\u5806\u6ea2\u51fa<\/h2>\n<pre><code>import java.util.ArrayList;\nimport java.util.List;\n\n\/**\n * VM Args: -Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError\n * @author Rain\n *\n *\/\npublic class heapOOM {\n    static class OOMObject{\n\n    }\n    public static void main(String[] args){\n        List&lt;OOMObject&gt; list = new ArrayList&lt;OOMObject&gt;();\n        while(true){\n            list.add(new OOMObject());\n        }\n    }\n}\n\n<\/code><\/pre>\n<h3>\u8fd0\u884c\u7ed3\u679c<\/h3>\n<pre><code>java.lang.OutOfMemoryError: Java heap space\nDumping heap to java_pid4572.hprof ...\nHeap dump file created [28022194 bytes in 0.087 secs]\n<\/code><\/pre>\n<h3>Java\u5806\u5185\u5b58\u7684OOM\u5f02\u5e38\u662f\u5b9e\u9645\u5e94\u7528\u4e2d\u5e38\u89c1\u7684\u5185\u5b58\u6ea2\u51fa\u5f02\u5e38\u60c5\u51b5\u3002\u56e0\u4e3aJava\u5806\u7528\u4e8e\u5b58\u50a8\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u53ea\u8981\u4e0d\u65ad\u7684\u521b\u5efa\u5bf9\u8c61\uff0c\u8d85\u8fc7\u6700\u5927\u5806\u7684\u5bb9\u91cf\u9650\u5236\u5fc5\u7136\u6ea2\u51fa\uff0c\u5f53\u51fa\u73b0Java\u5806\u5185\u5b58\u6ea2\u51fa\u65f6\uff0c\u5f02\u5e38\u5806\u6808\u4fe1\u606f\u63d0\u793a\uff1a<code>java.lang.OutOfMemoryError: Java heap space<\/code><\/h3>\n<h2>\u865a\u62df\u673a\u6808\u548c\u672c\u5730\u65b9\u6cd5\u6808\u6ea2\u51fa<\/h2>\n<pre><code>\/**\n * VM Args:-Xss128k\n * @author Rain\n *\n *\/\npublic class JavaVMStackSOF {\n    private int stackLength = 1;\n    public void stackLeak(){\n        stackLength++;\n        stackLeak();\n    }\n    public static void main(String[] args)throws Throwable{\n        JavaVMStackSOF oom = new JavaVMStackSOF();\n        try{\n            oom.stackLeak();\n        }catch (Throwable e){\n            System.out.println(\"stack length:\"+oom.stackLength);\n            throw e;\n        }\n    }\n}\n<\/code><\/pre>\n<h3>\u8f93\u51fa\u7ed3\u679c\uff1a<\/h3>\n<pre><code>stack length:19479\nException in thread \"main\" java.lang.StackOverflowError\n<\/code><\/pre>\n<h3>\u56e0\u4e3a\u5185\u5b58\u5206\u914d\u539f\u5219\uff0c\u865a\u62df\u673a\u6808\u548c\u672c\u673a\u65b9\u6cd5\u6808\u662f\u6700\u540e\u5206\u914d\u5185\u5b58\u7684\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u5206\u914d\u5230\u7684\u6808\u5bb9\u91cf\u8d8a\u5927\uff0c\u53ef\u4ee5\u5efa\u7acb\u7684\u7ebf\u7a0b\u6570\u91cf\u81ea\u7136\u8d8a\u5c11\uff0c\u5efa\u7acb\u7ebf\u7a0b\u65f6\u5c31\u8d8a\u5bb9\u6613\u628a\u5269\u4e0b\u7684\u5185\u5b58\u8017\u5c3d\u3002\u5982\u679c\u4f7f\u7528\u865a\u62df\u673a\u9ed8\u8ba4\u53c2\u6570\uff0c\u6808\u6df1\u5ea6\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u8fbe\u52301000~2000\u5b8c\u5168\u6ca1\u6709\u95ee\u9898\uff0c\u6b63\u5e38\u7684\u65b9\u6cd5\u8c03\u7528\u548c\u9012\u5f52\u662f\u591f\u7528\u4e86\uff0c\u4f46\u662f\u5728\u5efa\u7acb\u8fc7\u591a\u7ebf\u7a0b\u5bfc\u81f4\u7684\u5185\u5b58\u6ea2\u51fa\uff0c\u5728\u4e0d\u80fd\u51cf\u5c11\u7ebf\u7a0b\u6570\u6216\u8005\u66f4\u636264\u4f4d\u865a\u62df\u673a\u7684\u60c5\u51b5\u4e0b\uff0c\u5c31\u53ea\u80fd\u51cf\u5c11\u6700\u5927\u5806\u548c\u51cf\u5c11\u6808\u5bb9\u91cf\u6765\u6362\u53d6\u66f4\u591a\u7684\u7ebf\u7a0b\u3002<\/h3>\n<h2>\u65b9\u6cd5\u533a\u548c\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60\u6ea2\u51fa<\/h2>\n<pre><code>import java.util.ArrayList;\nimport java.util.List;\n\/**\n * VM Args: -XX:PermSize=10M -XX:MaxPermSize=10M\n * @author Rain\n *\n *\/\npublic class RuntimeConstantPoolOOM {\n\n    public static void main(String[] args){\n        \/\/ \u4f7f\u7528list\u4fdd\u6301\u7740\u5e38\u91cf\u6c60\u5f15\u7528\uff0c\u907f\u514dFull GC\u56de\u6536\u5e38\u91cf\u6c60\u884c\u4e3a\n        List&lt;String&gt; list = new ArrayList&lt;String&gt;();\n        \/\/ 10MB\u7684permsize\u5728integer\u8303\u56f4\u5185\u8db3\u591f\u4ea7\u751fOOM \n        int i = 0;\n        while(true){\n            list.add(String.valueOf(i++).intern());\n        }\n    }\n}\n<\/code><\/pre>\n<h3>\u8fd9\u6bb5\u4ee3\u7801\u53ea\u6709\u5728JDK1.6\u7248\u672c\u8fd0\u884c\u624d\u4f1a\u62a5\u9519\uff0c1.7\u4ee5\u4e0a\u9010\u6b65\u201c\u53bb\u6c38\u4e45\u4ee3\u201d\u3002<\/h3>\n<h3>\u9664\u6b64\u4e4b\u5916\u8fd8\u6709\u501f\u52a9CGLib\u4f7f\u65b9\u6cd5\u533a\u51fa\u73b0\u5185\u5b58\u6ea2\u51fa\u5f02\u5e38\uff0c\u672c\u673a\u76f4\u63a5\u5185\u5b58\u6ea2\u51fa\uff0c\u5148\u4e0d\u8fdb\u884c\u4ee3\u7801\u6f14\u793a\u3002<\/h3>\n","protected":false},"excerpt":{"rendered":"<p>Java\u865a\u62df\u673a\u5185\u5b58\u533a\u57df\u6ea2\u51fa\u6d4b\u8bd5 \u6d4b\u8bd5\u5185\u5b58\u6ea2\u51fa\u524d\uff0c\u5148\u914d\u7f6eDebug configurations\uff0c\u5728Argume\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2018\/07\/04\/article129\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,18],"tags":[51,90],"class_list":["post-966","post","type-post","status-publish","format-standard","hentry","category-java","category-technique","tag-jvm","tag-90"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/966","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=966"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/966\/revisions"}],"predecessor-version":[{"id":1404,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/966\/revisions\/1404"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=966"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}