{"id":64326,"date":"2025-05-03T14:02:36","date_gmt":"2025-05-03T06:02:36","guid":{"rendered":"https:\/\/fwq.ai\/blog\/64326\/"},"modified":"2025-05-03T14:02:36","modified_gmt":"2025-05-03T06:02:36","slug":"java%e5%ae%9a%e4%b9%89%e6%95%b0%e7%bb%84%e5%a4%aa%e5%a4%a7%e4%bc%9a%e6%8a%a5%e9%94%99%e6%80%8e%e4%b9%88%e8%a7%a3%e5%86%b3-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/64326\/","title":{"rendered":"java\u5b9a\u4e49\u6570\u7ec4\u592a\u5927\u4f1a\u62a5\u9519\u600e\u4e48\u89e3\u51b3"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u5b9a\u4e49\u5927\u6570\u7ec4\u65f6\u53ef\u80fd\u4ea7\u751f outofmemoryerror \u9519\u8bef\uff0c\u89e3\u51b3\u65b9\u6848\u5305\u62ec\uff1a1. \u4f7f\u7528\u96c6\u5408\u7c7b\uff1b2. \u4f7f\u7528\u5927\u5bb9\u91cf\u6570\u7ec4\u7c7b\uff1b3. \u4f18\u5316\u6570\u7ec4\u5927\u5c0f\uff1b4. \u4f7f\u7528\u5206\u9875\uff1b5. \u786e\u4fdd\u6709\u8db3\u591f\u7684\u5185\u5b58\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/03\/2024110305230393761.jpg\" class=\"aligncenter\" title=\"java\u5b9a\u4e49\u6570\u7ec4\u592a\u5927\u4f1a\u62a5\u9519\u600e\u4e48\u89e3\u51b3\u63d2\u56fe\" alt=\"java\u5b9a\u4e49\u6570\u7ec4\u592a\u5927\u4f1a\u62a5\u9519\u600e\u4e48\u89e3\u51b3\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u89e3\u51b3 Java \u5b9a\u4e49\u5927\u6570\u7ec4\u65f6\u4ea7\u751f\u7684\u9519\u8bef<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u6570\u7ec4\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u6570\u7ec4\u5143\u7d20\u7684\u7c7b\u578b\u548c\u957f\u5ea6\u3002\u4f46\u662f\uff0c\u5982\u679c\u6570\u7ec4\u957f\u5ea6\u8fc7\u5927\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4 OutOfMemoryError \u9519\u8bef\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u89e3\u51b3\u6b64\u9519\u8bef\u6709\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>1. \u4f7f\u7528\u96c6\u5408\u7c7b\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528\u96c6\u5408\u7c7b\uff08\u5982 ArrayList\u3001LinkedList \u6216 HashSet\uff09\u53ef\u4ee5\u52a8\u6001\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\uff0c\u907f\u514d OutOfMemoryError \u9519\u8bef\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>List&lt;Integer&gt; numbers = new ArrayList&lt;&gt;();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528\u5927\u5bb9\u91cf\u6570\u7ec4\u7c7b\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 java.util.concurrent.ArrayBlockingQueue \u6216 java.util.concurrent.LinkedBlockingQueue \u7b49\u5927\u5bb9\u91cf\u6570\u7ec4\u7c7b\uff0c\u5b83\u4eec\u4e13\u95e8\u8bbe\u8ba1\u7528\u4e8e\u5904\u7406\u5927\u6570\u7ec4\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>ArrayBlockingQueue&lt;Integer&gt; numbers = new ArrayBlockingQueue&lt;&gt;(1000000);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f18\u5316\u6570\u7ec4\u5927\u5c0f\uff1a<\/strong><\/p>\n<p>\u5982\u679c\u53ef\u80fd\uff0c\u4f18\u5316\u6570\u7ec4\u5927\u5c0f\u4ee5\u51cf\u5c11\u5185\u5b58\u6d88\u8017\u3002\u53ef\u4ee5\u6839\u636e\u9700\u8981\u52a8\u6001\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\uff0c\u6216\u8005\u4f7f\u7528\u66f4\u5c0f\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>int[] numbers = new int[1000]; \/\/ \u521d\u59cb\u5927\u5c0f\u4e3a 1000\n...\nif (numbers.length &lt; 2000) {\n    numbers = Arrays.copyOf(numbers, 2000);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528\u5206\u9875\uff1a<\/strong><\/p>\n<p>\u5c06\u5927\u6570\u7ec4\u5212\u5206\u4e3a\u8f83\u5c0f\u7684\u5206\u9875\uff0c\u5e76\u5728\u9700\u8981\u65f6\u52a0\u8f7d\u6bcf\u4e2a\u5206\u9875\u3002\u8fd9\u53ef\u4ee5\u51cf\u5c11\u4e00\u6b21\u6027\u5206\u914d\u7684\u5185\u5b58\u91cf\uff0c\u4ece\u800c\u907f\u514d OutOfMemoryError \u9519\u8bef\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u521b\u5efa\u4e00\u4e2a\u5305\u542b 100 \u4e07\u4e2a\u5143\u7d20\u7684\u6570\u7ec4\nInteger[] numbers = new Integer[1000000];\n\n\/\/ \u6bcf\u6b21\u4ec5\u52a0\u8f7d 100 \u4e2a\u5143\u7d20\nint pageSize = 100;\nfor (int i = 0; i &lt; numbers.length; i += pageSize) {\n    \/\/ \u52a0\u8f7d\u5f53\u524d\u5206\u9875\n    Integer[] page = Arrays.copyOfRange(numbers, i, Math.min(i + pageSize, numbers.length));\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u786e\u4fdd\u6709\u8db3\u591f\u7684\u5185\u5b58\uff1a<\/strong><\/p>\n<p>\u786e\u4fdd Java \u865a\u62df\u673a (JVM) \u5177\u6709\u8db3\u591f\u7684\u5185\u5b58\u5206\u914d\u7ed9\u6570\u7ec4\u3002\u53ef\u4ee5\u4f7f\u7528 -Xmx \u53c2\u6570\u5728 JVM \u542f\u52a8\u65f6\u589e\u52a0\u6700\u5927\u5806\u5927\u5c0f\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>java -Xmx4g MyProgram<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u5b9a\u4e49\u6570\u7ec4\u592a\u5927\u4f1a\u62a5\u9519\u600e\u4e48\u89e3\u51b3\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java \u4e2d\u5b9a\u4e49\u5927\u6570\u7ec4\u65f6\u53ef\u80fd\u4ea7\u751f outofmemoryerror \u9519\u8bef\uff0c\u89e3\u51b3\u65b9\u6848\u5305\u62ec\uff1a1. \u4f7f\u7528\u96c6\u5408\u7c7b\uff1b2. \u4f7f\u7528\u5927\u5bb9\u91cf\u6570\u7ec4\u7c7b\uff1b3. \u4f18\u5316\u6570\u7ec4\u5927\u5c0f\uff1b4. \u4f7f\u7528\u5206\u9875\uff1b5. \u786e\u4fdd\u6709\u8db3\u591f\u7684\u5185\u5b58\u3002 \u5982\u4f55\u89e3\u51b3 Java \u5b9a\u4e49\u5927\u6570\u7ec4\u65f6\u4ea7\u751f\u7684\u9519\u8bef \u5728 Java \u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u6570\u7ec4\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u6570\u7ec4\u5143\u7d20\u7684\u7c7b\u578b\u548c\u957f\u5ea6\u3002\u4f46\u662f\uff0c\u5982\u679c\u6570\u7ec4\u957f\u5ea6\u8fc7\u5927\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4 OutOfMemoryError \u9519\u8bef\u3002 \u89e3\u51b3\u65b9\u6848\uff1a \u89e3\u51b3\u6b64\u9519\u8bef\u6709\u4ee5\u4e0b\u65b9\u6cd5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 1. \u4f7f\u7528\u96c6\u5408\u7c7b\uff1a \u4f7f\u7528\u96c6\u5408\u7c7b\uff08\u5982 ArrayList\u3001LinkedList \u6216 HashSet\uff09\u53ef\u4ee5\u52a8\u6001\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\uff0c\u907f\u514d OutOfMemoryError \u9519\u8bef\u3002 \u793a\u4f8b\uff1a List&lt;Integer&gt; numbers = new ArrayList&lt;&gt;(); \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528\u5927\u5bb9\u91cf\u6570\u7ec4\u7c7b\uff1a \u4f7f\u7528 java.util.concurrent.ArrayBlockingQueue \u6216 java.util.concurrent.LinkedBlockingQueue \u7b49\u5927\u5bb9\u91cf\u6570\u7ec4\u7c7b\uff0c\u5b83\u4eec\u4e13\u95e8\u8bbe\u8ba1\u7528\u4e8e\u5904\u7406\u5927\u6570\u7ec4\u3002 \u793a\u4f8b\uff1a ArrayBlockingQueue&lt;Integer&gt; numbers = new ArrayBlockingQueue&lt;&gt;(1000000); \u767b\u5f55\u540e\u590d\u5236 3. \u4f18\u5316\u6570\u7ec4\u5927\u5c0f\uff1a \u5982\u679c\u53ef\u80fd\uff0c\u4f18\u5316\u6570\u7ec4\u5927\u5c0f\u4ee5\u51cf\u5c11\u5185\u5b58\u6d88\u8017\u3002\u53ef\u4ee5\u6839\u636e\u9700\u8981\u52a8\u6001\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\uff0c\u6216\u8005\u4f7f\u7528\u66f4\u5c0f\u7684\u6570\u636e\u7c7b\u578b\u3002 \u793a\u4f8b\uff1a int[] numbers [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-64326","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=64326"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64326\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=64326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=64326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=64326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}