OverviewBase
- Map implementation
- Entity implementation based on weakreference
- Implementation Based on reference and referencequeue
- Its weak reference is a key, not a value.
- Its key is automatically recycled (VM), and the value is not. It is manually recycled only when the put, remove, and get methods are triggered.
Java reference
>>>> Here is about Java. Lang. Ref
Detail
Includes specific implementation
Reclaim key values
This action is executed by the VM. When no strong reference is referenced, it will be recycled in the next recycling cycle.
Value (entity) value recycling
This action is triggered by the put, remove, and get methods.
Monitors which keys have been recycled Based on referencequeue. As the preceding method is triggered, expired elements are recycled.
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 // compatible with the first element 13 if (prev = e) 14 table [I] = next; 15 else16 Prev. next = next; 17 // Delete the value 18 E. next = NULL; // help gc19 E. value = NULL; // "20 size --; 21 break; 22} 23 // Delete the reference about entity 24 Prev = P; 25 p = next; 26} 27} 28}
Not a cache
Weakhashmap is not a useful cache, at least not as it is intended by most people or as described in some strange online articles.
There are two reasons:
- It uses weak references as the underlying memory management mechanism. As a result, we cannot possess the cache characteristics. We cannot control it well and it becomes invalid. In addition, weakreference depends on the implementation of virtual machines.
- It uses weak keys instead of weak values. So it is different from what most people think.
What is it good
Weakhashmap is mostly useful to keep metadata about objects whose lifecycle you don't control
It is suitable for saving additional information about the objects you need, and you do not want to control their lifecycle.
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.
Suppose you have some strongly referenced objects and they have some additional information. We use these objects as the key values of weakhashmap and the additional information as values.
As long as we keep strong references to these objects, we can use this weakhashmap to obtain additional information about this object. And once we release this object
(Discard the object reference), the corresponding additional information of this object will be cleared
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>
When you need to track what the various threads in your system are doing. When the thread dies, the entity will be removed from the map silently. Of course you cannot hold strong references to these threads.
In this way, we can traverse this map to see which threads are still active in our system, or view the additional information corresponding to them.
Tips
Do not save the reference to the key in value.
Reference
- Weakhashmap is not a cache! Understanding weakreference and softreference
- Java's weakhashmap and caching: Why is it referencing the keys, not the values?
- Is there a softhashmap in Java?
- How wocould you implement an LRU cache in Java 6?
Weakhashmap, not a cache