JVM1.8相关

1.JVM1.8在新版本的改进更新

  1.1对比:

    JDK 1.7 及以往的 JDK 版本中,Java 类信息、常量池、静态变量都存储在 Perm(永久代)里。类的元数据和静态变量在类加载的时候分配到 Perm,当类被卸载的时候垃圾收集器从 Perm 处理掉类的元数据和静态变量。当然常量池的东西也会在 Perm 垃圾收集的时候进行处理。

    JDK 1.8 的对 JVM 架构的改造将类元数据放到本地内存中,另外,将常量池和静态变量放到 Java 堆里。HotSopt VM 将会为类的元数据明确分配和释放本地内存。在这种架构下,类元信息就突破了原来 -XX:MaxPermSize 的限制,现在可以使用更多的本地内存。这样就从一定程度上解决了原来在运行时生成大量类的造成经常 Full GC 问题,如运行时使用反射、代理等。

  1.2注意:

    如果服务器内存足够,升级到 JDK 1.8 修改 JVM 参数最简单的办法就是将 -XX:PermSize 和 -XX:MaxPermSize 参数替换为 -XX:MetaspaceSize 和 -XX:MaxMetaspaceSize

    1.8中-XX:PermSize 和 -XX:MaxPermSize 已经失效,取而代之的是一个新的区域 —— Metaspace(元数据区)。

    使用JDK1.8以及之后的版本,不会再碰上“java.lang.OutOfMemoryError: PermGen space”这个错误了。

  1.3优势理解:

    permSize:原来的jar包及你自己项目的class存放的内存空间,这部分空间是固定的,启动参数里面-permSize确定,如果你的jar包很多,经常会遇到permSize溢出,且每个项目都会占用自己的permGen空间
    改成metaSpaces,各个项目会共享同样的class内存空间,比如两个项目都用了fast-json开源包,在mentaSpaces里面只存一份class,提高内存利用率,且更利于垃圾回收

   1.4区别

    元空间并不在虚拟机中,而是使用本地内存。因此,默认情况下,元空间的大小仅受本地内存限制

   1.5参数来指定元空间的大小   

    -XX:MetaspaceSize,初始空间大小,达到该值就会触发垃圾收集进行类型卸载,同时GC会对该值进行调整:如果释放了大量的空间,就适当降低该值;如果释放了很少的空间,那么在不超过MaxMetaspaceSize时,适当提高该值。
    -XX:MaxMetaspaceSize,最大空间,默认是没有限制的。

    除了上面两个指定大小的选项以外,还有两个与 GC 相关的属性:
    -XX:MinMetaspaceFreeRatio,在GC之后,最小的Metaspace剩余空间容量的百分比,减少为分配空间所导致的垃圾收集
    -XX:MaxMetaspaceFreeRatio,在GC之后,最大的Metaspace剩余空间容量的百分比,减少为释放空间所导致的垃圾收集

 

 

 

2.GC机制中,JVM对内存的分区

【需要说明:元数据区的大小限定于本地内存的大小,Eden、Survivor、Old区都是JVM管理下的堆中分配的内存区域】

 

JDK安装目录下 bin下面jvisualvm.exe  安装Visual GC插件,选择工具---插件

 

 

  2.1 GC是什么

    GC(GarbageCollection)是垃圾回收机制,在java中开发人员无法使用指针来自由的管理内存,GC是JVM对内存(实际上就是对象)进行管理的方式。GC使得Java开发人员摆脱了繁琐的内存管理工作,让程序的开发更有效率。

  2.2 GC的工作原理

    自己的话:对于程序员来说,分配对象使用new关键字;释放对象时,只要将对象所有引用赋值为null,让程序不能够再访问到这个对象,我们称该对象为"不可达的"或者"不被引用"。GC将负责回收所有"不可达"对象的内存空间。

    上图中

      1》新new的对象都放在Eden区

      2》Eden区满或者快满的时候进行一次清理,不被引用的对象直接被干掉;还有引用的对象,但是年龄比较大的,挪到S0区

      3》下次Eden区快满的时候,会进行上一步的操作,并且将Eden和S0区的年纪大的对象放到S1区【原理上随时保持S0和S1有一个是空的,用来存下一次的对象】

      4》下下次,Eden区快满的时候,会进行上一步操作,并且将Eden和S1区的年纪大的对象放到S0区【此时S1区就是空的】

      5》直到Eden区快满,S0或者S1也快满的时候,这时候就把这两个区的年纪大的对象放到Old区

      6》依次循环,直到Old区也快满的时候,Eden区也快满的时候,会对整个这一块内存区域进行一次大清洗,腾出内存,为之后的对象创建,程序运行腾地方。

  2.3 Minor GC、Major GC和Full GC

    清理Eden区和 Survivor区叫Minor GC

    清理Old区叫Major GC

    清理整个堆空间—包括年轻代和老年代叫Full GC

    参考:http://www.importnew.com/15820.html

 

jvm1.8 参数 在生产环境中  一般 

-XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=256m

 

 -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC


-Xmx1024m (堆最大大小) 
-Xms1024m (堆默认大小) 
-Xmn256m (新生代大小) 
-Xss256k (栈最大深度大小) 
-XX:SurvivorRatio=8 (新生代分区比例 8:2) 
-XX:+UseConcMarkSweepGC (指定使用的垃圾收集器,这里使用CMS收集器) 
-XX:+PrintGCDetails (打印详细的GC日志)

-XX:+UseG1GC (指定使用的垃圾收集器,这里使用G1收集器,建议生产环境使用) 

-XX:MetaspaceSize=256m的含义到底是什么呢?其实,这个JVM参数是指Metaspace扩容时触发FullGC的初始化阈值,也是最小的阈值。这里有几个要点需要明确:

  1. 无论-XX:MetaspaceSize配置什么值,Metaspace的初始容量一定是21807104(约20.8m);

  2. Metaspace由于使用不断扩容到-XX:MetaspaceSize参数指定的量,就会发生FGC;且之后每次Metaspace扩容都会发生FGC;

  3. 如果Old区配置CMS垃圾回收,那么第2点的FGC也会使用CMS算法进行回收;

  4. Meta区容量范围为[20.8m, MaxMetaspaceSize);

  5. 如果MaxMetaspaceSize设置太小,可能会导致频繁FGC,甚至OOM;

任何一个JVM参数的默认值可以通过java -XX:+PrintFlagsFinal -version |grep JVMParamName获取,例如:java -XX:+PrintFlagsFinal -version |grep MetaspaceSize

验证

笔者的环境,服务启动后,MU的值稳定在55296k,那么设置-XX:MetaspaceSize=50m -XX:MaxMetaspaceSize=256m,按照上面的推理,会发生一次CMS GC,事实也确实如此,部分gc日志如下所示:

 

[GC (CMS Initial Mark) [1 CMS-initial-mark: 13272K(524288K)] 253317K(996160K), 0.0566338 secs] [Times: user=0.13 sys=0.00, real=0.06 secs] 
[CMS-concurrent-mark-start]
[CMS-concurrent-mark: 0.033/0.033 secs] [Times: user=0.03 sys=0.00, real=0.03 secs] 
[CMS-concurrent-preclean-start]
[CMS-concurrent-preclean: 0.002/0.003 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-abortable-preclean-start]
 CMS: abort preclean due to time [CMS-concurrent-abortable-preclean: 2.904/5.006 secs] [Times: user=2.92 sys=0.00, real=5.01 secs] 
[GC (CMS Final Remark) [YG occupancy: 240743 K (471872 K)][Rescan (parallel) , 0.0676438 secs][weak refs processing, 0.0000916 secs][class unloading, 0.0085156 secs][scrub symbol table, 0.0139570 secs][scrub string table, 0.0007734 secs][1 CMS-remark: 13272K(524288K)] 254016K(996160K), 0.0922369 secs] [Times: user=0.24 sys=0.00, real=0.09 secs] 
[CMS-concurrent-sweep-start]
[CMS-concurrent-sweep: 0.006/0.006 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[CMS-concurrent-reset-start]
[CMS-concurrent-reset: 0.002/0.002 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 

 

通过 [GC (CMS Initial Mark) [1 CMS-initial-mark: 58004K(131072K)] 70447K(249088K), 0.0055264 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]这行日志可知:Old区还远远达不到70%(-XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70)触发CMS GC的条件。所以,这次CMS GC是Metaspace区扩容达到-XX:MetaspaceSize=50m触发的。

建议

  1. MetaspaceSizeMaxMetaspaceSize设置一样大;

  2. 具体设置多大,建议稳定运行一段时间后通过jstat -gc pid确认且这个值大一些,对于大部分项目256m即可。

验证

当 MaxMetaspaceSize 设置太小,频繁FGC,导致甚至OOM;

部分日志如下:

[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10996K->10998K(524288K), 0.0597620 secs] 21650K->10998K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0598754 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10998K->10995K(524288K), 0.0369201 secs] 10998K->10995K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0370169 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10995K->10995K(524288K), 0.0364850 secs] 15189K->10995K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0365840 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10995K->10995K(524288K), 0.0373423 secs] 10995K->10995K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0374434 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10995K(524288K)] 15190K(996160K), 0.0006551 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.018/0.018 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10995K->10996K(524288K), 0.0575554 secs] 15190K->10996K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0576558 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10996K->10993K(524288K), 0.0388822 secs] 10996K->10993K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0389807 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10993K->10993K(524288K), 0.0367893 secs] 15187K->10993K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0368886 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10993K->10993K(524288K), 0.0369320 secs] 10993K->10993K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0370305 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10993K(524288K)] 15188K(996160K), 0.0005274 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.016/0.016 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10993K->10995K(524288K), 0.0568867 secs] 15188K->10995K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0570195 secs] [Times: user=0.05 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10995K->10993K(524288K), 0.0409507 secs] 10995K->10993K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0410535 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10993K->11001K(524288K), 0.0435053 secs] 21647K->11001K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0436147 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 11001K->10995K(524288K), 0.0375241 secs] 11001K->10995K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0376231 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10995K(524288K)] 15190K(996160K), 0.0004919 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10995K->10995K(524288K), 0.0535647 secs] 21649K->10995K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0536974 secs] [Times: user=0.06 sys=0.00, real=0.05 secs] 
[Full GC (Last ditch collection) [CMS: 10995K->10994K(524288K), 0.0373442 secs] 10995K->10994K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0374459 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10994K->10995K(524288K), 0.0400787 secs] 15189K->10995K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0402923 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10995K->10995K(524288K), 0.0415953 secs] 10995K->10995K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0417313 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10995K(524288K)] 15189K(996160K), 0.0004332 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10995K->10997K(524288K), 0.0540166 secs] 15189K->10997K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0541175 secs] [Times: user=0.05 sys=0.00, real=0.05 secs] 
[Full GC (Last ditch collection) [CMS: 10997K->10997K(524288K), 0.0371319 secs] 10997K->10997K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0372279 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10997K->10997K(524288K), 0.0382360 secs] 15191K->10997K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0383428 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10997K->10997K(524288K), 0.0397804 secs] 10997K->10997K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0398800 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10997K->10996K(524288K), 0.0415289 secs] 15193K->10996K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0416360 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10996K->10996K(524288K), 0.0377221 secs] 10996K->10996K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0378574 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10996K(524288K)] 10996K(996160K), 0.0004777 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10996K->10996K(524288K), 0.0533219 secs] 10996K->10996K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0534206 secs] [Times: user=0.05 sys=0.00, real=0.05 secs] 
[Full GC (Last ditch collection) [CMS: 10996K->10996K(524288K), 0.0374463 secs] 10996K->10996K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0375502 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10996K->11015K(524288K), 0.0421514 secs] 21650K->11015K(996160K), [Metaspace: 29472K->29472K(1075200K)], 0.0422935 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 11015K->11005K(524288K), 0.0417410 secs] 11015K->11005K(996160K), [Metaspace: 29472K->29472K(1075200K)], 0.0419002 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 11005K(524288K)] 15199K(996160K), 0.0005729 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.018 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 11005K->10999K(524288K), 0.0553269 secs] 15199K->10999K(996160K), [Metaspace: 29472K->29472K(1075200K)], 0.0554282 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10999K->10998K(524288K), 0.0392870 secs] 10999K->10998K(996160K), [Metaspace: 29472K->29472K(1075200K)], 0.0393887 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10998K->10998K(524288K), 0.0397466 secs] 15192K->10998K(996160K), [Metaspace: 29468K->29468K(1075200K)], 0.0401416 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10998K->10998K(524288K), 0.0433778 secs] 10998K->10998K(996160K), [Metaspace: 29468K->29468K(1075200K)], 0.0436206 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10998K(524288K)] 10998K(996160K), 0.0006964 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.018 secs] [Times: user=0.05 sys=0.02, real=0.02 secs] 
 (concurrent mode failure): 10998K->10997K(524288K), 0.0596584 secs] 10998K->10997K(996160K), [Metaspace: 29468K->29468K(1075200K)], 0.0597722 secs] [Times: user=0.05 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10997K->10997K(524288K), 0.0367200 secs] 10997K->10997K(996160K), [Metaspace: 29468K->29468K(1075200K)], 0.0368175 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10997K->10999K(524288K), 0.0404981 secs] 21651K->10999K(996160K), [Metaspace: 29468K->29468K(1075200K)], 0.0405977 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10999K->10998K(524288K), 0.0382429 secs] 10999K->10998K(996160K), [Metaspace: 29468K->29468K(1075200K)], 0.0383780 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10998K(524288K)] 15192K(996160K), 0.0007988 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.022/0.022 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10998K->11007K(524288K), 0.0662522 secs] 15192K->11007K(996160K), [Metaspace: 29472K->29472K(1075200K)], 0.0663690 secs] [Times: user=0.06 sys=0.00, real=0.07 secs] 
[Full GC (Last ditch collection) [CMS: 11007K->10964K(524288K), 0.0407716 secs] 11007K->10964K(996160K), [Metaspace: 29472K->29472K(1075200K)], 0.0408863 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10964K->10962K(524288K), 0.0375811 secs] 15159K->10962K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0376826 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10962K->10962K(524288K), 0.0361406 secs] 10962K->10962K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0362370 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10962K(524288K)] 10962K(996160K), 0.0004835 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10962K->10962K(524288K), 0.0581142 secs] 10962K->10962K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0582166 secs] [Times: user=0.05 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10962K->10962K(524288K), 0.0419855 secs] 10962K->10962K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0421280 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10962K->10962K(524288K), 0.0387063 secs] 10962K->10962K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0388109 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10962K->10962K(524288K), 0.0371432 secs] 10962K->10962K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0372394 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10962K(524288K)] 17421K(996160K), 0.0005023 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.016/0.016 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10962K->10969K(524288K), 0.0586031 secs] 17421K->10969K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0587152 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10969K->10968K(524288K), 0.0402176 secs] 10969K->10968K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0403834 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10968K->10964K(524288K), 0.0395990 secs] 10968K->10964K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0397337 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10964K->10964K(524288K), 0.0375395 secs] 10964K->10964K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0376404 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10964K(524288K)] 15158K(996160K), 0.0004721 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.03 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10964K->10963K(524288K), 0.0567881 secs] 15158K->10963K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0568911 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10963K->10963K(524288K), 0.0377466 secs] 10963K->10963K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0378478 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10963K->10963K(524288K), 0.0416776 secs] 10964K->10963K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0417782 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10963K->10963K(524288K), 0.0394801 secs] 10963K->10963K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0396151 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10963K(524288K)] 15159K(996160K), 0.0007194 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10963K->10966K(524288K), 0.0550673 secs] 21619K->10966K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0551686 secs] [Times: user=0.05 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10966K->10966K(524288K), 0.0372895 secs] 10966K->10966K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0373936 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10966K->10966K(524288K), 0.0377889 secs] 15161K->10966K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0378945 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10966K->10966K(524288K), 0.0409924 secs] 10966K->10966K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0410907 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10966K(524288K)] 15161K(996160K), 0.0005863 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.018/0.019 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10966K->10968K(524288K), 0.0582391 secs] 15161K->10968K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0583396 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10968K->10968K(524288K), 0.0375418 secs] 10968K->10968K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0376384 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10968K->10968K(524288K), 0.0370213 secs] 15163K->10968K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0371185 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10968K->10968K(524288K), 0.0366392 secs] 10968K->10968K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0367340 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10968K(524288K)] 15163K(996160K), 0.0004850 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10968K->10968K(524288K), 0.0575500 secs] 15163K->10968K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0576588 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10968K->10968K(524288K), 0.0395850 secs] 10968K->10968K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0396838 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10968K->10986K(524288K), 0.0383948 secs] 21622K->10986K(996160K), [Metaspace: 29442K->29442K(1075200K)], 0.0385036 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10986K->10977K(524288K), 0.0377620 secs] 10986K->10977K(996160K), [Metaspace: 29442K->29442K(1075200K)], 0.0378644 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10977K(524288K)] 15172K(996160K), 0.0005240 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10977K->10973K(524288K), 0.0555831 secs] 15172K->10973K(996160K), [Metaspace: 29442K->29442K(1075200K)], 0.0556830 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10973K->10973K(524288K), 0.0402066 secs] 10973K->10973K(996160K), [Metaspace: 29442K->29442K(1075200K)], 0.0403364 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10973K->10974K(524288K), 0.0398905 secs] 15168K->10974K(996160K), [Metaspace: 29442K->29442K(1075200K)], 0.0399919 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10974K->10974K(524288K), 0.0369311 secs] 10974K->10974K(996160K), [Metaspace: 29442K->29442K(1075200K)], 0.0370515 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10974K(524288K)] 10974K(996160K), 0.0004963 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10974K->10973K(524288K), 0.0531260 secs] 15169K->10973K(996160K), [Metaspace: 29442K->29442K(1075200K)], 0.0532296 secs] [Times: user=0.06 sys=0.00, real=0.05 secs] 
[Full GC (Last ditch collection) [CMS: 10973K->10973K(524288K), 0.0376419 secs] 10973K->10973K(996160K), [Metaspace: 29442K->29442K(1075200K)], 0.0377432 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10973K->10974K(524288K), 0.0406882 secs] 21627K->10974K(996160K), [Metaspace: 29445K->29445K(1075200K)], 0.0407966 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10974K->10974K(524288K), 0.0392890 secs] 10974K->10974K(996160K), [Metaspace: 29445K->29445K(1075200K)], 0.0393872 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10974K(524288K)] 10974K(996160K), 0.0004978 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.018/0.018 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10974K->10973K(524288K), 0.0580606 secs] 15169K->10973K(996160K), [Metaspace: 29446K->29446K(1075200K)], 0.0581835 secs] [Times: user=0.05 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10973K->10973K(524288K), 0.0393296 secs] 10973K->10973K(996160K), [Metaspace: 29446K->29446K(1075200K)], 0.0394352 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10973K->10973K(524288K), 0.0377485 secs] 15167K->10973K(996160K), [Metaspace: 29446K->29446K(1075200K)], 0.0378468 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10973K->10973K(524288K), 0.0426285 secs] 10973K->10973K(996160K), [Metaspace: 29446K->29446K(1075200K)], 0.0427650 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10973K(524288K)] 10973K(996160K), 0.0006518 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.018/0.018 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10973K->10985K(524288K), 0.0552724 secs] 21627K->10985K(996160K), [Metaspace: 29446K->29446K(1075200K)], 0.0553784 secs] [Times: user=0.05 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10985K->10978K(524288K), 0.0373473 secs] 10985K->10978K(996160K), [Metaspace: 29446K->29446K(1075200K)], 0.0374443 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10978K->10976K(524288K), 0.0364243 secs] 15173K->10976K(996160K), [Metaspace: 29446K->29446K(1075200K)], 0.0365242 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10976K->10976K(524288K), 0.0381757 secs] 10976K->10976K(996160K), [Metaspace: 29446K->29446K(1075200K)], 0.0382755 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10976K(524288K)] 15171K(996160K), 0.0005042 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10976K->10976K(524288K), 0.0571292 secs] 15171K->10976K(996160K), [Metaspace: 29448K->29448K(1075200K)], 0.0572279 secs] [Times: user=0.05 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10976K->10976K(524288K), 0.0410987 secs] 10976K->10976K(996160K), [Metaspace: 29448K->29448K(1075200K)], 0.0411986 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10976K->10976K(524288K), 0.0370404 secs] 21630K->10976K(996160K), [Metaspace: 29449K->29449K(1075200K)], 0.0371431 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10976K->10976K(524288K), 0.0362079 secs] 10976K->10976K(996160K), [Metaspace: 29449K->29449K(1075200K)], 0.0363024 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10976K(524288K)] 15171K(996160K), 0.0005074 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10976K->10975K(524288K), 0.0568865 secs] 15171K->10975K(996160K), [Metaspace: 29449K->29449K(1075200K)], 0.0569955 secs] [Times: user=0.05 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10975K->10975K(524288K), 0.0433910 secs] 10975K->10975K(996160K), [Metaspace: 29449K->29449K(1075200K)], 0.0435500 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10975K->10976K(524288K), 0.0405072 secs] 15169K->10976K(996160K), [Metaspace: 29449K->29449K(1075200K)], 0.0406177 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10976K->10976K(524288K), 0.0372435 secs] 10976K->10976K(996160K), [Metaspace: 29449K->29449K(1075200K)], 0.0373406 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10976K(524288K)] 21630K(996160K), 0.0006310 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10976K->11016K(524288K), 0.0575538 secs] 21630K->11016K(996160K), [Metaspace: 29453K->29453K(1075200K)], 0.0576634 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 11016K->10995K(524288K), 0.0368434 secs] 11016K->10995K(996160K), [Metaspace: 29453K->29453K(1075200K)], 0.0369395 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10995K->10979K(524288K), 0.0402371 secs] 15190K->10979K(996160K), [Metaspace: 29453K->29453K(1075200K)], 0.0403805 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10979K->10973K(524288K), 0.0396410 secs] 10979K->10973K(996160K), [Metaspace: 29453K->29453K(1075200K)], 0.0397870 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10973K(524288K)] 10973K(996160K), 0.0005205 secs] [Times: user=0.06 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10973K->10973K(524288K), 0.0550588 secs] 10973K->10973K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0551630 secs] [Times: user=0.05 sys=0.00, real=0.05 secs] 
[Full GC (Last ditch collection) [CMS: 10973K->10973K(524288K), 0.0369433 secs] 10973K->10973K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0370398 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10973K->10966K(524288K), 0.0408875 secs] 10973K->10966K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0409917 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10966K->10966K(524288K), 0.0408376 secs] 10966K->10966K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0410226 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10966K(524288K)] 10966K(996160K), 0.0009718 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10966K->10968K(524288K), 0.0570590 secs] 15161K->10968K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0571933 secs] [Times: user=0.05 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10968K->10968K(524288K), 0.0377433 secs] 10968K->10968K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0378456 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
Exception in thread "main" java.lang.OutOfMemoryError: Metaspace
[Full GC (Metadata GC Threshold) [CMS: 10968K->7605K(524288K), 0.0339590 secs] 46790K->7605K(996160K), [Metaspace: 29443K->29443K(1075200K)], 0.0340657 secs] [Times: user=0.03 sys=0.00, real=0.03 secs] 
[Full GC (Last ditch collection) [CMS: 7605K->7604K(524288K), 0.0299186 secs] 7605K->7604K(996160K), [Metaspace: 29443K->29443K(1075200K)], 0.0300518 secs] [Times: user=0.03 sys=0.00, real=0.03 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 7604K(524288K)] 7604K(996160K), 0.0005979 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
Heap
 par new generation   total 471872K, used 8390K [0x0000000080000000, 0x00000000a0000000, 0x00000000a0000000)
  eden space 419456K,   2% used [0x0000000080000000, 0x00000000808319d0, 0x00000000999a0000)
  from space 52416K,   0% used [0x000000009ccd0000, 0x000000009ccd0000, 0x00000000a0000000)
  to   space 52416K,   0% used [0x00000000999a0000, 0x00000000999a0000, 0x000000009ccd0000)
 concurrent mark-sweep generation total 524288K, used 7604K [0x00000000a0000000, 0x00000000c0000000, 0x0000000100000000)
 Metaspace       used 29446K, capacity 30324K, committed 30720K, reserved 1075200K
  class space    used 4003K, capacity 4224K, committed 4352K, reserved 1048576K

 

验证string常量存在堆内存中,demo如下

  /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) throws Exception {
        try {
            List<String> list = new ArrayList<String>();
            for (int i = 0; ; i++) {
                System.out.println(i);
                list.add(""+i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }



    }

 

JVM参数如下

-XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=256m -Xms50m -Xmx50m -Xmn20m -Xss256k -XX:SurvivorRatio=8 -XX:+UseG1GC  -XX:+PrintGCDetails 

运行结果如下

[GC pause (G1 Humongous Allocation) (young), 0.0004699 secs]
   [Parallel Time: 0.2 ms, GC Workers: 4]
      [GC Worker Start (ms): Min: 14630.5, Avg: 14630.5, Max: 14630.6, Diff: 0.0]
      [Ext Root Scanning (ms): Min: 0.1, Avg: 0.1, Max: 0.1, Diff: 0.0, Sum: 0.5]
      [Update RS (ms): Min: 0.0, Avg: 0.0, Max: 0.1, Diff: 0.0, Sum: 0.2]
         [Processed Buffers: Min: 1, Avg: 1.5, Max: 2, Diff: 1, Sum: 6]
      [Scan RS (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
      [Code Root Scanning (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
      [Object Copy (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
      [Termination (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
         [Termination Attempts: Min: 1, Avg: 1.0, Max: 1, Diff: 0, Sum: 4]
      [GC Worker Other (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
      [GC Worker Total (ms): Min: 0.2, Avg: 0.2, Max: 0.2, Diff: 0.0, Sum: 0.8]
      [GC Worker End (ms): Min: 14630.7, Avg: 14630.7, Max: 14630.7, Diff: 0.0]
   [Code Root Fixup: 0.0 ms]
   [Code Root Purge: 0.0 ms]
   [Clear CT: 0.1 ms]
   [Other: 0.2 ms]
      [Choose CSet: 0.0 ms]
      [Ref Proc: 0.1 ms]
      [Ref Enq: 0.0 ms]
      [Redirty Cards: 0.1 ms]
      [Humongous Register: 0.0 ms]
      [Humongous Reclaim: 0.0 ms]
      [Free CSet: 0.0 ms]
   [Eden: 0.0B(20.0M)->0.0B(20.0M) Survivors: 0.0B->0.0B Heap: 48.2M(50.0M)->48.2M(50.0M)]
 [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure)  48M->47M(50M), 0.1027376 secs]
   [Eden: 0.0B(20.0M)->0.0B(20.0M) Survivors: 0.0B->0.0B Heap: 48.2M(50.0M)->47.5M(50.0M)], [Metaspace: 3914K->3914K(1056768K)]
 [Times: user=0.17 sys=0.00, real=0.10 secs] 
[Full GC (Allocation Failure)  47M->47M(50M), 0.0880405 secs]
   [Eden: 0.0B(20.0M)->0.0B(20.0M) Survivors: 0.0B->0.0B Heap: 47.5M(50.0M)->47.5M(50.0M)], [Metaspace: 3914K->3914K(1056768K)]
 [Times: user=0.14 sys=0.00, real=0.09 secs] 
[GC concurrent-mark-abort]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOf(Arrays.java:3210)
	at java.util.Arrays.copyOf(Arrays.java:3181)
	at java.util.ArrayList.grow(ArrayList.java:265)
	at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:239)
	at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:231)
	at java.util.ArrayList.add(ArrayList.java:462)
	at com.zsj.tool.util.DateUtil.main(DateUtil.java:524)
Heap
 garbage-first heap   total 51200K, used 48676K [0x00000000fce00000, 0x00000000fcf00190, 0x0000000100000000)
  region size 1024K, 1 young (1024K), 0 survivors (0K)
 Metaspace       used 3944K, capacity 4600K, committed 4864K, reserved 1056768K
  class space    used 419K, capacity 428K, committed 512K, reserved 1048576K

Process finished with exit code 1

可以看到   

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space,堆内存溢出。

 

元数据区
元数据区OOM测试:

//借助cglib框架生成新类。

demo

package com.zsj.tool.util;

import org.springframework.cglib.proxy.CallbackFilter;
import org.springframework.cglib.proxy.Dispatcher;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;

import java.lang.management.ClassLoadingMXBean;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Method;

/**
 * @Description TODO
 * @Date 2019/4/19 9:25
 * @Author zsj
 */
public class MetaSpaceOomMock {
   //借助cglib框架生成新类。
    public static void main(String[] args) {
        ClassLoadingMXBean loadingBean = ManagementFactory.getClassLoadingMXBean();
        while (true) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(MetaSpaceOomMock.class);
            enhancer.setCallbackTypes(new Class[]{Dispatcher.class, MethodInterceptor.class});
            enhancer.setCallbackFilter(new CallbackFilter() {
                @Override
                public int accept(Method method) {
                    return 1;
                }

                @Override
                public boolean equals(Object obj) {
                    return super.equals(obj);
                }
            });

            Class clazz = enhancer.createClass();
            System.out.println(clazz.getName());
            //显示数量信息(共加载过的类型数目,当前还有效的类型数目,已经被卸载的类型数目)
            System.out.println("total: " + loadingBean.getTotalLoadedClassCount());
            System.out.println("active: " + loadingBean.getLoadedClassCount());
            System.out.println("unloaded: " + loadingBean.getUnloadedClassCount());
        }
    }

}

jvm参数

-XX:MetaspaceSize=200m -XX:MaxMetaspaceSize=200m -Xms50m -Xmx50m -Xmn20m -Xss256k -XX:SurvivorRatio=8 -XX:+UseG1GC  -XX:+PrintGCDetails 

运行结果

Exception in thread "main" java.lang.IllegalStateException: Unable to load cache item
	at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:79)
	at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)
	at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116)
	at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291)
	at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:480)
	at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:337)
	at com.zsj.tool.util.MetaSpaceOomMock.main(MetaSpaceOomMock.java:37)
Caused by: java.lang.OutOfMemoryError: Metaspace
	at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:345)
	at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:492)
	at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:93)
	at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91)
	at org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61)
	... 6 more
Heap
 garbage-first heap   total 51200K, used 49464K [0x00000000fce00000, 0x00000000fcf00190, 0x0000000100000000)
  region size 1024K, 2 young (2048K), 0 survivors (0K)
 Metaspace       used 204322K, capacity 204600K, committed 204800K, reserved 1239040K
  class space    used 16052K, capacity 16141K, committed 16256K, reserved 1048576K

Process finished with exit code 1

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章