{"id":213303,"date":"2021-03-20T10:14:43","date_gmt":"2021-03-20T02:14:43","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=213303"},"modified":"2021-03-08T08:15:49","modified_gmt":"2021-03-08T00:15:49","slug":"java-memory-overflow","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/java-memory-overflow.html","title":{"rendered":"Java\u5185\u5b58\u6ea2\u51fa"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5185\u5b58\u6ea2\u51fa\u662f\u6307\u5e94\u7528\u7cfb\u7edf\u4e2d\u5b58\u5728\u65e0\u6cd5\u56de\u6536\u7684\u5185\u5b58\u6216\u4f7f\u7528\u7684\u5185\u5b58\u8fc7\u591a\uff0c\u6700\u7ec8\u4f7f\u5f97\u7a0b\u5e8f\u8fd0\u884c\u8981\u7528\u5230\u7684\u5185\u5b58\u5927\u4e8e\u865a\u62df\u673a\u80fd\u63d0\u4f9b\u7684\u6700\u5927\u5185\u5b58\u3002\u8fd9\u7bc7\u6587\u7ae0\u6574\u7406\u81ea\u300a\u6df1\u5165\u7406\u89e3java\u865a\u62df\u673a\u300b\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4e00\u3001\u5185\u5b58\u6ea2\u51fa\u539f\u56e0<\/strong><\/div>\n

\u5185\u5b58\u6ea2\u51fa\u5c31\u662f\u5185\u5b58\u4e0d\u591f\uff0c\u5f15\u8d77\u5185\u5b58\u6ea2\u51fa\u7684\u539f\u56e0\u6709\u5f88\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n

    1\u3001\u5185\u5b58\u4e2d\u52a0\u8f7d\u7684\u6570\u636e\u91cf\u8fc7\u4e8e\u5e9e\u5927\uff0c\u5982\u4e00\u6b21\u4ece\u6570\u636e\u5e93\u53d6\u51fa\u8fc7\u591a\u6570\u636e;<\/ol>\n
      2\u3001\u96c6\u5408\u7c7b\u4e2d\u6709\u5bf9\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u4f7f\u7528\u5b8c\u540e\u672a\u6e05\u7a7a\uff0c\u4f7f\u5f97JVM\u4e0d\u80fd\u56de\u6536;<\/ol>\n
        3\u3001\u4ee3\u7801\u4e2d\u5b58\u5728\u6b7b\u5faa\u73af\u6216\u5faa\u73af\u4ea7\u751f\u8fc7\u591a\u91cd\u590d\u7684\u5bf9\u8c61\u5b9e\u4f53;<\/ol>\n
          4\u3001\u4f7f\u7528\u7684\u7b2c\u4e09\u65b9\u8f6f\u4ef6\u4e2d\u7684BUG;<\/ol>\n
            5\u3001\u542f\u52a8\u53c2\u6570\u5185\u5b58\u503c\u8bbe\u5b9a\u7684\u8fc7\u5c0f;<\/ol>\n

            \u5f53\u7136\u5b9e\u9645\u60c5\u51b5\u4e2d\u5185\u5b58\u6ea2\u51fa\u7684\u539f\u56e0\u5c31\u592a\u591a\u4e86\u3002\u4e0b\u9762\u6211\u4eec\u5c31\u5bf9\u8fd9\u4e9b\u539f\u56e0\u5206\u7c7b\u4e00\u4e0b\uff1a<\/p>\n

            \u4ee5\u4e0a\u7684\u56fe\u662f\u57fa\u4e8ejava7\u6765\u53d9\u8ff0\u7684\uff0c\u4ece\u4e0a\u9762\u8fd9\u5f20\u56fe\u6211\u4eec\u80fd\u591f\u5f97\u5230\u5982\u4e0b\u4fe1\u606f\uff1ajava\u865a\u62df\u673a\u628a\u5185\u5b58\u5206\u4e3a5\u4e2a\u6a21\u5757\u3002<\/p>\n

              (1)\u7a0b\u5e8f\u8ba1\u6570\u5668\uff1a<\/ol>\n

              \u7a0b\u5e8f\u8ba1\u6570\u5668\u662f\u7ebf\u7a0b\u79c1\u6709\u7684\uff0c\u4e3b\u8981\u7684\u4f5c\u7528\u662f\u901a\u8fc7\u6539\u53d8\u8fd9\u4e2a\u8ba1\u6570\u5668\u7684\u503c\u6765\u9009\u53d6\u4e0b\u4e00\u6761\u9700\u8981\u6267\u884c\u7684\u5b57\u8282\u7801\u6307\u4ee4\u3002\u65e2\u7136\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u4e00\u4e2a\uff0c\u90a3\u4e48\u8fd9\u4e9b\u7ebf\u7a0b\u7684\u8ba1\u6570\u5668\u662f\u4e92\u4e0d\u5f71\u54cd\u7684\u3002\u4e5f\u4e0d\u4f1a\u629b\u51fa\u4efb\u4f55\u5f02\u5e38\u3002<\/p>\n

                (2)\u865a\u62df\u673a\u6808\u548c\u672c\u5730\u65b9\u6cd5\u6808\uff1a<\/ol>\n

                \u865a\u62df\u673a\u6808\u63cf\u8ff0\u7684\u662fjava\u65b9\u6cd5\u6267\u884c\u7684\u5185\u5b58\u6a21\u578b\uff0c\u6bcf\u4e2a\u65b9\u6cd5\u5728\u6267\u884c\u7684\u65f6\u5019\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u6808\u5e27\u7528\u4e8e\u5b58\u50a8\u5c40\u90e8\u53d8\u91cf\u8868\u3001\u64cd\u4f5c\u6570\u6808\u3001\u52a8\u6001\u8fde\u63a5\u3001\u65b9\u6cd5\u51fa\u53e3\u7b49\u4fe1\u606f\u3002\u672c\u5730\u65b9\u6cd5\u6808\u4e0e\u865a\u62df\u673a\u6808\u7684\u533a\u522b\u662f\uff0c\u865a\u62df\u673a\u6808\u4e3a\u865a\u62df\u673a\u6267\u884cjava\u65b9\u6cd5\u670d\u52a1\uff0c\u800c\u672c\u5730\u65b9\u6cd5\u6808\u5219\u4e3a\u865a\u62df\u673a\u63d0\u4f9bnative\u65b9\u6cd5\u670d\u52a1\u3002<\/p>\n

                \u5728\u5355\u7ebf\u7a0b\u7684\u64cd\u4f5c\u4e2d\uff0c\u65e0\u8bba\u662f\u7531\u4e8e\u6808\u5e27\u592a\u5927\uff0c\u8fd8\u662f\u865a\u62df\u673a\u6808\u7a7a\u95f4\u592a\u5c0f\uff0c\u5f53\u6808\u7a7a\u95f4\u65e0\u6cd5\u5206\u914d\u65f6\uff0c\u865a\u62df\u673a\u629b\u51fa\u7684\u90fd\u662fStackOverflowError\u5f02\u5e38\uff0c\u800c\u4e0d\u4f1a\u5f97\u5230OutOfMemoryError\u5f02\u5e38\u3002\u800c\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0c\u5219\u4f1a\u629b\u51faOutOfMemoryError\u5f02\u5e38\u3002<\/p>\n

                  (3)java\u5806\u548c\u65b9\u6cd5\u533a\uff1a<\/ol>\n

                  java\u5806\u533a\u4e3b\u8981\u5b58\u653e\u5bf9\u8c61\u5b9e\u4f8b\u548c\u6570\u7ec4\u7b49\uff0c\u65b9\u6cd5\u533a\u4fdd\u5b58\u7c7b\u4fe1\u606f\u3001\u5e38\u91cf\u3001\u9759\u6001\u53d8\u91cf\u7b49\u7b49\u3002\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60\u4e5f\u662f\u65b9\u6cd5\u533a\u7684\u4e00\u90e8\u5206\u3002\u8fd9\u4e24\u5757\u533a\u57df\u662f\u7ebf\u7a0b\u5171\u4eab\u7684\u533a\u57df\uff0c\u53ea\u4f1a\u629b\u51faOutOfMemoryError\u3002<\/p>\n

                  \u4e0d\u77e5\u9053\u5404\u4f4d\u5728B\u7ad9\u770b\u89c1\u8fc7\u90a3\u4e2a\u9762\u8bd5\u7ecf\u5178\u573a\u666f\u6ca1\uff0c\u5728\u56de\u7b54java\u7684\u5185\u5b58\u8fd0\u884c\u6570\u636e\u533a\u7ed3\u6784\u65f6\uff0c\u4ee5\u4e0a\u7684\u529f\u80fd\u4f5c\u7528\u662f\u4e00\u65b9\u9762\uff0c\u5982\u679c\u56de\u7b54\u65f6\u628a\u5185\u5b58\u6ea2\u51fa\u95ee\u9898\u6dfb\u52a0\u4e0a\u662f\u4e00\u4e2a\u6781\u5927\u7684\u52a0\u5206\u9879\u3002<\/p>\n

                  \u4e8c\u3001\u5185\u5b58\u6ea2\u51fa\u5b9e\u4f8b<\/strong><\/div>\n
                  1\u3001\u5806\u6ea2\u51fa<\/strong><\/span><\/div>\n

                  \u65e2\u7136\u5806\u662f\u5b58\u653e\u5b9e\u4f8b\u5bf9\u8c61\u7684\uff0c\u90a3\u6211\u4eec\u5c31\u65e0\u7ebf\u521b\u5efa\u5b9e\u4f8b\u5bf9\u8c61\u3002\u8fd9\u6837\u5806\u533a\u8fdf\u65e9\u4f1a\u6ee1\u3002<\/p>\n

                  \r\npublic class HeapOOM { \r\n    static class User {} \r\n public static void main(String[] args) { \r\n   List list = new ArrayList<\/user>(); \r\n         while (true) { \r\n             list.add(new User()); \r\n      } \r\n } \r\n} \r\n\/*Exception in thread \"main\" java.lang.OutOfMemoryError:  \r\nGC overhead limit exceeded \r\n at com.fdd.test.HeapOOM.main(HeapOOM.java:11)*\/\r\n<\/user><\/pre>\n

                  \u56e0\u4e3a\u6211\u63d0\u524d\u8bbe\u7f6e\u4e86\u5806\u533a\u5185\u5b58\uff0c\u6240\u4ee5\u65e0\u9650\u521b\u5efa\u5c31\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n

                  2\u3001\u865a\u62df\u673a\u6808\u548c\u672c\u5730\u65b9\u6cd5\u6808\u6ea2\u51fa<\/strong><\/span><\/div>\n

                  Java\u865a\u62df\u673a\u89c4\u8303\u4e2d\u63cf\u8ff0\u4e86\u4e24\u79cd\u5f02\u5e38\uff1a<\/p>\n

                    \u5982\u679c\u7ebf\u7a0b\u8bf7\u6c42\u7684\u6808\u6df1\u5ea6\u5927\u4e8e\u865a\u62df\u673a\u9501\u5141\u8bb8\u7684\u6700\u5927\u6df1\u5ea6\uff0c\u5c06\u629b\u51faStackOverflowError\u5f02\u5e38\u3002<\/ol>\n
                      \u5982\u679c\u865a\u62df\u673a\u5728\u6269\u5c55\u6808\u65f6\u65e0\u6cd5\u7533\u8bf7\u5230\u8db3\u591f\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u5219\u629b\u51faOutOfMemoryError\u5f02\u5e38\u3002<\/ol>\n

                      \u7b2c\u4e00\u79cd\u6211\u4eec\u53ea\u9700\u8981\u4f7f\u7528\u65b9\u6cd5\u9012\u5f52\u8c03\u7528\u5373\u53ef\u6a21\u62df\uff1a<\/p>\n

                      \r\npublic class StackOutOfMemoryError { \r\n    public static void main(String[] args) {      \r\n         test(); \r\n    } \r\n    private static void go() { \r\n        System.out.println(\"StackOverflowError\u5f02\u5e38\"); \r\n        test(); \r\n    } \r\n} \r\n\/*Exception in thread \"main\" java.lang.StackOverflowError \r\n at sun.nio.cs.ext.DoubleByte$Encoder.encodeLoop(DoubleByte.java:617) \r\n at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:579) \r\n at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:271) \r\n at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125) \r\n at java.io.OutputStreamWriter.write(OutputStreamWriter.java:207) \r\n at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:129) \r\n at java.io.PrintStream.write(PrintStream.java:526) \r\n at java.io.PrintStream.print(PrintStream.java:597) \r\n at java.io.PrintStream.println(PrintStream.java:736) \r\n at com.fdd.test.StackOutOfMemoryError.go(StackOutOfMemoryError.java:11) \r\n at com.fdd.test.StackOutOfMemoryError.go(StackOutOfMemoryError.java:13)*\/\r\n<\/pre>\n

                      \u7b2c\u4e8c\u79cd\u4e5f\u53ef\u4ee5\u9012\u5f52\u8c03\u7528\u6a21\u62df,\uff0c\u4f46\u662f\u4f7f\u7528\u7684\u662f\u7c7b\u76f4\u63a5\u8c03\u7528\u3002<\/p>\n

                      \r\npublic class JavaVMStackSOF { \r\n    private int stackLength = 1; \r\n    public void stackLeak() { \r\n        stackLength++; \r\n        stackLeak(); \r\n    } \r\n public static void main(String[] args) { \r\n        JavaVMStackSOF oom = new JavaVMStackSOF(); \r\n        oom.stackLeak(); \r\n    } \r\n} \r\n\/*Exception in thread \"main\" java.lang.StackOverflowError \r\n   at com.lindaxuan.outofmemory.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:18) \r\n   at com.lindaxuan.outofmemory.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:19) \r\n   at com.lindaxuan.outofmemory.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:19) \r\n   at com.lindaxuan.outofmemory.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:19) \r\n   at com.lindaxuan.outofmemory.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:19) \r\n   at com.lindaxuan.outofmemory.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:19) \r\n   at com.lindaxuan.outofmemory.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:19) \r\n   at com.lindaxuan.outofmemory.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:19) \r\n   at com.lindaxuan.outofmemory.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:19) \r\n   ... *\/\r\n<\/pre>\n
                      3\u3001\u65b9\u6cd5\u533a\u548c\u8fd0\u884c\u65f6\u5e38\u91cf\u6c60\u6ea2\u51fa<\/strong><\/span><\/div>\n
                      \r\npublic class JavaMethodAreaOOM { \r\n    public static void main(String[] args) { \r\n        while (true) { \r\n            Enhancer enhancer = new Enhancer(); \r\n            enhancer.setSuperclass(User.class); \r\n            enhancer.setUseCache(false); \r\n            enhancer.setCallback(new MethodInterceptor() { \r\n                public Object intercept(Object obj, Method method, \r\n                      Object[] args, MethodProxy proxy) throws Throwable { \r\n                    return proxy.invokeSuper(obj, args); \r\n                } \r\n            }); \r\n            enhancer.create(); \r\n        } \r\n    } \r\n    static class User {} \r\n} \r\n\/*Exception in thread \"main\" \r\n Exception: java.lang.OutOfMemoryError thrown  \r\n from the UncaughtExceptionHandler in thread \"main\" \r\n*\/\r\n<\/pre>\n
                      4\u3001\u672c\u673a\u76f4\u63a5\u5185\u5b58\u6ea2\u51fa<\/strong><\/span><\/div>\n

                      DirectMemory\u5bb9\u91cf\u53ef\u901a\u8fc7-XX: MaxDirectMemorySize\u6307\u5b9a\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u5219\u9ed8\u8ba4\u4e0eJava\u5806\u6700\u5927\u503c (-Xmx\u6307\u5b9a)\u4e00\u6837\u3002<\/p>\n

                      \r\npublic class DirectMemoryOOM { \r\n    private static final int _1MB = 1024 * 1024; \r\n    public static void main(String[] args) throws Exception { \r\n        Field unsafeField = Unsafe.class.getDeclaredFields()[0]; \r\n        unsafeField.setAccessible(true); \r\n        Unsafe unsafe = (Unsafe) unsafeField.get(null); \r\n        while (true) { \r\n            unsafe.allocateMemory(_1MB); \r\n        } \r\n    } \r\n}\r\n<\/pre>\n

                      \u4e0a\u9762\u4ecb\u7ecd\u4e86\u51e0\u4e2a\u5b9e\u4f8b\uff0c\u90a3\u9047\u5230\u8fd9\u79cd\u95ee\u9898\u5982\u4f55\u6392\u67e5\u5462?<\/p>\n

                      \u4e09\u3001\u5185\u5b58\u6ea2\u51fa\u6392\u67e5<\/strong><\/div>\n

                      \u6392\u67e5\u5176\u5b9e\u6700\u4e3b\u8981\u7684\u5c31\u662f\u68c0\u67e5\u4ee3\u7801\uff0c\u800c\u4e14\u5185\u5b58\u6ea2\u51fa\u5f80\u5f80\u90fd\u662f\u4ee3\u7801\u7684\u95ee\u9898\u3002\u5f53\u7136\u4e00\u4e0b\u51e0\u70b9\u90fd\u662f\u9700\u8981\u6ce8\u610f\u7684\uff1a<\/p>\n

                      (1)\u5185\u5b58\u4e2d\u52a0\u8f7d\u7684\u6570\u636e\u91cf\u8fc7\u4e8e\u5e9e\u5927\uff0c\u5982\u4e00\u6b21\u4ece\u6570\u636e\u5e93\u53d6\u51fa\u8fc7\u591a\u6570\u636e;<\/p>\n

                      (2)\u96c6\u5408\u7c7b\u4e2d\u6709\u5bf9\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u4f7f\u7528\u5b8c\u540e\u672a\u6e05\u7a7a\uff0c\u4f7f\u5f97JVM\u4e0d\u80fd\u56de\u6536;<\/p>\n

                      (3)\u4ee3\u7801\u4e2d\u5b58\u5728\u6b7b\u5faa\u73af\u6216\u5faa\u73af\u4ea7\u751f\u8fc7\u591a\u91cd\u590d\u7684\u5bf9\u8c61\u5b9e\u4f53;<\/p>\n

                      (4)\u4f7f\u7528\u7684\u7b2c\u4e09\u65b9\u8f6f\u4ef6\u4e2d\u7684BUG;<\/p>\n

                      (5)\u542f\u52a8\u53c2\u6570\u5185\u5b58\u503c\u8bbe\u5b9a\u7684\u8fc7\u5c0f;<\/p>\n

                      \u6700\u540e\u5c31\u662f\u89e3\u51b3\u4e86\u3002<\/p>\n

                      \u7b2c\u4e00\u6b65\uff0c\u4fee\u6539JVM\u542f\u52a8\u53c2\u6570\uff0c\u76f4\u63a5\u589e\u52a0\u5185\u5b58\u3002<\/p>\n

                      \u7b2c\u4e8c\u6b65\uff0c\u68c0\u67e5\u9519\u8bef\u65e5\u5fd7<\/p>\n

                      \u7b2c\u4e09\u6b65\uff0c\u5bf9\u4ee3\u7801\u8fdb\u884c\u8d70\u67e5\u548c\u5206\u6790\uff0c\u627e\u51fa\u53ef\u80fd\u53d1\u751f\u5185\u5b58\u6ea2\u51fa\u7684\u4f4d\u7f6e\u3002<\/p>\n

                      \u4e00\u822c\u60c5\u51b5\u4e0b\u4ee3\u7801\u51fa\u9519\u7684\u6982\u7387\u4f1a\u6bd4\u8f83\u5927\u4e00\u4e9b\uff0c\u5f53\u7136\u4e86\u4e0d\u540c\u7684\u573a\u666f\u4e0d\u540c\u9519\u8bef\u603b\u662f\u590d\u6742\u591a\u6837\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

                      \u5185\u5b58\u6ea2\u51fa\u5c31\u662f\u5185\u5b58\u4e0d\u591f\uff0c\u5f15\u8d77\u5185\u5b58\u6ea2\u51fa\u7684\u539f\u56e0\u6709\u5f88\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a 1\u3001\u5185\u5b58\u4e2d\u52a0\u8f7d\u7684\u6570\u636e\u91cf\u8fc7\u4e8e\u5e9e\u5927\uff0c\u5982\u4e00\u6b21\u4ece […]<\/p>\n","protected":false},"author":1329,"featured_media":213305,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-213303","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/213303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=213303"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/213303\/revisions"}],"predecessor-version":[{"id":213306,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/213303\/revisions\/213306"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/213305"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=213303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=213303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=213303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}