首页 > 代码库 > WeakHashMap, NOT A CACHE

WeakHashMap, NOT A CACHE

Overview

Base

  1. Map的实现
  2. 基于WeakReference的Entity实现
  3. 基于Reference和ReferenceQueue实现
  4. 它的弱引用是键,而不是值
  5. 它的key会被全自动回收(VM),value值不会,只有当触发put,remove,get方法的时候才会手动回收

JAVA Reference

>>>>here is about java.lang.ref

Detail

包含具体的实现

Key值回收

该动作由VM执行,当没有Strong Reference引用的时候,在下一个回收周期会被回收

Value(Entity)值回收

该动作由put, remove, get方法触发

基于ReferenceQueue监视哪些key已经被回收,随着上述方法的触发以回收过期的元素

 1     private void expungeStaleEntries() { 2     Entry<K,V> e; 3         while ( (e = (Entry<K,V>) queue.poll()) != null) { 4             int h = e.hash; 5             int i = indexFor(h, table.length); 6  7             Entry<K,V> prev = table[i]; 8             Entry<K,V> p = prev; 9             while (p != null) {10                 Entry<K,V> next = p.next;11                 if (p == e) {12                     //兼容第一个元素13                     if (prev == e)14                         table[i] = next;15                     else16                         prev.next = next;17                     //删除值18                     e.next = null;  // Help GC19                     e.value = http://www.mamicode.com/null; //  "   "20                     size--;21                     break;22                 }23                 //删除关于entity的引用24                 prev = p;25                 p = next;26             }27         }28     }
 

Not a Cache

WeakHashMap并不是一个有用的缓存,至少并不是像大多数人想的或者网上那些奇怪的文章中介绍的那样。
原因包括以下两个:

  1. 它使用弱引用作为底层的内存管理机制。由此并不能具备缓存特质,我们并不能良好的控制它失效。而且weakReference依赖于虚拟机的实现。
  2. 它使用weak keys,而不是weak values。所以和大部分人想象的并不一样。

What is it good for

WeakHashMap is mostly useful to keep metadata about objects whose lifecycle you don‘t control

它适用于保存一些关于你所需要的对象的额外信息,而且这些额外信息你不想控制他们的生命周期

case 1

Lets say you want to associate some extra information to an object that you have a strong reference to.You put an entry in a WeakHashMap with the object as the key, and the extra information as the map value. Then, as long as you keep a strongreference to the object, you will be able to check the map to retrieve the extra information. And once you release the object, the map entry will be cleared and the memory used by the extra information will be released.

假设你有一些强引用的对象,并且他们存在一些额外的信息。我们将这些object当做WeakHashMap的key值,那些额外的信息当做value。
只要我们保持者对于这些对象的强引用,那么我们就可以通过这个weakHashMap来获取关于该对象的额外信息。而一旦我们释放了该对象
(放弃该对象的引用),这个对象相应的额外信息就会被清理

case 2

where you might keep track of what various threads in your system are doing; when the thread dies, the entry will be removed silently from your map, and you won‘t keep the Thread from being garbage collected if you‘re the last reference to it. You can then iterate over the entries in that map to find out what metadata you have about active threads in your system.

WeakHashMap<Thread, SomeMetaData>

当你需要跟踪在你系统中的各个线程正在干嘛时。当线程死亡时,这个entity就会悄无声息地从map中移除。当然你不能持有这些线程的强引用。
我们这样一来就可以通过遍历这个map来看一看那些线程在我们的系统中仍然活跃,也可以查看他对应的那些额外信息。

Tips

不要再value中保存对key的引用。

reference

  1. WeakHashMap is not a cache! Understanding WeakReference and SoftReference
  2. Java‘s WeakHashMap and caching: Why is it referencing the keys, not the values?
  3. Is there a SoftHashMap in Java?
  4. How would you implement an LRU cache in Java 6?

 

WeakHashMap, NOT A CACHE