• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

WeakHashmap in java

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic