• Post Reply Bookmark Topic Watch Topic
  • New Topic

WeakHashmap in java  RSS feed

 
anish jain
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lanny Gilbert wrote: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



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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!