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

本文通过实例演示了WeakHashMap的工作原理及其对对象回收的影响。解释了如何通过remove操作配合垃圾回收来实现对象的有效释放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    定义一个类,在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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值