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
)