java WeakHashMap

最近读英文的effective_java,感受颇深,很好的书籍.
item7:eliminate obsolete object references(消除废弃的对象引用)中有一段:

If you’re lucky enough to implement a cache for which an entry is
relevant exactly so long as there are references to its key outside
of the cache, represent the cache as a WeakHashMap; entries will be
removed automatically after they become obsolete. Remember that
WeakHashMap is useful only if the desired lifetime of cache entries
is determined by external references to the key, not the value.

大概意思是(英文不好):
如果你有幸去实现一个缓存(大概写底层很牛逼吧- -),这个缓存中的entry只有当外部有引用指向它的key(entry的key)时才有意义(entry是Map中的Entry吧),代表性的就是WeakHashMap。entries将会在他们废弃之后自动的被移除.记住WeakHashMap只有在缓存中entry的生命周期是通过外部引用指向它的key而不是value时才会有用

进而试验了一番:

  String key1 = new String("key1");
        String key2 = new String("key2");

        Map weakMap = new WeakHashMap();
        weakMap.put(key1, "value1");
        weakMap.put(key2, "value2");

        System.out.println("垃圾回收前:");
        Set<Map.Entry> set = weakMap.entrySet();
        for(Map.Entry entry : set){
            Object key = entry.getKey();
            Object value = entry.getValue();
            System.out.println("key: " + key);
            System.out.println("value: " + value);
        }
        System.out.println("垃圾回收前size:" + weakMap.size());

        // 将key1置为null,以便垃圾回收
        key1 = null;

        System.gc();

        System.out.println("垃圾回收后:");
        for(Map.Entry entry : set){
            Object key = entry.getKey();
            Object value = entry.getValue();
            System.out.println("key: " + key);
            System.out.println("value: " + value);
        }
        System.out.println("垃圾回收后size:" + weakMap.size());

输出结果:
结果
可以看出来,在把key1变成null并进行垃圾回收后,weakmap中以key1为键的entry已经被移除了,普通的map是没有这个功能的,可以去试一试.
但是做一个小小的变化,只要让有别的对象对key1有引用,那weakHashMap中的key1为键的entry便不会消失:


/** 这是一个类 **/
    class A{
        private String a;

        public String getA() {
            return a;
        }

        public void setA(String a) {
            this.a = a;
        }
    }
/** 这是一个类 **/

    public static void main(String args[]){

        // 类的对象
        WeakHashMapDeom.A a = new WeakHashMapDeom().new A();

        String key1 = new String("key1");
        String key2 = new String("key2");

        Map weakMap = new WeakHashMap();
        weakMap.put(key1, "value1");
        weakMap.put(key2, "value2");

        System.out.println("垃圾回收前:");
        Set<Map.Entry> set = weakMap.entrySet();
        for(Map.Entry entry : set){
            Object key = entry.getKey();
            Object value = entry.getValue();
            System.out.println("key: " + key);
            System.out.println("value: " + value);
        }
        System.out.println("垃圾回收前size:" + weakMap.size());

        // 让它有key1的引用
        a.setA(key1);

        // 将key1置为null,以便垃圾回收
        key1 = null;

        System.gc();

        System.out.println("垃圾回收后:");
        for(Map.Entry entry : set){
            Object key = entry.getKey();
            Object value = entry.getValue();
            System.out.println("key: " + key);
            System.out.println("value: " + value);
        }
        System.out.println("垃圾回收后size:" + weakMap.size());
    }

结果
结果很明显了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值