WeakHashMap的用法? [英] Usage of WeakHashMap?

查看:388
本文介绍了WeakHashMap的用法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WeakHashMap是Map接口的一个实现,如果相应的键不再由程序的任何部分引用,则Grabage Collector
可以回收值对象的内存。因此,如果在程序中不再使用密钥。它的Entry
对象将被垃圾收集,无论其用途如何。它清晰到这里

WeakHashMap is an implementation of Map interface where the memory of the value object can be reclaimed by Grabage Collector if the corresponding key is no longer referred by any section of program. So if key is no longer used in program. its Entry object will be garbage collected irrespective of its usage. Its clear till here

这与HashMap不同,即使不再引用键,值对象仍保留在HashMap中。我们需要在HashMap对象上显式调用
remove()方法来删除该值。调用remove将只删除map中的条目。它对GC的准备将是
取决于它是否仍然在程序中的某个地方使用。

This is different from HashMap where the value object remain in HashMap even if key is no longer referred. We need to explicitly call remove() method on HashMap object to remove the value. calling remove will just remove the entry from map. Its readyness for GC will depend whether it is still used somewhere in program or not.

请找到上面解释的编码示例

根据我的理解使用WeakHashMap而非HashMap

我的理解是,只有当我们想要确保回收值对象时才应该使用WeakHashMap当
密钥不再被任何程序部分引用时,由Grabage Collector提供。这使程序内存有效我的理解是正确的吗?

My understanding is we should go for WeakHashMap only when we want to ensure that value object is reclaimed by Grabage Collector when key is no longer referred by any section of program. This makes program memory efficient Is my understanding correct here?

根据 JavaDocs ,我可以发现这个陈述

Usage of WeakHashMap as per JavaDocs , i could spot this statement

此类主要用于与等于
方法的关键对象一起使用==运算符测试对象标识。

This class is intended primarily for use with key objects whose equals methods test for object identity using the == operator.

我没有得到上述陈述的含义以及它与我对WeakHashMap用法的理解形成鲜明对比。实际上我没有得到这个陈述与WeakHashMap的用法有什么关系?

I did not get what above statement meant and how it contrast with mine understanding of WeakHashMap usage. Actually i did not get how this statement is related to usage of WeakHashMap?

更新: - 进一步仔细阅读下面的陈述javadocs

UPDATE:- on further carefully reading below statement the javadocs


当其密钥
不再正常使用时,WeakHashMap中的条目将自动被删除。更准确地说,给定键的
映射的存在不会阻止密钥被垃圾收集器丢弃
,即,可以最终化,最终确定,然后回收
。当一个键被丢弃时,它的输入实际上是从地图中删除了
,因此这个类的行为与
其他Map实现的行为略有不同。

An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. More precisely, the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed. When a key has been discarded its entry is effectively removed from the map, so this class behaves somewhat differently from other Map implementations.

我正在修改我对我和其他人的好处的理解

i am revising my understanding for the benefit of me and others

WeakHashMap在HashMap上的使用按照我的修订理解

我们应该选择WeakHashMap只有当我们想要确保在GC运行时从地图上删除键值对时,除了map之外不再使用键本身。

We should go for WeakHashMap only when we want to ensure that key-value pair is removed from map on GC run when key is no longer in ordinary use other than map itself.

例如: -

    WeakHashMap<Integer, String> numbers = new WeakHashMap<Integer, String>();
    numbers.put(new Integer(1), "one");// key only used within map not anywhere else
    numbers.put(new Integer(2), "two");
    System.out.println(numbers.get(new Integer(1))); // prints "one"
    System.gc();
    // let's say a garbage collection happens here
    System.out.println(numbers.get(new Integer(1))); // prints "null"
    System.out.println(numbers.get(new Integer(2))); // prints "null"


    Object key = new Object();
    m1.put(key, c1);
    System.out.println(m1.size());
    key = null or new Object() ; // privious key only used within map not anywhere else
    System.gc();
    Thread.sleep(100);
    System.out.println(m1.size());


推荐答案

这是因为对象将是垃圾当它们不再具有来自程序的任何其他部分的强引用时收集(GCed)。

This is due to the fact that objects will be garbage collected (GCed) when they are no longer have a strong reference from any other part of the program.

给定 WeakHashMap< MyObject,String> 然后我们执行以下操作:

Given a WeakHashMap<MyObject, String> then if we do the following:

MyObject mo = new MyObject();
map.put(mo, "Test");
mo = null;

然后条目 mo - >测试将有资格获得GC。这意味着如果你有一个自定义的 .equals 实现,它使用 MyObject 的某些属性来测试相等性,那你就不能稍后这样做:

Then the entry mo -> Test will be eligible for GC. This means that if you have a custom .equals implementation that uses some property of MyObject to test for equality then you cannot later do this:

MyObject mo2 = new MyObject();
map.get(mo2);

因为即使你被覆盖的 .equals 方法可能会说 mo2.equals(mo)== true 不是 mo2 == mo 的情况,因此该条目可能已经过GCed。

Because even though your overridden .equals method may say that mo2.equals(mo) == true it is not the case that mo2 == mo and therefore the entry may have already been GCed.

重点是,如果你保留对 mo 的引用并使用它要从 Map 中检索值,那么该引用必须 == mo ,因此有两件事情是true:

The point is that if you keep a reference to mo and use that to retrieve the value from the Map then it is the case that that reference must == mo and therefore two things are true:


  1. 条目 mo - >无法测试

  2. 您可以使用 == 基于 .equals 从地图中检索条目的方法

  1. the entry mo -> Test cannot be gced
  2. you can use an == based .equals method to retrieve the entry from the map

基本上;由于GC将使用强引用来测试对象是否可以进行GC操作,因此最好确保您的 .equals 方法做同样的操作以避免混淆。

Basically; as the GC will use strong references to test whether an object can be GCed it is best to ensure that your .equals method does the same to avoid confusion.

这篇关于WeakHashMap的用法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆