public class Bad {
public static void main(String[] args) {
Integer[] buff = new Integer[5000000];
int i = 0;
while (true) {
i++;
if (i == buff.length)
i = 0;
Integer obj = new Integer(i); // line 14
buff[i] = obj;
// do something useful with buff[i];
}
}
}
terminated unexpectedly after several seconds and the following message was printed at the command line: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at exam.Bad.main(Bad.java:14)
Can anyone explain what went wrong, and please provide me with code to fix the problem?