打开日志:运行配置---XX:+PrintGCDetails
示例程序:
package com.test; public class Test { private Object instance = null; private String str; private static final int _1MB = 1024 * 1024; /** 这个成员属性唯一的作用就是占用一点内存 */ private byte[] bigSize = new byte[2 * _1MB]; public static void main(String[] args) { Test objectA = new Test(); objectA.str = objectA.bigSize.toString(); Test objectB = new Test(); objectB.str = objectB.bigSize.toString(); objectA.instance = objectB; objectB.instance = objectA; objectA = null; objectB = null; System.gc(); } }
打印:
[GC [PSYoungGen: 9912K->568K(229376K)] 9912K->568K(491520K), 0.0052670 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] [Full GC [PSYoungGen: 568K->0K(229376K)] [ParOldGen: 0K->465K(262144K)] 568K->465K(491520K) [PSPermGen: 2573K->2572K(131072K)], 0.0105626 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] Heap PSYoungGen total 229376K, used 5898K [0x00000000f0000000, 0x0000000100000000, 0x0000000100000000) eden space 196608K, 3% used [0x00000000f0000000,0x00000000f05c29c0,0x00000000fc000000) from space 32768K, 0% used [0x00000000fc000000,0x00000000fc000000,0x00000000fe000000) to space 32768K, 0% used [0x00000000fe000000,0x00000000fe000000,0x0000000100000000) ParOldGen total 262144K, used 465K [0x00000000e0000000, 0x00000000f0000000, 0x00000000f0000000) object space 262144K, 0% used [0x00000000e0000000,0x00000000e00744a0,0x00000000f0000000) PSPermGen total 131072K, used 2579K [0x00000000d0000000, 0x00000000d8000000, 0x00000000e0000000) object space 131072K, 1% used [0x00000000d0000000,0x00000000d0284e98,0x00000000d8000000)
注释掉objectA = null; 和 objectB = null;
打印:
[GC [PSYoungGen: 9912K->4664K(229376K)] 9912K->4664K(491520K), 0.0053971 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [Full GC [PSYoungGen: 4664K->0K(229376K)] [ParOldGen: 0K->4561K(262144K)] 4664K->4561K(491520K) [PSPermGen: 2573K->2572K(131072K)], 0.0118699 secs] [Times: user=0.01 sys=0.03, real=0.01 secs] Heap PSYoungGen total 229376K, used 5898K [0x00000000f0000000, 0x0000000100000000, 0x0000000100000000) eden space 196608K, 3% used [0x00000000f0000000,0x00000000f05c29c0,0x00000000fc000000) from space 32768K, 0% used [0x00000000fc000000,0x00000000fc000000,0x00000000fe000000) to space 32768K, 0% used [0x00000000fe000000,0x00000000fe000000,0x0000000100000000) ParOldGen total 262144K, used 4561K [0x00000000e0000000, 0x00000000f0000000, 0x00000000f0000000) object space 262144K, 1% used [0x00000000e0000000,0x00000000e0474570,0x00000000f0000000) PSPermGen total 131072K, used 2579K [0x00000000d0000000, 0x00000000d8000000, 0x00000000e0000000) object space 131072K, 1% used [0x00000000d0000000,0x00000000d0284e90,0x00000000d8000000)
日志信息解释:
YoungGC日志解释如下
FullGC