When I remove the for-loop I get an OutOfMemoryError
. When I use for-loop I don't get any error.
Can anyone help me to understand this behavior?
public class JavaMemoryPuzzlePolite {
private final int dataSize = (int) (Runtime.getRuntime().maxMemory() * 0.6);
public void f() {
{
System.out.println(dataSize);
byte[] data = new byte[dataSize];
}
for (int i = 0; i < 1; i++) {
System.out.println("Please be so kind and release memory");
}
System.out.println(dataSize);
byte[] data2 = new byte[dataSize];
}
public static void main(String[] args) {
JavaMemoryPuzzlePolite jmp = new JavaMemoryPuzzlePolite();
jmp.f();
}
}