打开日志:运行配置---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 = objectA; objectB.instance = objectB; objectA = null; objectB = null; System.gc(); } }
打印:
[GC [PSYoungGen: 5980K->584K(229376K)] 5980K->584K(491520K), 0.0020351 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [Full GC [PSYoungGen: 584K->0K(229376K)] [ParOldGen: 0K->465K(262144K)] 584K->465K(491520K) [PSPermGen: 2573K->2572K(131072K)], 0.0116565 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] Heap PSYoungGen total 229376K, used 7864K [0x00000000f0000000, 0x0000000100000000, 0x0000000100000000) eden space 196608K, 4% used [0x00000000f0000000,0x00000000f07ae210,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->4680K(229376K)] 9912K->4680K(491520K), 0.0092221 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] [Full GC [PSYoungGen: 4680K->0K(229376K)] [ParOldGen: 0K->4561K(262144K)] 4680K->4561K(491520K) [PSPermGen: 2573K->2572K(131072K)], 0.0155152 secs] [Times: user=0.05 sys=0.00, real=0.02 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