{"id":10091,"date":"2023-11-28T21:26:16","date_gmt":"2023-11-28T13:26:16","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=10091"},"modified":"2023-12-07T10:49:33","modified_gmt":"2023-12-07T02:49:33","slug":"java-destackoverflow-cuo-wupermgen-space-cuo-wu-zh","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/java-destackoverflow-cuo-wupermgen-space-cuo-wu-zh\/","title":{"rendered":"Java\u7684stackoverflow\u9519\u8bef\uff0cpermgen space\u9519\u8bef\u4e3b\u8981\u51fa\u73b0\u5728\u4ec0\u4e48\u60c5\u51b5\uff1f"},"content":{"rendered":"<p>\u5728Java 6\u53ca\u4e4b\u524d\u7684\u7248\u672c\u4e2d\uff0c<code>java.lang.StackOverflowError<\/code> \u548c <code>OutOfMemoryError: PermGen space<\/code> \u9519\u8bef\u4e3b\u8981\u51fa\u73b0\u5728\u4ee5\u4e0b\u60c5\u51b5\uff1a<\/p>\n<h3><a id=\"1%E9%80%92%E5%BD%92%E8%B0%83%E7%94%A8%E5%AF%BC%E8%87%B4%E6%A0%88%E6%BA%A2%E5%87%BA%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>1. \u9012\u5f52\u8c03\u7528\u5bfc\u81f4\u6808\u6ea2\u51fa\uff1a<\/h3>\n<p>\u5f53\u4e00\u4e2a\u65b9\u6cd5\u4e0d\u65ad\u5730\u9012\u5f52\u8c03\u7528\uff0c\u800c\u9012\u5f52\u7684\u6df1\u5ea6\u8fc7\u5927\uff0c\u6808\u7a7a\u95f4\u88ab\u8017\u5c3d\uff0c\u5c31\u4f1a\u629b\u51fa <code>java.lang.StackOverflowError<\/code>\u3002<\/p>\n<pre><code class=\"language-java\">public class StackOverflowExample {\n    public static void main(String[] args) {\n        recursiveMethod(0);\n    }\n\n    private static void recursiveMethod(int count) {\n        System.out.println(&quot;Count: &quot; + count);\n        recursiveMethod(count + 1);\n    }\n}\n<\/code><\/pre>\n<h3><a id=\"2%E5%A4%A7%E9%87%8F%E7%B1%BB%E7%9A%84%E5%8A%A0%E8%BD%BD%E5%AF%BC%E8%87%B4%E6%B0%B8%E4%B9%85%E4%BB%A3%E6%BA%A2%E5%87%BA%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>2. \u5927\u91cf\u7c7b\u7684\u52a0\u8f7d\u5bfc\u81f4\u6c38\u4e45\u4ee3\u6ea2\u51fa\uff1a<\/h3>\n<p>\u5728Java 6\u53ca\u4e4b\u524d\u7684\u7248\u672c\u4e2d\uff0c\u7c7b\u7684\u5143\u6570\u636e\uff08\u5143\u4fe1\u606f\uff09\u5b58\u50a8\u5728\u6c38\u4e45\u4ee3\uff08Permanent Generation\uff09\u4e2d\u3002\u5f53\u5e94\u7528\u7a0b\u5e8f\u52a8\u6001\u751f\u6210\u5927\u91cf\u7684\u7c7b\uff08\u4f8b\u5982\u4f7f\u7528CGLIB\u6216\u52a8\u6001\u4ee3\u7406\u65f6\uff09\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6c38\u4e45\u4ee3\u6ea2\u51fa\u3002<\/p>\n<pre><code class=\"language-java\">import net.sf.cglib.proxy.Enhancer;\n\npublic class PermGenErrorExample {\n    public static void main(String[] args) {\n        while (true) {\n            Enhancer enhancer = new Enhancer();\n            enhancer.setSuperclass(PermGenErrorExample.class);\n            enhancer.setUseCache(false);\n            enhancer.setCallback((obj, method, arg, proxy) -&gt; proxy.invokeSuper(obj, arg));\n            enhancer.create();\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u793a\u4f8b\u4e2d\u4f7f\u7528\u4e86CGLIB\u52a8\u6001\u751f\u6210\u7c7b\uff0c\u5bfc\u81f4\u4e0d\u65ad\u4ea7\u751f\u65b0\u7684\u7c7b\u5e76\u52a0\u8f7d\u5230\u6c38\u4e45\u4ee3\u4e2d\uff0c\u6700\u7ec8\u5bfc\u81f4\u6c38\u4e45\u4ee3\u6ea2\u51fa\u3002<\/p>\n<h3><a id=\"%E5%A6%82%E4%BD%95%E8%A7%A3%E5%86%B3%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5982\u4f55\u89e3\u51b3\uff1a<\/h3>\n<ol>\n<li>\n<strong>\u6808\u6ea2\u51fa\u9519\u8bef\uff1a<\/strong><\/p>\n<ul>\n<li>\u9012\u5f52\u6df1\u5ea6\u8fc7\u6df1\u65f6\uff0c\u9700\u8981\u4f18\u5316\u7b97\u6cd5\uff0c\u51cf\u5c11\u9012\u5f52\u6df1\u5ea6\uff0c\u6216\u8005\u4f7f\u7528\u8fed\u4ee3\u66ff\u4ee3\u9012\u5f52\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>\u6c38\u4e45\u4ee3\u6ea2\u51fa\u9519\u8bef\uff1a<\/strong><\/p>\n<ul>\n<li>\u5347\u7ea7\u81f3Java 7\u53ca\u4ee5\u4e0a\u7248\u672c\uff0c\u56e0\u4e3a\u5728Java 7\u4e2d\u79fb\u9664\u4e86\u6c38\u4e45\u4ee3\uff0c\u53d6\u800c\u4ee3\u4e4b\u7684\u662f\u4f7f\u7528\u5143\u7a7a\u95f4\uff08Metaspace\uff09\u3002<\/li>\n<li>\u8c03\u6574JVM\u53c2\u6570\uff0c\u589e\u5927\u6c38\u4e45\u4ee3\u7684\u5927\u5c0f\uff0c\u4f8b\u5982\u4f7f\u7528 <code>-XX:MaxPermSize<\/code> \u6765\u589e\u5927\u6c38\u4e45\u4ee3\u7684\u6700\u5927\u5927\u5c0f\u3002\u4f46\u8fd9\u5728Java 8\u53ca\u4ee5\u4e0a\u7248\u672c\u5df2\u7ecf\u4e0d\u518d\u751f\u6548\uff0c\u56e0\u4e3a\u6c38\u4e45\u4ee3\u5df2\u88ab\u5143\u7a7a\u95f4\u66ff\u4ee3\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528Java 8\u53ca\u4ee5\u4e0a\u7248\u672c\uff0c\u907f\u514d\u6c38\u4e45\u4ee3\u6ea2\u51fa\u95ee\u9898\u3002\u5982\u679c\u4f7f\u7528\u8f83\u8001\u7684\u7248\u672c\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8c03\u6574JVM\u53c2\u6570\u4ee5\u53ca\u4f18\u5316\u5e94\u7528\u7a0b\u5e8f\u4ee3\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Java 6\u53ca\u4e4b\u524d\u7684\u7248\u672c\u4e2d\uff0cjava.lang.StackOverflowError \u548c OutOfMemo [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[119],"tags":[],"class_list":["post-10091","post","type-post","status-publish","format-standard","hentry","category-jvm"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10091","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=10091"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10091\/revisions"}],"predecessor-version":[{"id":16798,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10091\/revisions\/16798"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=10091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=10091"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=10091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}