Java GC 应用了“引用计数算法没"?运行下面代码就知道了:
public class RefernceCountingGC {
public Object instance = null;
private static final int _3MB = 1024 * 1024 * 3;
private byte[] useSpace = new byte[10 * _3MB];
/**
* VM Args:-verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
RefernceCountingGC ins1 = new RefernceCountingGC();
RefernceCountingGC ins2 = new RefernceCountingGC();
ins1.instance = ins2;
ins2.instance = ins1;
ins1 = null;
ins2 = null;
// 进行GC
System.gc();
}
}
运行结果
[GC [PSYoungGen: 31374K->584K(38080K)] 62094K->31304K(125056K), 0.0009580 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (System) [PSYoungGen: 584K->0K(38080K)] [ParOldGen: 30720K->474K(86976K)] 31304K->474K(125056K) [PSPermGen: 2550K->2549K(21248K)], 0.0046570 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
Heap
PSYoungGen total 38080K, used 1962K [0x00000007d58b0000, 0x00000007d8320000, 0x0000000800000000)
eden space 32704K, 6% used [0x00000007d58b0000,0x00000007d5a9aa28,0x00000007d78a0000)
from space 5376K, 0% used [0x00000007d78a0000,0x00000007d78a0000,0x00000007d7de0000)
to space 5376K, 0% used [0x00000007d7de0000,0x00000007d7de0000,0x00000007d8320000)
ParOldGen total 86976K, used 474K [0x0000000780a00000, 0x0000000785ef0000, 0x00000007d58b0000)
object space 86976K, 0% used [0x0000000780a00000,0x0000000780a769b0,0x0000000785ef0000)
PSPermGen total 21248K, used 2559K [0x000000077b800000, 0x000000077ccc0000, 0x0000000780a00000)
object space 21248K, 12% used [0x000000077b800000,0x000000077ba7fe18,0x000000077ccc0000)
很明显:31304K->474K(125056K) 已经回收了,java GC 没有用“引用计数算法”,而是用了跟搜索算法!