File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Performance and the fly likes Why we have WeakHashMap and not SoftHashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Why we have WeakHashMap and not SoftHashMap" Watch "Why we have WeakHashMap and not SoftHashMap" New topic
Author

Why we have WeakHashMap and not SoftHashMap

shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
Why we don't have SoftHashMap in java? In my opinion having SoftHashMap is more useful than having WeakHashMap. I want opinion, suggestions of ranchers.


SCJP 1.5(97%) My Blog
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39


In the few cases, where I used weak references (or soft references), I used the classes, (and queues) directly. Never used the WeakHashMap class. So, never thought about the possibility of the SoftHashMap class. IMO, the WeakHashMap class was written for internal (as support) reasons, and then exposed because others may need it. Maybe that is why SoftHashMap was never written.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
Thanks Henry. I would appreciate more though from ranchers.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39


Sorry for bumping an old topic, but I would like to elaborate my answer a bit....

In thinking about it a bit more, I don't think that it is possible to write a SoftHashMap class. Remember that a SoftHashMap isn't just a WeakHashMap that is using SoftReferences. It has to also not "distrurb" those soft references. Let me explain....

To implement a WeakHashMap, just use WeakReferences on the keys. This means, of course, that you need to do an additional deference to add/remove the key/value pairs. You will also need to attach a ReferenceQueue so that you can detect that a key has been GC'ed -- which the WeakHashMap will use to automatically delete the key-value pair.

There is a minor flaw though -- particularly if more than one key lands in a bucket. The WeakHashMap needs to deference the key (ie. make it a hard reference temporarily) so that it can use the hashCode() and equals() methods for the operation. This means, that in theory, it is possible for a key (that isn't referenced by the application) to survive a GC cycle -- if by chance the WeakHashMap is doing this operation..... This is probably not a big deal, as the chances are ridiculously low, and it can be picked up on the next GC cycle.


This becomes a major flaw with the SoftHashMap. Remember that SoftReference objects may or may not be GC'ed even if there are no hard references. It is at the whim of the JVM, which decides based on the amount of memory that it needs. The idea is, if there are lots of memory being freed, there is no reason to completely delete a cache that could be useful to the application.

The issue is, the SoftReference determines which instance to GC based on the access -- it keeps timestamps of which object has been used and how recent (Least Recently Used). This means, that the temporary conversion of the soft reference to a hard reference (to do the equals()) will affect the LRU. A key that hasn't been accessed by the application in a very long time, may survive a GC purely because it shares a bucket with a more commonly used key. And unlike the WeakHashMap issue, the chances are not low, and it may not be picked up in the next GC cycle.

Basically, you have a SoftHashMap that has soft references, but isn't really a soft hash map.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39


Of course, I am not saying that having a SoftHashMap isn't possible. I am just saying that to implement it correctly, it will need a feature from the SoftReference class that doesn't currently exist.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why we have WeakHashMap and not SoftHashMap
 
Similar Threads
Soft Hashmap, weak, strong references etc.
lock-update-unlock
a simple WeakReference and SoftReference exemple
Hash map???
NX:URlyBird 1.3.2/ A question for Database cache.