Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Soft Hashmap, weak, strong references etc.

 
Adam Rees
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please put me on the right track with this?. I've had a search through the forums and google and can't find anything relevant, but apologies if I'm repeating a thread. Anyway, on to the question.

Basically I'm throwing around some pretty large hashmaps and to avoid memory problems I'd like to make it a soft hashmap. Now, it's my understanding that the softness means if an element of the hashmap is not strongly referenced it can be garbage collected. However, if the key of a gc'd element is then accessed (using get or put), the element is reloaded into memory from a cache on a hardrive. Is this even remotely correct?

Thanks for any help in advance. Life has been saved by this site quite a few times, but this is my first question.

Cheers
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you're asking about something which doesn't actually exist in the standard API. So it's a hypothetical question. If you wrote a SoftHashMap class, you could certainly write it so that it reloaded entries which had been garbage-collected from a cache, or from the database they originally came from, or something like that.

However all you have to work with is SoftReference(object). And all you can get out of that (via the get()) method is the object (if it hasn't yet been garbage-collected) or null (if it has).
 
Adam Rees
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,

Thanks for your prompt reply. Digging further into the hierarchy, you're absolutly right. I didn't realise AbstractSoftMap was one of our classes. Sorry.

Thanks again
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wanted to clarify: (making an analogy from WeakHashMap though)

A SoftHashMap, if made on the lines of WeakHashMap, will have the keys as soft references and not the values.

(Since this is not really related to performance, moving to Java In General forum)
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Rees wrote:Basically I'm throwing around some pretty large hashmaps and to avoid memory problems I'd like to make it a soft hashmap. Now, it's my understanding that the softness means if an element of the hashmap is not strongly referenced it can be garbage collected. However, if the key of a gc'd element is then accessed (using get or put), the element is reloaded into memory from a cache on a hardrive. Is this even remotely correct?


If you're talking about WeakHashMap in the java.util package then they way it works is that an entry may disappear when its key is not strongly referenced. It may be that the entry was the only strong reference to the value, and if so the value becomes eligible for GC.

Whether they are GC'd or not, they are gone from the map. There is no memory or disk cache (unless you've implemented your own somehow) and nothing is reloaded.

You may also want to take a look at LinkedHashMap. It has nothing to do with weak references, but it has methods that can be overridden to remove old entries as new ones are added so that the number of mappings doesn't get too large. They can be used to essentially turn LinkedHashMap into an LRU cache.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic