{"id":10020984,"date":"2025-08-29T21:40:52","date_gmt":"2025-08-29T13:40:52","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020984"},"modified":"2025-08-29T21:40:52","modified_gmt":"2025-08-29T13:40:52","slug":"%e9%9d%a2%e8%af%95%e8%a2%ab%e9%97%ae%ef%bc%9aoom%e7%b1%bb%e5%9e%8b%e6%9c%89%e5%93%aa%e4%ba%9b%ef%bc%9f%e6%80%8e%e4%b9%88%e7%ad%94%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020984","title":{"rendered":"\u9762\u8bd5\u88ab\u95ee\uff1aOOM\u7c7b\u578b\u6709\u54ea\u4e9b\uff1f\u600e\u4e48\u7b54\uff1f"},"content":{"rendered":"<p><strong>\u5927\u5bb6\u597d\uff0c\u6211\u662f\u5c0f\u5bcc\uff5e<\/strong><\/p>\n<blockquote>\n<p>\u9762\u8bd5\u5b98\uff1aOOM\u7c7b\u578b\u6709\u54ea\u4e9b\uff1f<\/p>\n<p>\u4f60\uff1a\u5c31\u662f\u8001\u5e74\u4ee3\u653e\u4e0d\u4e0b\u4e86\u561b\uff01<\/p>\n<p>\u9762\u8bd5\u5b98\uff1a\u7b49\u6d88\u606f\u5427\uff01<\/p>\n<\/blockquote>\n<p><strong>OOM\uff08Out Of Memory\uff09<\/strong> \u9519\u8bef\u6709\u591a\u79cd\u7c7b\u578b\uff0c\u6bcf\u79cd\u7c7b\u578b\u5bf9\u5e94\u4e0d\u540c\u7684\u5185\u5b58\u533a\u57df\u6216\u89e6\u53d1\u573a\u666f\u3002\u4ee5\u4e0b\u662f\u5e38\u89c1\u7684 OOM \u7c7b\u578b\u53ca\u5176\u4ea7\u751f\u539f\u56e0\uff1a<\/p>\n<hr>\n<h3>1. <strong><code>java.lang.OutOfMemoryError: Java heap space<\/code><\/strong><\/h3>\n<ul>\n<li><strong>\u89e6\u53d1\u539f\u56e0<\/strong>\uff1a\u5806\u5185\u5b58\uff08\u5b58\u653e\u5bf9\u8c61\u5b9e\u4f8b\uff09\u4e0d\u8db3\uff0c\u65e0\u6cd5\u5206\u914d\u65b0\u5bf9\u8c61\u3002<\/li>\n<li>\n<p><strong>\u5178\u578b\u573a\u666f<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u5185\u5b58\u6cc4\u6f0f<\/strong>\uff1a\u5bf9\u8c61\u88ab\u65e0\u610f\u957f\u671f\u5f15\u7528\uff08\u5982\u9759\u6001\u96c6\u5408\u3001\u672a\u5173\u95ed\u7684\u8d44\u6e90\uff09\uff0c\u65e0\u6cd5\u88ab GC \u56de\u6536\u3002<\/li>\n<li><strong>\u5806\u5927\u5c0f\u4e0d\u8db3<\/strong>\uff1aJVM \u5806\u53c2\u6570\uff08<code>-Xmx<\/code>\uff09\u8bbe\u7f6e\u8fc7\u5c0f\uff0c\u6216\u7a0b\u5e8f\u9700\u8981\u5904\u7406\u7684\u6570\u636e\u91cf\u8d85\u51fa\u9884\u671f\u3002<\/li>\n<li><strong>\u5927\u5bf9\u8c61\u5206\u914d<\/strong>\uff1a\u4e00\u6b21\u6027\u7533\u8bf7\u8d85\u5927\u5bf9\u8c61\uff08\u5982\u5927\u6570\u7ec4\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"java\">\/\/ \u4e0d\u65ad\u5411\u96c6\u5408\u4e2d\u6dfb\u52a0\u5bf9\u8c61\u5bfc\u81f4\u5806\u6ea2\u51fa\nList&lt;Object&gt; list = new ArrayList&lt;&gt;();\nwhile (true) {\n    list.add(new Object());\n}<\/code><\/pre>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li>\u68c0\u67e5\u5185\u5b58\u6cc4\u6f0f\uff08\u4f7f\u7528 <code>jmap<\/code> + <code>MAT<\/code> \u5206\u6790\u5806\u8f6c\u50a8\uff09\u3002<\/li>\n<li>\u8c03\u6574\u5806\u5927\u5c0f\uff08<code>-Xmx<\/code> \u548c <code>-Xms<\/code>\uff09\u3002<\/li>\n<li>\u4f18\u5316\u4ee3\u7801\u903b\u8f91\uff0c\u51cf\u5c11\u5bf9\u8c61\u751f\u547d\u5468\u671f\u3002<\/li>\n<\/ul>\n<hr>\n<h3>2. <strong><code>java.lang.OutOfMemoryError: Metaspace<\/code>\uff08Java 8+\uff09\u6216 <code>PermGen space<\/code>\uff08Java 7-\uff09<\/strong><\/h3>\n<ul>\n<li><strong>\u89e6\u53d1\u539f\u56e0<\/strong>\uff1a\u5143\u7a7a\u95f4\uff08Metaspace\uff09\u6216\u6c38\u4e45\u4ee3\uff08PermGen\uff09\u5185\u5b58\u4e0d\u8db3\uff0c\u7528\u4e8e\u5b58\u50a8\u7c7b\u5143\u6570\u636e\u3001\u65b9\u6cd5\u4fe1\u606f\u7b49\u3002<\/li>\n<li>\n<p><strong>\u5178\u578b\u573a\u666f<\/strong>\uff1a<\/p>\n<ul>\n<li>\u52a8\u6001\u751f\u6210\u5927\u91cf\u7c7b\uff08\u5982\u4f7f\u7528 CGLib\u3001\u53cd\u5c04\u3001\u52a8\u6001\u4ee3\u7406\uff09\u3002<\/li>\n<li>\u7c7b\u52a0\u8f7d\u5668\u672a\u6b63\u786e\u91ca\u653e\uff08\u5982\u9891\u7e41\u90e8\u7f72\u7684 Web \u5e94\u7528\u5bfc\u81f4\u65e7\u7c7b\u672a\u5378\u8f7d\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"java\">\/\/ \u4f7f\u7528 CGLib \u52a8\u6001\u751f\u6210\u5927\u91cf\u4ee3\u7406\u7c7b\nEnhancer enhancer = new Enhancer();\nwhile (true) {\n    enhancer.setSuperclass(OOM.class);\n    enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -&gt; proxy.invokeSuper(obj, args)));\n    enhancer.create();\n}<\/code><\/pre>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li>\u8c03\u6574\u5143\u7a7a\u95f4\u5927\u5c0f\uff08<code>-XX:MaxMetaspaceSize<\/code>\uff09\u3002<\/li>\n<li>\u68c0\u67e5\u7c7b\u52a0\u8f7d\u5668\u6cc4\u6f0f\u6216\u52a8\u6001\u7c7b\u751f\u6210\u903b\u8f91\u3002<\/li>\n<\/ul>\n<hr>\n<h3>3. <strong><code>java.lang.OutOfMemoryError: Direct buffer memory<\/code><\/strong><\/h3>\n<ul>\n<li><strong>\u89e6\u53d1\u539f\u56e0<\/strong>\uff1a\u76f4\u63a5\u5185\u5b58\uff08Direct Memory\uff0c\u901a\u8fc7 <code>ByteBuffer.allocateDirect()<\/code> \u5206\u914d\uff09\u8017\u5c3d\u3002<\/li>\n<li>\n<p><strong>\u5178\u578b\u573a\u666f<\/strong>\uff1a<\/p>\n<ul>\n<li>\u9891\u7e41\u7533\u8bf7\u76f4\u63a5\u5185\u5b58\u4f46\u672a\u53ca\u65f6\u91ca\u653e\uff08\u9700\u4f9d\u8d56 <code>System.gc()<\/code> \u6216 <code>Cleaner<\/code> \u673a\u5236\uff09\u3002<\/li>\n<li>JVM \u76f4\u63a5\u5185\u5b58\u53c2\u6570\uff08<code>-XX:MaxDirectMemorySize<\/code>\uff09\u8bbe\u7f6e\u8fc7\u5c0f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"java\">\/\/ \u4e0d\u65ad\u7533\u8bf7\u76f4\u63a5\u5185\u5b58\nList&lt;ByteBuffer&gt; buffers = new ArrayList&lt;&gt;();\nwhile (true) {\n    buffers.add(ByteBuffer.allocateDirect(1024 * 1024)); \/\/ 1MB\n}<\/code><\/pre>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li>\u68c0\u67e5\u76f4\u63a5\u5185\u5b58\u4f7f\u7528\u4ee3\u7801\uff0c\u786e\u4fdd\u53ca\u65f6\u91ca\u653e\u3002<\/li>\n<li>\u8c03\u6574 <code>-XX:MaxDirectMemorySize<\/code>\u3002<\/li>\n<\/ul>\n<hr>\n<h3>4. <strong><code>java.lang.OutOfMemoryError: Unable to create new native thread<\/code><\/strong><\/h3>\n<ul>\n<li><strong>\u89e6\u53d1\u539f\u56e0<\/strong>\uff1a\u64cd\u4f5c\u7cfb\u7edf\u9650\u5236\u7ebf\u7a0b\u6570\u91cf\uff0c\u65e0\u6cd5\u521b\u5efa\u65b0\u7ebf\u7a0b\u3002<\/li>\n<li>\n<p><strong>\u5178\u578b\u573a\u666f<\/strong>\uff1a<\/p>\n<ul>\n<li>\u7ebf\u7a0b\u6570\u8d85\u8fc7\u7cfb\u7edf\u9650\u5236\uff08\u5982 Linux \u7684 <code>ulimit -u<\/code>\uff09\u3002<\/li>\n<li>\u6bcf\u4e2a\u7ebf\u7a0b\u7684\u6808\u5185\u5b58\uff08<code>-Xss<\/code>\uff09\u8bbe\u7f6e\u8fc7\u5927\uff0c\u5bfc\u81f4\u603b\u5185\u5b58\u5360\u7528\u8d85\u51fa\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"java\">\/\/ \u65e0\u9650\u521b\u5efa\u7ebf\u7a0b\nwhile (true) {\n    new Thread(() -&gt; {\n        try { Thread.sleep(1000000); } catch (InterruptedException e) {}\n    }).start();\n}<\/code><\/pre>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li>\u51cf\u5c11\u7ebf\u7a0b\u6570\uff08\u4f7f\u7528\u7ebf\u7a0b\u6c60\uff09\u3002<\/li>\n<li>\u8c03\u6574 <code>-Xss<\/code> \u51cf\u5c0f\u7ebf\u7a0b\u6808\u5927\u5c0f\u3002<\/li>\n<li>\u4fee\u6539\u7cfb\u7edf\u7ebf\u7a0b\u6570\u9650\u5236\u3002<\/li>\n<\/ul>\n<hr>\n<h3>5. <strong><code>java.lang.OutOfMemoryError: Requested array size exceeds VM limit<\/code><\/strong><\/h3>\n<ul>\n<li><strong>\u89e6\u53d1\u539f\u56e0<\/strong>\uff1a\u5c1d\u8bd5\u5206\u914d\u8d85\u8fc7 JVM \u9650\u5236\u7684\u6570\u7ec4\uff08\u901a\u5e38\u63a5\u8fd1 <code>Integer.MAX_VALUE<\/code>\uff09\u3002<\/li>\n<li><strong>\u5178\u578b\u573a\u666f<\/strong>\uff1a\u9519\u8bef\u8ba1\u7b97\u6570\u7ec4\u957f\u5ea6\uff0c\u5982 <code>new int[Integer.MAX_VALUE]<\/code>\u3002<\/li>\n<\/ul>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li>\u68c0\u67e5\u6570\u7ec4\u957f\u5ea6\u8ba1\u7b97\u903b\u8f91\uff0c\u4f7f\u7528\u5408\u7406\u7684\u6570\u636e\u7ed3\u6784\u3002<\/li>\n<\/ul>\n<hr>\n<h3>6. <strong><code>java.lang.OutOfMemoryError: GC Overhead limit exceeded<\/code><\/strong><\/h3>\n<ul>\n<li><strong>\u89e6\u53d1\u539f\u56e0<\/strong>\uff1aGC \u9891\u7e41\u6267\u884c\u4f46\u56de\u6536\u6548\u7387\u6781\u4f4e\uff08\u5982 98% \u65f6\u95f4\u7528\u4e8e GC\uff0c\u4ec5\u56de\u6536 2% \u5185\u5b58\uff09\u3002<\/li>\n<li><strong>\u5178\u578b\u573a\u666f<\/strong>\uff1a\u5806\u5185\u5b58\u51e0\u4e4e\u88ab\u5360\u6ee1\uff0c\u4e14\u5b58\u5728\u5927\u91cf\u65e0\u6cd5\u56de\u6536\u7684\u5bf9\u8c61\uff08\u5185\u5b58\u6cc4\u6f0f\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li>\u68c0\u67e5\u5185\u5b58\u6cc4\u6f0f\u6216\u4f18\u5316 GC \u7b56\u7565\uff08\u5982\u8c03\u6574\u5806\u5927\u5c0f\u3001\u66f4\u6362\u5783\u573e\u56de\u6536\u5668\uff09\u3002<\/li>\n<\/ul>\n<hr>\n<h3>7. <strong><code>java.lang.OutOfMemoryError: CodeCache<\/code>\uff08JIT \u7f16\u8bd1\u4ee3\u7801\u7f13\u5b58\u6ea2\u51fa\uff09<\/strong><\/h3>\n<ul>\n<li><strong>\u89e6\u53d1\u539f\u56e0<\/strong>\uff1aJIT \u7f16\u8bd1\u5668\u751f\u6210\u7684\u672c\u5730\u4ee3\u7801\u5360\u6ee1\u4ee3\u7801\u7f13\u5b58\u533a\u3002<\/li>\n<li><strong>\u5178\u578b\u573a\u666f<\/strong>\uff1a\u9ad8\u9891\u52a8\u6001\u7f16\u8bd1\u5927\u91cf\u65b9\u6cd5\uff08\u5982\u590d\u6742\u7684\u70ed\u70b9\u4ee3\u7801\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li>\u8c03\u6574\u4ee3\u7801\u7f13\u5b58\u5927\u5c0f\uff08<code>-XX:ReservedCodeCacheSize<\/code>\uff09\u3002<\/li>\n<li>\u5173\u95ed\u5206\u5c42\u7f16\u8bd1\uff08<code>-XX:-TieredCompilation<\/code>\uff09\u3002<\/li>\n<\/ul>\n<hr>\n<h2>\u603b\u7ed3<\/h2>\n<p>OOM \u7684\u6839\u672c\u539f\u56e0\u662f <strong>JVM \u5185\u5b58\u533a\u57df\u4e0d\u8db3<\/strong> \u6216 <strong>\u8d44\u6e90\u8017\u5c3d<\/strong>\uff0c\u9700\u7ed3\u5408\u9519\u8bef\u7c7b\u578b\u5206\u6790\u5177\u4f53\u5185\u5b58\u533a\u57df\uff08\u5806\u3001\u5143\u7a7a\u95f4\u3001\u76f4\u63a5\u5185\u5b58\u7b49\uff09\u3002<\/p>\n<p>\u6392\u67e5\u65f6\u53ef\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p>1\u3001\u786e\u5b9a OOM \u7c7b\u578b\uff08\u901a\u8fc7\u9519\u8bef\u65e5\u5fd7\uff09\u3002<\/p>\n<p>2\u3001\u4f7f\u7528\u5de5\u5177\u5206\u6790\uff08\u5982 <code>jstat<\/code>\u3001<code>jmap<\/code>\u3001<code>VisualVM<\/code>\u3001<code>MAT<\/code>\uff09\u3002<\/p>\n<p>3\u3001\u8c03\u6574 JVM \u53c2\u6570\u6216\u4f18\u5316\u4ee3\u7801\u903b\u8f91\u3002<\/p>\n<p><strong>\u7b49\u88ab\u88c1\u5458\u5728\u5b66\u4e60\uff1f\u8d76\u7d27\u7684\u5427\uff01<\/strong><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047224048\">https:\/\/segmentfault.com\/a\/1190000047224048<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6211\u662f\u5c0f\u5bcc\uff5e \u9762\u8bd5\u5b98\uff1aOOM\u7c7b\u578b\u6709\u54ea\u4e9b\uff1f \u4f60\uff1a\u5c31\u662f\u8001\u5e74\u4ee3\u653e\u4e0d\u4e0b\u4e86\u561b\uff01 \u9762\u8bd5\u5b98\uff1a\u7b49\u6d88\u606f\u5427\uff01 OOM\uff08Out [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020984","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020984","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020984"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020984\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}