JDK8垃圾回收长时间存活对象回收GC日志分析【第五篇】

mac2025-05-17  20

 

package com.jvm.jvmdemo.test; /** * @author :miaoqs * @date :2019-10-31 23:13 * @description:TODO -XX:MaxTenuringThreshold=3 只适用于串行收集器 */ public class GCLongLifeTest { /** * 1M容量 */ private static int _1MB = 2 * 1024 * 1024; public static void main(String[] args) { byte[] allocation1, allocation2, allocation3, allocation4; // 注意,先设置一个较小的值,否则可能因为系统的一些数据,导致从幸存区移动到了老年区,就看不到效果了. allocation1 = new byte[_1MB / 100]; allocation2 = new byte[4 * _1MB]; allocation3 = new byte[4 * _1MB];// 申请 Eden 空间不足,触发 GC. allocation3 = null; allocation3 = new byte[4 * _1MB];// 申请 Eden 空间不足,触发 GC. allocation4 = new byte[4 * _1MB];// 申请 Eden 空间不足,触发 GC. } /** * * TODO VM options 参数设置 * -Xms40M 堆的大小 * -Xmx40M 堆的最大大小 * -Xmn20M 新生代大小 * -XX:+PrintGCDetails * -XX:SurvivorRatio=16 伊甸园的大小 * -XX:MaxTenuringThreshold=5 最大GC几次会将幸存区的写入老年代 * -XX:+UseSerialGC 串行垃圾收集器 * [GC (Allocation Failure) [DefNew: 11521K->773K(19392K), 0.0059804 secs] 11521K->8965K(39872K), 0.0060080 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] * [GC (Allocation Failure) [DefNew: 17688K->24K(19392K), 0.0057849 secs] 25880K->16997K(39872K), 0.0058077 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] * Heap * TODO 新生代内存变化 新生代伊甸园区由4M from区有_1MB / 100 * def new generation total 19392K, used 8400K [0x00000007bd800000, 0x00000007bec00000, 0x00000007bec00000) * eden space 18304K, 45% used [0x00000007bd800000, 0x00000007be02ddb0, 0x00000007be9e0000) * from space 1088K, 2% used [0x00000007be9e0000, 0x00000007be9e63a0, 0x00000007beaf0000) * to space 1088K, 0% used [0x00000007beaf0000, 0x00000007beaf0000, 0x00000007bec00000) * TODO 年老代内存变化 有4M * tenured generation total 20480K, used 16972K [0x00000007bec00000, 0x00000007c0000000, 0x00000007c0000000) * the space 20480K, 82% used [0x00000007bec00000, 0x00000007bfc932e0, 0x00000007bfc93400, 0x00000007c0000000) * TODO 元空间内存变化 * Metaspace used 3382K, capacity 4496K, committed 4864K, reserved 1056768K * class space used 372K, capacity 388K, committed 512K, reserved 1048576K * * TODO 注意 * 有可能还没到达MaxTenuringThreshold最大值幸存区的对象呗晋升到老年代了 * 解决办法:动态判断. * 为了能更好的适应不同程序的内存状况,虚拟机并不是永远要求达到年龄才能晋升老年代. * 比如 Survivor 空间不足,比如在 Survivor 空间中相同年龄所有对象大小超过了 Survivor 空间的一半,那么年龄大于或等于该年龄的对象就可以直接进入老年代. * 而在上面的案例中,我们设置的 allocation1 + 系统的一些数据,超过了 survivor 空间的一半,所以通过动态的计算,直接将幸存区 的数据复制到了老年代. */ /** * TODO VM options 参数设置 * -Xms40M * -Xmx40M * -Xmn20M * -XX:+PrintGCDetails * -XX:SurvivorRatio=16 * -XX:MaxTenuringThreshold=1 GC一次幸存区的对象就被移到年老代了 * -XX:+UseSerialGC * [GC (Allocation Failure) [DefNew: 11521K->770K(19392K), 0.0060188 secs] 11521K->8962K(39872K), 0.0060490 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] * [GC (Allocation Failure) [DefNew: 17686K->1K(19392K), 0.0049933 secs] 25878K->16973K(39872K), 0.0050149 secs] [Times: user=0.00 sys=0.01, real=0.01 secs] * Heap * def new generation total 19392K, used 8497K [0x00000007bd800000, 0x00000007bec00000, 0x00000007bec00000) * eden space 18304K, 46% used [0x00000007bd800000, 0x00000007be04c0d8, 0x00000007be9e0000) * from space 1088K, 0% used [0x00000007be9e0000, 0x00000007be9e0500, 0x00000007beaf0000) * to space 1088K, 0% used [0x00000007beaf0000, 0x00000007beaf0000, 0x00000007bec00000) * tenured generation total 20480K, used 16972K [0x00000007bec00000, 0x00000007c0000000, 0x00000007c0000000) * the space 20480K, 82% used [0x00000007bec00000, 0x00000007bfc93030, 0x00000007bfc93200, 0x00000007c0000000) * Metaspace used 3380K, capacity 4496K, committed 4864K, reserved 1056768K * class space used 372K, capacity 388K, committed 512K, reserved 1048576K */ }

 

最新回复(0)