先给出一个小demo,这里我偷个懒直接用的周志明《深入理解Java虚拟机》第三版P69的代码清单3-1:
public class ReferenceCountingGC{
public Object instance = null;
private static final int _1MB = 1024*1024;
private byte[] bigSize = new byte[2 * _1MB];
public static void main(String[] args) {
ReferenceCountingGC obj1= new ReferenceCountingGC();
ReferenceCountingGC obj2= new ReferenceCountingGC();
obj1.instance = obj2;
obj2.instance = obj1;
System.gc();
}
}
设置VM options如下: -Xms20M -Xmx20M -XX:+PrintGCDetails。
打印出来的GC日志为:
[GC (Allocation Failure) [PSYoungGen: 3807K->488K(6144K)] 3807K->2716K(19968K), 0.0020225 secs] [Times: user=0.06 sys=0.02, real=0.00 secs]
[GC (System.gc()) [PSYoungGen: 2536K->488K(6144K)] 4764K->4820K(19968K), 0.0013806 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (System.gc()) [PSYoungGen: 488K->0K(6144K)] [ParOldGen: 4332K->4718K(13824K)] 4820K->4718K(19968K), [Metaspace: 3220K->3220K(1056768K)], 0.0065847 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
Heap
PSYoungGen total 6144K, used 169K [0x00000000ff980000, 0x0000000100000000, 0x0000000100000000)
eden space 5632K, 3% used [0x00000000ff980000,0x00000000ff9aa558,0x00000000fff00000)
from space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
to space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
ParOldGen total 13824K, used 4718K [0x00000000fec00000, 0x00000000ff980000, 0x00000000ff980000)
object space 13824K, 34% used [0x00000000fec00000,0x00000000ff09b898,0x00000000ff980000)
Metaspace used 3227K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 350K, capacity 388K, committed 512K, reserved 1048576K