Technically the limit is Integer.MAX_VALUE
characters, however, some JVMs can't actually create an array of length Integer.MAX_VALUE
so you can find a practical limit is a few characters less.
public static void main(String[] args) {
for (int i = 0; i < 1000; i++) {
try {
char[] chars = new char[Integer.MAX_VALUE - i];
System.out.println("Created new char[MAX_VALUE-" + i + "]");
break;
} catch (OutOfMemoryError oome) {
if (i == 0)
oome.printStackTrace();
}
}
}
on my Windows 10 Java 8 update 251 machine prints
java.lang.OutOfMemoryError: Requested array size exceeds VM limit
at A.main(A.java:12)
Created new char[MAX_VALUE-2]