证明jvm未使用引用计数器

引用计数概要

在对象中加入一个引用计数器,每当有一个地方引用时,计数器值就加1;
当引用计数失效时,计数器值就减1;任何时刻,对象的引用计数未0时,
对象将被回收。

现状

引用计数算法(referenc counting)实现简单,判定效率也很高,像
微软COM、FlashPlayer、Python、oc等都引用计数算法来管理
内存。

但是,主流JVM 没有使用reference counting 算法。

原因是reference counting算法很难解决循环引用问题。

证明Hotspot没使用reference counting算法:

public class TestGC {

    public Object instance;
    private final static int _1M = 1024 * 1024; 
    private byte[] bytesize =  new byte[2*_1M];

    public static void testGC() {
        TestGC A = new TestGC();
        TestGC B = new TestGC();
        A.instance = B;
        B.instance = A;
        A = null;
        B = null;
        System.gc();
    }
}

public class Test {
    public static void main(String[] args) {
        TestGC.testGC();
    }
}

控制台打印:
[GC (System.gc()) [PSYoungGen: 5407K->552K(18944K)] 5407K->552K(62976K), 0.0434520 secs] [Times: user=0.00 sys=0.00, real=0.04 secs]
[Full GC (System.gc()) [PSYoungGen: 552K->0K(18944K)] [ParOldGen: 0K->517K(44032K)] 552K->517K(62976K), [Metaspace: 2661K->2661K(1056768K)], 0.0229769 secs] [Times: user=0.05 sys=0.00, real=0.02 secs]
Heap
PSYoungGen total 18944K, used 164K [0x00000000eb300000, 0x00000000ec800000, 0x0000000100000000)
eden space 16384K, 1% used [0x00000000eb300000,0x00000000eb3290d0,0x00000000ec300000)
from space 2560K, 0% used [0x00000000ec300000,0x00000000ec300000,0x00000000ec580000)
to space 2560K, 0% used [0x00000000ec580000,0x00000000ec580000,0x00000000ec800000)
ParOldGen total 44032K, used 517K [0x00000000c1800000, 0x00000000c4300000, 0x00000000eb300000)
object space 44032K, 1% used [0x00000000c1800000,0x00000000c1881670,0x00000000c4300000)
Metaspace used 2668K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 282K, capacity 386K, committed 512K, reserved 1048576K

分析:

主要看:
[GC (System.gc()) [PSYoungGen: 5407K->552K(18944K)] 5407K->552K(62976K)

堆在gc回收前大小为5407k,gc后为552k;
说明两个对象并没有因为相互持有,造成循环引用,无法释放。间接证明JVM 并未采用reference counting算法管理内存。

延伸

控制台打印GC日志

1.右键项目或文件——Debug As——Debug Configurations

2.双击Java Application——VM arguments中填写-verbose:gc——Debug。

VM arguments参数配置:

-verbose:gc (开启打印垃圾回收日志)

-Xloggc:D:testgc.log (设置垃圾回收日志打印的文件,文件名称可以自定义)

-XX:+PrintGCTimeStamps (打印垃圾回收时间信息时的时间格式)

-XX:+PrintGCDetails (打印垃圾回收详情)

(以上仅为个人读书笔记,如有不当,欢迎指正!)

猜你喜欢

转载自blog.csdn.net/lovehalok/article/details/81145859