SoftReferences and java.lang.OutOfMemoryError: Direct buffer memory
Florian Weimer
fw at deneb.enyo.de
Sun Dec 20 21:50:30 UTC 2020
* Brett Okken:
> I am not sure if this is the correct mailing list for this question.
> Please let me know if I should post it somewhere else.
>
> The javadoc for SoftReference states:
>
> All soft references to softly-reachable objects are guaranteed to have been
> cleared before the virtual machine throws an OutOfMemoryError.
>
> But that is not quite true when it comes to allocating direct ByteBuffer
> instances. Would it be possible to clear softly-reachable direct ByteBuffer
> instances prior to throwing java.lang.OutOfMemoryError: Direct buffer
> memory? This would make it much simpler (and safer) to implement a cache of
> direct ByteBuffer instances for re-use.
It's not true in other contexts, either:
jshell> var r = new SoftReference<>(new Object() {});
r ==> java.lang.ref.SoftReference at 57fa26b7
jshell> r.get();
$11 ==> $0 at 2f410acf
jshell> new long[Integer.MAX_VALUE];
| Exception java.lang.OutOfMemoryError: Requested array size exceeds VM limit
jshell> r.get();
$13 ==> $0 at 2f410acf
jshell> IntStream.range(0, 1000000000).boxed().collect(Collectors.toList()).size();
| Exception java.lang.OutOfMemoryError: Java heap space
jshell> r.get();
$15 ==> $0 at 2f410acf
If taken literally, the specification requires that the
OutOfMemoryError constructor has to clear all SoftReference objects,
but that does not seem to happen.
I think the intent is to encourage implementations not to report
spurious OOMEs if they could be avoid by clearing soft references.
More information about the nio-dev
mailing list