JDK-8230744 : Several classes throw OutOfMemoryError without message
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 11,12,13,14
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2019-09-06
  • Updated: 2024-10-17
  • Resolved: 2020-06-11
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 15 JDK 16
15 b27Fixed 16Fixed
Related Reports
Relates :  
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
Usually when an OutOfMemoryError is thrown for reasons other than the VM having run out of memory, the reason is included as message, e.g.:
new byte[Integer.MAX_VALUE]: java.lang.OutOfMemoryError: Requested array size exceeds VM limit
StringUTF16.newBytesFor(int): java.lang.OutOfMemoryError: UTF16 String size is [X], should be less than 1073741823

However, there are classes which throw OutOfMemoryError instances without message when the resulting array would be too large. This is not really helpful because the documentation of OutOfMemoryError still only says: "Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory" (see also JDK-8210577)
Therefore the user would assume that the JVM ran out of memory, which is not actually the case.

Simply search for calls of the OutOfMemoryError() constructor. At the time of writing this, they are here:
(list probably incomplete)
- https://github.com/openjdk/jdk/blob/d7ca08a5cc64c8d3941493d98423a49b5bc1b922/src/java.base/share/classes/java/lang/AbstractStringBuilder.java#L269
- https://github.com/openjdk/jdk/blob/d7ca08a5cc64c8d3941493d98423a49b5bc1b922/src/java.base/share/classes/java/lang/String.java#L2189
- https://github.com/openjdk/jdk/blob/d7ca08a5cc64c8d3941493d98423a49b5bc1b922/src/java.base/share/classes/java/lang/StringLatin1.java#L360
- https://github.com/openjdk/jdk/blob/d7ca08a5cc64c8d3941493d98423a49b5bc1b922/src/java.base/share/classes/java/lang/StringUTF16.java#L664
- https://github.com/openjdk/jdk/blob/d7ca08a5cc64c8d3941493d98423a49b5bc1b922/src/java.base/share/classes/java/util/concurrent/PriorityBlockingQueue.java#L306
- https://github.com/openjdk/jdk/blob/4f02d011b06fbd033071e8d0d02cffb60d2bed49/src/java.base/share/classes/java/util/regex/Pattern.java#L1685

(Possibly consider deprecating the OutOfMemoryError constructor without arguments?)

Note that there also classes which throw OutOfMemoryError with misleading messages:
(list probably incomplete)
- https://github.com/openjdk/jdk/blob/d7ca08a5cc64c8d3941493d98423a49b5bc1b922/src/java.base/share/classes/sun/text/bidi/BidiBase.java#L1352
- https://github.com/openjdk/jdk/blob/d7ca08a5cc64c8d3941493d98423a49b5bc1b922/src/java.base/share/classes/sun/text/bidi/BidiBase.java#L1361
- https://github.com/openjdk/jdk/blob/d7ca08a5cc64c8d3941493d98423a49b5bc1b922/src/java.base/share/classes/sun/text/bidi/BidiBase.java#L1455
- https://github.com/openjdk/jdk/blob/d7ca08a5cc64c8d3941493d98423a49b5bc1b922/src/java.base/share/classes/sun/text/bidi/BidiBase.java#L1777



---------- BEGIN SOURCE ----------
// Requires String.COMPACT_STRINGS = true
int missingToIntMax = 10;
String tooLarge = "a".repeat(Integer.MAX_VALUE - missingToIntMax);
tooLarge.replace("a", "a".repeat(missingToIntMax + 2));
---------- END SOURCE ----------


Comments
Changeset: 03642a01 Author: Jim Laskey <jlaskey@openjdk.org> Date: 2020-06-11 10:08:23 +0000 URL: https://git.openjdk.java.net/lanai/commit/03642a01
02-07-2020

URL: https://hg.openjdk.java.net/jdk/jdk/rev/49a68abdb0ba User: jlaskey Date: 2020-06-11 13:09:00 +0000
11-06-2020

Review: http://mail.openjdk.java.net/pipermail/core-libs-dev/2020-June/066874.html
09-06-2020

It might be reasonable to split this into several bugs if appropriate, that is, if we decide to do something about these issues. The PriorityBlockingQueue bug should be in java.util.concurrent. The Pattern bug should be in java.util.regex. The BidiBase bugs should be in java.util:i18n.
09-09-2019

To reproduce the issue, run the attached test case: JDK 11.0.4- Fail JDK 13-ea - Fail JDK 14-ea - Fail Output: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.base/java.util.Arrays.copyOf(Arrays.java:3794) at java.base/java.lang.StringLatin1.replace(StringLatin1.java:349) at java.base/java.lang.String.replace(String.java:2179) at JI9062163.main(JI9062163.java:6)
09-09-2019