I have following code:
public static void main(String[] args) {
for(int i=0;i<10;i++) {
System.out.println(i);
byte[] b = new byte[1024*1024*5];
}
}
You see each operation allocates 5M. When I set -Xms8M -Xmx8M
, it runs successfully without exception, while when -Xms7M -Xmx7M
, it throws OutOfMemoryError
exception. Could someone explain why? I'm under Windows 7, 64bit, Eclipse 4.3.
Following code is same result:
public static void main(String[] args) {
byte[] b;
for(int i=0;i<10;i++) {
System.out.println(i);
b = new byte[1024*1024*5];
}
}
You mistakenly assume that only 5MB of memory are allocated by that program, and you're wrong. Just because you create a 5MB array doesn't mean there isn't additional memory used for classloading and other things.
So to answer your question, you're allocating too little memory for your process.