I want to write a cache using SoftReference
s using as much memory as possible, as long as it doesn't get too inefficient.
Trying to estimate the used size by calculating object sizes or by getting some used memory approximation of the JVM
are dead ends.
The javadoc even states that SoftReference
s are good for memory-aware caches, but there is no hard rule on how a JVM
implementation shall handle SoftReference
s. I'm only talking about the Oracle implementation of the JVM
(Version 6.22 and above and Version 7).
Now my questions (please feel free to answer partial, grouped or in any way you please):
- Does the
JVM
take the last access of the object into account and only remove the old ones? Javadoc states:Virtual machine implementations are, however, encouraged to bias against clearing recently-created or recently-used soft references.
- What happens when memory gets tight? The
JVM
panics and just eats all objects? - Is there a parameter for telling the
JVM
to only eat as much to survive (noOOME
s) and live healthy (not having the CPU only run theGC
)
I don't think there is an order. (I'm not sure though about the order of events)
But what happens with soft references is that it is always guaranteed that they will be released before there is an out of memory exception. Unless you have a hard reference pointing to them.
But you should be aware that you might try to access them and they are gone. My guess is that the garbage collector will just eat the first soft reference that fits the amount needed for the operation.