Search code examples
javagarbage-collectionsoft-referencesg1gc

Java SoftReference, panicing GC and GC behavior


I want to write a cache using SoftReferences 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 SoftReferences are good for memory-aware caches, but there is no hard rule on how a JVM implementation shall handle SoftReferences. 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):

  1. 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.
  2. What happens when memory gets tight? The JVM panics and just eats all objects?
  3. Is there a parameter for telling the JVM to only eat as much to survive (no OOMEs) and live healthy (not having the CPU only run the GC)

Solution

  • 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.