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.