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

    定义一个类,在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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
 本次课程会带着大家学习Hash算法,从源码的角度去学习算法,更加容易理解的方式去学习,能够更高效的吸收学到的内容,也能培养出能够独自看源码,分析源码的能力。Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值。这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不同的输入可能会散列成相同的输出,而不可能从散列值来唯一的确定输入值。简单的说就是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数。  哈希表是根据设定的哈希函数H(key)和处理冲突方法将一组关键字映射到一个有限的地址区间上,并以关键字在地址区间中的象作为记录在表中的存储位置,这种表称为哈希表或散列,所得存储位置称为哈希地址或散列地址。作为线性数据结构与表格和队列等相比,哈希表无疑是查找速度比较快的一种。  通过将单向数学函数(有时称为“哈希算法”)应用到任意数量的数据所得到的固定大小的结果。如果输入数据中有变化,则哈希也会发生变化。哈希可用于许多操作,包括身份验证和数字签名。也称为“消息摘要”。  简单解释:哈希(Hash)算法,即散列函数。它是一种单向密码体制,即它是一个从明文到密文的不可逆的映射,只有加密过程,没有解密过程。同时,哈希函数可以将任意长度的输入经过变化以后得到固定长度的输出。哈希函数的这种单向特征和输出数据长度固定的特征使得它可以生成消息或者数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值