JDK-8210577 : adjust OutOfMemoryError specification to mention implementation limits being exceeded
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 12
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2018-09-09
  • Updated: 2022-04-15
Related Reports
Relates :  
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
OutOfMemoryError currently specifies "Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector." [1]

However, as was pointed out by Chris Hegarty [2], it's kind of a de facto standard to throw an OutOfMemoryError when inherent size limits (typically of arrays) are hit. So I believe that the Javadoc of OutOfMemoryError should be updated to specify this usage and give concrete examples such as:
- allocation of arrays, e.g. new byte[Integer.MAX_VALUE] gives an OOME with message "Requested array size exceeds VM limit"
- methods that return an array/Buffer, e.g. InputStream::readAllBytes [3], Files::readAllBytes [4] and ModuleReader::read [5]
- classes that use arrays internally (ArrayList, StringBuilder, ...) and are unable to complete a requested operation, e.g. new StringBuffer().ensureCapacity(Integer.MAX_VALUE)

[1] https://download.java.net/java/early_access/jdk12/docs/api/java.base/java/lang/OutOfMemoryError.html
[2] http://mail.openjdk.java.net/pipermail/core-libs-dev/2017-March/046671.html
[3] https://download.java.net/java/early_access/jdk12/docs/api/java.base/java/io/InputStream.html#readAllBytes()
[4] https://download.java.net/java/early_access/jdk12/docs/api/java.base/java/nio/file/Files.html#readAllBytes(java.nio.file.Path)
[5] https://download.java.net/java/early_access/jdk12/docs/api/java.base/java/lang/module/ModuleReader.html#read(java.lang.String)




Comments
We probably don't want to mention specific cases or specific messages that are emitted. However, it might be reasonable to expand the definition of OutOfMemoryError to mention that it maybe thrown, either by the JVM or by library code, when some implementation limit is exceeded.
03-06-2020