| Author |
WeakHashmap in java
|
anish jain
Ranch Hand
Joined: Feb 03, 2010
Posts: 129
|
|
I read about weakhashmap and understood that an entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use.
May I know exactly when the entry from the weakhashmap is removed i.e. how to decide when its key is no longer used?
Is there any time period defined? Also let mw know the use of WeakHashmap?
|
 |
Lanny Gilbert
Ranch Hand
Joined: Jun 11, 2002
Posts: 103
|
|
Here's a good article explaining what WeakHashMap is good for (and when not to use it).
http://www.codeinstructions.com/2008/09/weakhashmap-is-not-cache-understanding.html
|
 |
anish jain
Ranch Hand
Joined: Feb 03, 2010
Posts: 129
|
|
Thnaks Lanny that was a very useful information shared by you but still it doesn't answer my question
An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use.
May I know exactly when the entry from the weakhashmap is removed i.e. how to decide when its key is no longer used? Is there any time period defined?
|
 |
Tapan Maru
Ranch Hand
Joined: May 08, 2006
Posts: 65
|
|
My solution about ordinary use is --> Whenever GC thread runs, it will first identify all the objects for which there are no active reference present in the JVM. Suppose in the list of those objects, any particular object is used as a Key in WeakHashMap, the corresponding key-value pair will be removed from that WeakHashMap.
Please correct me if my understanding is not correct.
|
Tapan Maru
tapanmaru@gmail.com
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5846
|
|
anish jain wrote:
May I know exactly when the entry from the weakhashmap is removed i.e. how to decide when its key is no longer used? Is there any time period defined?
When you don't have any references to that object other than in the map key, the key object becomes eligible for GC. When it's GCed, it's removed from the map.
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5846
|
|
Tapan Maru wrote:My solution about ordinary use is --> Whenever GC thread runs, it will first identify all the objects for which there are no active reference present in the JVM. Suppose in the list of those objects, any particular object is used as a Key in WeakHashMap, the corresponding key-value pair will be removed from that WeakHashMap.
Please correct me if my understanding is not correct.
It will be removed if the only reference to that object is the map's key, yes.
|
 |
 |
|
|
subject: WeakHashmap in java
|
|
|