Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

WeakHashMap

 
Isabel Wanderley
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain when WeakHashMap is used and how it works?? I'm studying it here but I didn't understand what's used for...
Thanks
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Items in a WeakHashMap are eligible for garbage collection if the WeakHashMap has the only reference.
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote this some time ago:
 
John Dale
Ranch Hand
Posts: 399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think items (keys and values) in a WeakHashMap are removed from the map when the key is no longer referenced outside the map. The value then eligible for garbage collection only if it is not referenced elsewhere (perhaps by another key in the map).
I've not had an occasion in which I knew needed WeakHashMap yet, so there is a chance I don't have this right.
I suspect WeakHashMap may be useful when a subsystem needs to provide a handle by which other subsystems can refer to an object. The handle could be key in a WeakHashMap. When no other subsystem (and nothing besides the WeakHashMap) contains a reference to a particular key, that key and its value are removed from the map. At that point, if nothing else refers to the value object, the value object is unreferenced and hence eligable for garbage collection.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
me too
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic