OpenJDK 64-Bit Server VM (build 15.0.2+7, mixed mode, sharing)
... 在MacOS上,答案似乎是Integer.MAX_VALUE - 2。一旦你超越了这一点:
cat > Foo.java << "END"
public class Foo {
public static void main(String[] args) {
boolean[] array = new boolean[Integer.MAX_VALUE - 1]; // too big
}
}
END
java -Xmx4g Foo.java
... 你会得到:
Exception in thread "main" java.lang.OutOfMemoryError:
Requested array size exceeds VM limit
/**
* Some VMs reserve some header words in an array.
* Attempts to allocate larger arrays may result in
* OutOfMemoryError: Requested array size exceeds VM limit
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;