File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Soft Hashmap, weak, strong references etc. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Soft Hashmap, weak, strong references etc." Watch "Soft Hashmap, weak, strong references etc." New topic
Author

Soft Hashmap, weak, strong references etc.

Adam Rees
Greenhorn

Joined: Nov 06, 2009
Posts: 2
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
Bartender

Joined: Oct 14, 2005
Posts: 18129
    
    8

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

Joined: Nov 06, 2009
Posts: 2
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

Joined: Feb 25, 2007
Posts: 1638

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)


apigee, a better way to API!
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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.


bitguru blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Soft Hashmap, weak, strong references etc.
 
Similar Threads
DBAccess interface
array with elements of different types?
HashMap Concurrency question
Blocks synchronized, but without wait() or notifyAll() methods.
Application has all memory params set, Still Consumed Memory is way too high