What are the "practical consequences" of using soft references?
Asked Answered
W

2

7

Per the documentation for Guava's MapMaker.softValues():

Warning: in most circumstances it is better to set a per-cache maximum size instead of using soft references. You should only use this method if you are well familiar with the practical consequences of soft references.

I have an intermediate understanding of soft references - their behavior, uses, and their contract with garbage collection. However I'm wondering what these practical consequences are which the doc alludes to. Why exactly is it better to use maximum size rather than soft references? Don't the algorithms and behavior of soft references make their use more efficient that a hardcoded ceiling, in terms of implementing a cache?

Wanderlust answered 6/7, 2011 at 6:2 Comment(0)
S
4

I think that all they are alluding too is that you should be prepared for maximum memory usage, and potentially more gc activity, if you use a Soft reference map, since references are only gc'd as memory needs to be freed up.

If you know you only need the last n values in the cache then using a LRU Cache is a leaner approach, with more predictable resource usage for a running application.

Furthermore, according to this, it seems there are subtle differences in behaviour between -server and -client JVM's.

The Sun JRE does treat SoftReferences differently from WeakReferences. We attempt to hold on to object referenced by a SoftReference if there isn't pressure on the available memory. One detail: the policy for the "-client" and "-server" JRE's are different: the -client JRE tries to keep your footprint small by preferring to clear SoftReferences rather than expand the heap, whereas the -server JRE tries to keep your performance high by preferring to expand the heap (if possible) rather than clear SoftReferences. One size does not fit all.

Sumptuous answered 6/7, 2011 at 6:29 Comment(1)
Thanks, I can see the advantages of LRU more clearly now. Taking this route, is there any way to guarantee that no strongly reachable object is evicted (using MapMaker)?Wanderlust
D
4

One of the practical problems with using SoftReferences is that they tend to be discarded all at once. The reason you have a cache is to provide pretty good perform, most of the time.

However using SoftReferences for a cache can mean after your application has stopped for a GC, it will run slowly until the cache is rebuilt. i.e. Just at the time you need to application catch up.

Note: You can use a LinkedHashMap as an LRU cache, its doesn't have to be complex.

Deflection answered 6/7, 2011 at 6:44 Comment(2)
+1 I didn't realize they were usually all discarded at once. Thanks for the helpWanderlust
A WeakReference can be discarded on an GC, a SoftReference will be discarded at the GCs discretion. However, it is highly likely that if one has to go, they will all be reclaimed. i.e. it not a graceful degredation.Deflection

© 2022 - 2024 — McMap. All rights reserved.