WeakHashMap的remove方法导致对象回收的测试

来源:互联网 发布:虚拟图片打印机软件 编辑:程序博客网 时间:2024/06/08 03:51
    定义一个类,在finalize方法中添加log
public class Test{
        int value;

        public Test() {
            // TODO Auto-generated constructor stub
        }

        public Test(int i) {
            // TODO Auto-generated constructor stub
            value = i;
        }

        @Override
        protected void finalize() throws Throwable {
            // TODO Auto-generated method stub
            System.out.println("Test finalize,value=" + value 
                    + ", this=" + this);

            super.finalize();
        }
    }

    public void testWMap()
    {
        //test WeakHashMap
        WeakHashMap<String, Test>wmap = new WeakHashMap<String, Test>();
        wmap.put(""+1, new Test(1));
        wmap.put(""+2, new Test(2));
        wmap.put(""+3, new Test(3));
        wmap.put(""+4, new Test(4));
        wmap.put(""+5, new Test(5));

        System.gc();
        System.out.println("wmap.size=" + wmap.size());
        wmap.remove("1");

        System.out.println("wmap.size=" + wmap.size());

        for (int i = 6; i < 30; i++) {
            wmap.put(""+i, new Test(i));

            System.out.println("== wmap.size=" + wmap.size());
        }
        //gc do not make the Test object to release
        //but remove from the weakmap and than gc can make it release
        System.gc();
        wmap.remove("2");
        for (int i = 0; i < wmap.size(); i++) {

            System.out.println("key=" + wmap.keySet().toArray()[i]);
        }
        System.out.println("== wmap.size=" + wmap.size());
//        System.gc();

    }

调用testWMap()

wmap.size=5
wmap.size=4
== wmap.size=5
== wmap.size=6
== wmap.size=7
== wmap.size=8
== wmap.size=9
== wmap.size=10
== wmap.size=11
== wmap.size=12
== wmap.size=13
== wmap.size=14
== wmap.size=15
== wmap.size=16
== wmap.size=17
== wmap.size=18
== wmap.size=19
== wmap.size=20
== wmap.size=21
== wmap.size=22
== wmap.size=23
== wmap.size=24
== wmap.size=25
== wmap.size=26
== wmap.size=27
== wmap.size=28
Test finalize,value=1, this=com.foxx.a.File1$Test@ca0b6
key=4
key=5
key=3
== wmap.size=3

可以看出,WeakHashMap中没有被引用的对象可能会被WeakHashMap去掉(remove)
被remove后,再System.gc(),才能够让对象及时的被回收。
WeakHashMap自动remove难以确定何时进行,必要的时候,还是需要调用remove方法

修改为
    public void testWMap()
    {
        //test WeakHashMap
        WeakHashMap<String, Test>wmap = new WeakHashMap<String, Test>();
        wmap.put(""+1, new Test(1));
        wmap.put(""+2, new Test(2));
        wmap.put(""+3, new Test(3));
        wmap.put(""+4, new Test(4));
        wmap.put(""+5, new Test(5));

        System.gc();
        System.out.println("wmap.size=" + wmap.size());
        wmap.remove("1");

        System.out.println("wmap.size=" + wmap.size());

        for (int i = 6; i < 30; i++) {
            wmap.put(""+i, new Test(i));

            System.out.println("== wmap.size=" + wmap.size());
        }
        //gc do not make the Test object to release
        //but remove from the weakmap and than gc can make it release
        System.gc();
        wmap.remove("2");
        for (int i = 0; i < wmap.size(); i++) {

            System.out.println("key=" + wmap.keySet().toArray()[i]);
        }
        System.out.println("== wmap.size=" + wmap.size());
        System.gc();

    }

对象都会被回收
wmap.size=5
wmap.size=4
== wmap.size=5
== wmap.size=6
== wmap.size=7
== wmap.size=8
== wmap.size=9
== wmap.size=10
== wmap.size=11
== wmap.size=12
== wmap.size=13
== wmap.size=14
== wmap.size=15
== wmap.size=16
== wmap.size=17
== wmap.size=18
== wmap.size=19
== wmap.size=20
== wmap.size=21
== wmap.size=22
== wmap.size=23
== wmap.size=24
== wmap.size=25
== wmap.size=26
== wmap.size=27
== wmap.size=28
Test finalize,value=1, this=com.foxx.a.File1$Test@1a758cb
key=4
key=5
key=3
== wmap.size=3
Test finalize,value=13, this=com.foxx.a.File1$Test@69b332
Test finalize,value=29, this=com.foxx.a.File1$Test@173a10f
newString=com.UCMobile
Test finalize,value=28, this=com.foxx.a.File1$Test@530daa
Test finalize,value=27, this=com.foxx.a.File1$Test@a62fc3

Test finalize,value=26, this=com.foxx.a.File1$Test@89ae9e

Test finalize,value=25, this=com.foxx.a.File1$Test@1270b73
 
Test finalize,value=24, this=com.foxx.a.File1$Test@60aeb0

Test finalize,value=23, this=com.foxx.a.File1$Test@16caf43


Test finalize,value=22, this=com.foxx.a.File1$Test@66848c
Test finalize,value=21, this=com.foxx.a.File1$Test@8813f2
Test finalize,value=20, this=com.foxx.a.File1$Test@1d58aae
Test finalize,value=19, this=com.foxx.a.File1$Test@83cc67
Test finalize,value=18, this=com.foxx.a.File1$Test@e09713
Test finalize,value=17, this=com.foxx.a.File1$Test@156ee8e
Test finalize,value=16, this=com.foxx.a.File1$Test@47b480
Test finalize,value=15, this=com.foxx.a.File1$Test@10d448

Test finalize,value=14, this=com.foxx.a.File1$Test@e0e1c6

Test finalize,value=12, this=com.foxx.a.File1$Test@6ca1c
Test finalize,value=11, this=com.foxx.a.File1$Test@1bf216a
Test finalize,value=10, this=com.foxx.a.File1$Test@12ac982
Test finalize,value=9, this=com.foxx.a.File1$Test@1389e4
Test finalize,value=8, this=com.foxx.a.File1$Test@c20e24
Test finalize,value=7, this=com.foxx.a.File1$Test@2e7263
Test finalize,value=6, this=com.foxx.a.File1$Test@157f0dc
Test finalize,value=2, this=com.foxx.a.File1$Test@863399
原创粉丝点击