{"id":3357,"date":"2018-11-14T13:50:53","date_gmt":"2018-11-14T05:50:53","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3357"},"modified":"2018-11-14T13:50:53","modified_gmt":"2018-11-14T05:50:53","slug":"java-lang-outofmemoryerror-metaspace","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3357","title":{"rendered":"java.lang.OutOfMemoryError: Metaspace"},"content":{"rendered":"<p>\n\t\u6700\u8fd1\uff0c\u6211\u5728\u63a8\u5e7f\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\u3002\u6211\u53d1\u73b0\u5f53\u6211\u8ba4\u8ba4\u771f\u771f\u5199\u6280\u672f\u6587\u7ae0\u7684\u65f6\u5019\uff0c\u770b\u7684\u4eba\u5e76\u4e0d\u591a\u3002\u5f53\u6211\u5199\u70ed\u70b9\u4e8b\u4ef6\uff0c\u6216\u8005\u79d1\u6280\u65b0\u95fb\u540e\u53d1\u73b0\u9605\u8bfb\u91cf\u53c8\u5927\u5e45\u7684\u63d0\u5347\u3002\u4e8e\u662f\uff0c\u6211\u5c31\u5f97\u51fa\u4e00\u4e2a\u7ed3\u8bba\uff1a\u6280\u672f\u8d8a\u6df1\u7684\u6587\u7ae0\u770b\u7684\u4eba\u8d8a\u6df1\uff1b\u6280\u672f\u4e2d\u7b49\u7684\u6b21\u4e4b\uff1b\u6280\u672f\u504f\u521d\u7ea7\u7684\u770b\u7684\u4eba\u6700\u591a\uff01\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f\u539f\u56e0\u5f88\u7b80\u5355\u3002\u8fd9\u5c31\u548c\u4e70\u623f\u5b50\u4e00\u6837\u554a\uff0c\u666e\u901a\u5546\u54c1\u623f\u4e70\u7684\u4eba\u6700\u591a\uff0c\u590d\u5f0f\u5957\u623f\u8981\u5c11\u4e00\u70b9\uff0c\u522b\u5885\u518d\u5c11\u4e00\u70b9\u3002\u5199\u7684\u8d8a\u6df1\u7684\u6587\u7ae0\uff0c\u4e13\u5bb6\u7684\u6587\u7ae0\u5c31\u76f8\u5f53\u4e8e\u522b\u5885\u554a\uff01\u4f60\u8bf4\u662f\u4e0d\u662f\uff1f\n<\/p>\n<p>\n\t\u597d\u4e86\uff0c\u8a00\u5f52\u6b63\u4f20\uff0c\u6211\u4eec\u4eca\u5929\u6765\u8bf4\u8bf4 java.lang.OutOfMemoryError: Metaspace \u9519\u8bef\u3002\n<\/p>\n<p>\n\t\u6ca1\u770b\u8fc7\u524d\u9762\u6587\u7ae0\u7684\u7f51\u53cb\uff0c\u53ef\u4ee5\u770b\u770b\u6211\u7684\u6df1\u5165\u7406\u89e3 JVM&nbsp;OutOfMemoryError \u7cfb\u5217\u6587\u7ae0\uff1a\n<\/p>\n<ul>\n<li>\n\t\t<a href=\"https:\/\/www.xttblog.com\/?p=3345\">\u4e3a\u4ec0\u4e48\u4f1a\u4ea7\u751f java.lang.OutOfMemoryError: Java heap space \u9519\u8bef\u4ee5\u53ca\u5982\u4f55\u89e3\u51b3\uff1f<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/www.xttblog.com\/?p=3347\">\u8be6\u89e3 java.lang.OutOfMemoryError: GC overhead limit exceeded \u9519\u8bef\uff01<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/www.xttblog.com\/?p=3351\">\u8be6\u89e3 java.lang.OutOfMemoryError: Permgen space \u9519\u8bef\uff01<\/a>\n\t<\/li>\n<\/ul>\n<p>\n\tMetaspace \u8fd9\u4e2a\u4e1c\u897f\u662f\u5728 JDK8 \u540e\u624d\u6709\u7684\u3002Java 8 \u5f7b\u5e95\u5c06\u6c38\u4e45\u4ee3 (PermGen) \u79fb\u9664\u51fa\u4e86 HotSpot JVM\uff0c\u5c06\u5176\u539f\u6709\u7684\u6570\u636e\u8fc1\u79fb\u81f3 Java Heap \u6216 Metaspace\u3002\n<\/p>\n<p>\n\tPermGen \u6c38\u4e45\u4ee3\u4e2d\u7528\u4e8e\u5b58\u653e\u7c7b\u548c\u65b9\u6cd5\u7684\u5143\u6570\u636e\u4ee5\u53ca\u5e38\u91cf\u6c60\uff0c\u6bd4\u5982 Class \u548c Method \u7b49\u3002\u6c38\u4e45\u4ee3\u662f\u6709\u5927\u5c0f\u9650\u5236\u7684\uff0c\u56e0\u6b64\u5982\u679c\u52a0\u8f7d\u7684\u7c7b\u592a\u591a\uff0c\u5f88\u6709\u53ef\u80fd\u5bfc\u81f4\u6c38\u4e45\u4ee3\u5185\u5b58\u6ea2\u51fa\uff0c\u5373\u4e07\u6076\u7684 <a href=\"http:\/\/www.xttblog.com\/?p=3351\" target=\"_blank\">java.lang.OutOfMemoryError: PermGen<\/a> \uff0c\u4e3a\u6b64\u6211\u4eec\u4e0d\u5f97\u4e0d\u5bf9\u865a\u62df\u673a\u505a\u8c03\u4f18\u3002\n<\/p>\n<p>\n\t\u6240\u4ee5 java 8 \u4e2d PermGen \u6700\u7ec8\u88ab\u79fb\u9664\uff0c\u65b9\u6cd5\u533a\u79fb\u81f3 Metaspace\uff0c\u5b57\u7b26\u4e32\u5e38\u91cf\u79fb\u81f3 Java Heap\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"java8 \u4e2d\u5806\u5185\u5b58\u5212\u5206\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/java8-metaspace.png\" style=\"width: 710px;height: 245px\" \/>\n<\/p>\n<p>\n\tMetaspace \u7684\u5927\u5c0f\u503c, \u7531 -Xmx \u548c -XX:MaxMetaspaceSize \u7b49 JVM \u542f\u52a8\u53c2\u6570\u6307\u5b9a. \u5982\u679c\u6ca1\u6709\u660e\u786e\u6307\u5b9a, \u5219\u6839\u636e\u5e73\u53f0\u7c7b\u578b(OS \u7248\u672c + JVM \u7248\u672c)\u548c\u7269\u7406\u5185\u5b58\u7684\u5927\u5c0f\u6765\u786e\u5b9a\u3002\n<\/p>\n<p>\n\tjava.lang.OutOfMemoryError: Metaspace \u9519\u8bef\u6240\u8868\u8fbe\u7684\u4fe1\u606f\u5c31\u662f\u5143\u6570\u636e\u533a(Metaspace) \u5df2\u88ab\u7528\u6ee1\u3002\n<\/p>\n<p>\n\t\u7531\u4e8e\u65b9\u6cd5\u533a\u88ab\u79fb\u81f3 Metaspace\uff0c\u6240\u4ee5 Metaspace \u7684\u4f7f\u7528\u91cf\u4e0e JVM \u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u7684 class \u6570\u91cf\/\u5927\u5c0f\u6709\u5173\u3002\u53ef\u4ee5\u660e\u786e\u7684\u8bf4 java.lang.OutOfMemoryError: Metaspace \u9519\u8bef\u7684\u4e3b\u8981\u539f\u56e0, \u662f\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u7684 class \u6570\u91cf\u592a\u591a\u6216\u8005\u4f53\u79ef\u592a\u5927\u3002\n<\/p>\n<p>\n\t\u4e0b\u9762\u6211\u4eec\u518d\u4f7f\u7528 javassist \u5de5\u5177\u52a0\u8f7d\u4e00\u4e9b class \u5230 Metaspace \u4e2d\uff0c\u6f14\u793a\u4e00\u4e2a&nbsp;java.lang.OutOfMemoryError: Metaspace \u9519\u8bef\uff01\n<\/p>\n<pre class=\"brush:java;\">\r\npublic class Metaspace {\r\n  static javassist.ClassPool cp = javassist.ClassPool.getDefault();\r\n  \/\/ \u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n  public static void main(String[] args) throws Exception{\r\n    for (int i = 0; ; i++) { \r\n      Class c = cp.makeClass(&quot;com.xttblog.demo.Generated&quot; + i).toClass();\r\n    }\r\n  }\r\n}<\/pre>\n<p>\n\t\u6267\u884c\u8fd9\u6bb5\u4ee3\u7801, \u968f\u7740\u751f\u6210\u7684 class \u8d8a\u6765\u8d8a\u591a, \u6700\u540e\u5c06\u4f1a\u5360\u6ee1 Metaspace \u7a7a\u95f4, \u629b\u51fa java.lang.OutOfMemoryError: Metaspace. \u5728 Mac OS X\u4e0a, Java 1.8.0_05 \u73af\u5883\u4e0b, \u5982\u679c\u8bbe\u7f6e\u4e86\u542f\u52a8\u53c2\u6570 -XX:MaxMetaspaceSize=64m, \u5927\u7ea6\u52a0\u8f7d 70000 \u4e2a class \u540e JVM \u5c31\u4f1a\u6302\u6389\u3002\n<\/p>\n<p>\n\t\u5982\u679c\u629b\u51fa\u4e0e Metaspace \u6709\u5173\u7684 OutOfMemoryError\uff0c\u7b2c\u4e00\u89e3\u51b3\u65b9\u6848\u662f\u589e\u52a0 Metaspace \u7684\u5927\u5c0f\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\n-XX:MaxMetaspaceSize=512m<\/pre>\n<p>\n\t\u6709\u4e00\u79cd\u770b\u8d77\u6765\u5f88\u7b80\u5355\u7684\u65b9\u6848\uff0c\u662f\u76f4\u63a5\u53bb\u6389 Metaspace \u7684\u5927\u5c0f\u9650\u5236\u3002\u4f46\u9700\u8981\u6ce8\u610f\uff0c\u4e0d\u9650\u5236 Metaspace \u5185\u5b58\u7684\u5927\u5c0f\uff0c\u5047\u82e5\u7269\u7406\u5185\u5b58\u4e0d\u8db3\uff0c\u6709\u53ef\u80fd\u4f1a\u5f15\u8d77\u5185\u5b58\u4ea4\u6362(swapping)\uff0c\u4e25\u91cd\u62d6\u7d2f\u7cfb\u7edf\u6027\u80fd\u3002 \u6b64\u5916\uff0c\u8fd8\u53ef\u80fd\u9020\u6210 native \u5185\u5b58\u5206\u914d\u5931\u8d25\u7b49\u95ee\u9898\u3002\n<\/p>\n<p>\n\t\u9664\u6b64\u4e4b\u5916\uff0cVisualVM\u3001jstat\u3001jstack \u90fd\u53ef\u4ee5\u76d1\u6d4b Metaspace \u7684\u52a8\u6001\u3002\n<\/p>\n<p>\n\t\u4e0e Metaspace \u76f8\u5173\u7684\u8fd8\u6709 4 \u65b0\u589e\u7684 JVM \u53c2\u6570\uff1a\n<\/p>\n<ul>\n<li>\n\t\t-XX:MetaspaceSize \u662f\u5206\u914d\u7ed9\u7c7b\u5143\u6570\u636e\u7a7a\u95f4\uff08\u4ee5\u5b57\u8282\u8ba1\uff09\u7684\u521d\u59cb\u5927\u5c0f(Oracle\u903b\u8f91\u5b58\u50a8\u4e0a\u7684\u521d\u59cb\u9ad8\u6c34\u4f4d\uff0cthe initial high-water-mark )\uff0c\u6b64\u503c\u4e3a\u4f30\u8ba1\u503c\u3002MetaspaceSize\u7684\u503c\u8bbe\u7f6e\u7684\u8fc7\u5927\u4f1a\u5ef6\u957f\u5783\u573e\u56de\u6536\u65f6\u95f4\u3002\u5783\u573e\u56de\u6536\u8fc7\u540e\uff0c\u5f15\u8d77\u4e0b\u4e00\u6b21\u5783\u573e\u56de\u6536\u7684\u7c7b\u5143\u6570\u636e\u7a7a\u95f4\u7684\u5927\u5c0f\u53ef\u80fd\u4f1a\u53d8\u5927\u3002\n\t<\/li>\n<li>\n\t\t-XX:MaxMetaspaceSize \u662f\u5206\u914d\u7ed9\u7c7b\u5143\u6570\u636e\u7a7a\u95f4\u7684\u6700\u5927\u503c\uff0c\u8d85\u8fc7\u6b64\u503c\u5c31\u4f1a\u89e6\u53d1Full GC\uff0c\u6b64\u503c\u9ed8\u8ba4\u6ca1\u6709\u9650\u5236\uff0c\u4f46\u5e94\u53d6\u51b3\u4e8e\u7cfb\u7edf\u5185\u5b58\u7684\u5927\u5c0f\u3002JVM\u4f1a\u52a8\u6001\u5730\u6539\u53d8\u6b64\u503c\u3002\n\t<\/li>\n<li>\n\t\t-XX:MinMetaspaceFreeRatio \u8868\u793a\u4e00\u6b21GC\u4ee5\u540e\uff0c\u4e3a\u4e86\u907f\u514d\u589e\u52a0\u5143\u6570\u636e\u7a7a\u95f4\u7684\u5927\u5c0f\uff0c\u7a7a\u95f2\u7684\u7c7b\u5143\u6570\u636e\u7684\u5bb9\u91cf\u7684\u6700\u5c0f\u6bd4\u4f8b\uff0c\u4e0d\u591f\u5c31\u4f1a\u5bfc\u81f4\u5783\u573e\u56de\u6536\u3002\n\t<\/li>\n<li>\n\t\t-XX:MaxMetaspaceFreeRatio \u8868\u793a\u4e00\u6b21GC\u4ee5\u540e\uff0c\u4e3a\u4e86\u907f\u514d\u589e\u52a0\u5143\u6570\u636e\u7a7a\u95f4\u7684\u5927\u5c0f\uff0c\u7a7a\u95f2\u7684\u7c7b\u5143\u6570\u636e\u7684\u5bb9\u91cf\u7684\u6700\u5927\u6bd4\u4f8b\uff0c\u4e0d\u591f\u5c31\u4f1a\u5bfc\u81f4\u5783\u573e\u56de\u6536\u3002\n\t<\/li>\n<\/ul>\n<h2>\n\t\u53c2\u8003\u8d44\u6599<br \/>\n<\/h2>\n<ul>\n<li>\n\t\t<a href=\"https:\/\/plumbr.eu\/outofmemoryerror\/permgen-space\" rel=\"nofollow\" target=\"_blank\">https:\/\/plumbr.eu\/outofmemoryerror\/permgen-space<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/java-latte.blogspot.sg\/2014\/03\/metaspace-in-java-8.html\" rel=\"external\" target=\"_blank\">Metaspace in Java 8<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/stackoverflow.com\/questions\/24074164\/what-is-the-use-of-metaspace-in-java-8\" rel=\"external\" target=\"_blank\">What is the use of Metaspace in Java 8? &#8211; StackOverflow<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/blogs.oracle.com\/poonam\/entry\/about_g1_garbage_collector_permanent\" rel=\"external\" target=\"_blank\">About G1 Garbage Collector, Permanent Generation and Metaspace<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/openjdk.java.net\/jeps\/122\" rel=\"external\" target=\"_blank\">JEP 122: Remove the Permanent Generation<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/stackoverflow.com\/questions\/18339707\/permgen-elimination-in-jdk-8\/22509753#22509753\" rel=\"external\" target=\"_blank\">PermGen elimination in JDK 8 &#8211; StackOverflow<\/a>\n\t<\/li>\n<\/ul>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3357\">java.lang.OutOfMemoryError: Metaspace<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\uff0c\u6211\u5728\u63a8\u5e7f\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\u3002\u6211\u53d1\u73b0\u5f53\u6211\u8ba4\u8ba4\u771f\u771f\u5199\u6280\u672f\u6587\u7ae0\u7684\u65f6\u5019\uff0c\u770b\u7684\u4eba\u5e76\u4e0d\u591a\u3002\u5f53\u6211\u5199\u70ed\u70b9\u4e8b\u4ef6\uff0c\u6216 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,6],"tags":[1727,731,1750,601,263],"class_list":["post-3357","post","type-post","status-publish","format-standard","hentry","category-java","category-yyzt","tag-java8","tag-jvm","tag-metaspace","tag-outofmemoryerror","tag-substring"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3357"}],"version-history":[{"count":2,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3357\/revisions"}],"predecessor-version":[{"id":3359,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3357\/revisions\/3359"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}