SoftReference incorrect javadoc?

Per Liden per.liden at oracle.com
Tue Apr 16 06:27:15 UTC 2019


Hi Michael,

On 4/16/19 4:19 AM, David Holmes wrote:
> Hi Michael,
> 
> Re-directing to core-libs-dev and hotspot-gc-dev.
> 
> Thanks,
> David
> 
> On 16/04/2019 12:14 pm, Michael Pollmeier wrote:
>> Quoting
>> https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/ref/SoftReference.html 
>>
>>
>>
>>> All soft references to softly-reachable objects are guaranteed to have
>> been cleared before the virtual machine throws an OutOfMemoryError
>>
>> That statement was true when soft references were first introduced in
>> java 1.2, but from java 1.3.1 the jvm property
>> `-XX:SoftRefLRUPolicyMSPerMB` was introduced.

That statement is still true. When the GC gets into a situation where it 
is having trouble satisfying an allocation, then SoftRefLRUPolicyMSPerMB 
will be ignored and all soft references will be cleared, before the GC 
gives up and throws an OOME.

>> It defaults to 1000 (milliseconds), meaning that if there’s only 10MB
>> available heap, the garbage collector will free references that have
>> been used more than 10s ago. I.e. everything else (including young
>> softly reachable objects) will *not* be freed, leading to an
>> OutOfMemoryError, contradicting the above quoted 'guarantee'.
>>
>> That's also the behaviour I observed on various JREs. Would you agree,
>> i.e. should I propose an updated doc?

Could you elaborate on what kind of test you did to come to this 
conclusion? Preferably provide a re-producer. In OOME situations, if a 
SoftReference isn't cleared, it is typically because you have 
unknowingly made it strongly reachable.

cheers,
Per



More information about the hotspot-gc-dev mailing list