problem with Java Vm options
Hi, I am trying to compile this program to check for memory leaks.
************************************************** Main.java***************************************** **********8
import java.util.EmptyStackException;
public class Main {
static final int MAX_ELEMENTS = 25;
Object[] elements = new Object[MAX_ELEMENTS];
int size = 0;
public void push(Object o) {
elements[size++] = o;
}
public Object pop() {
if (size == 0)
{System.out.println("size is 0");return null;}
else {
Object result = elements[--size];
/* elements[size] = null; */
return result;
}
}
public static void main(String[] args) {
Main stackLeaker=new Main();
Object bigObj = null;
try {
//populating the stack
for (int i = 0; i < MAX_ELEMENTS; i++) {
bigObj = new BigObject();
System.out.println("created object no: "+(i+1));
stackLeaker.push(bigObj);
/*stackLeaker.push(new
WeakReference<BigObject>(bigObj));*/
}
//accessing the stack
for (int i = 0; i < MAX_ELEMENTS; i++) {
stackLeaker.pop();
}
//creating an extra object
Object big2 = new BigObject();
}
catch (OutOfMemoryError e) {
e.printStackTrace();
}
}
}
************************************************** ***********BigObject.java************************* *******
public class BigObject {
static final int SIZE = 10000000;
int[] numbers = null;
public BigObject() {
numbers = new int[SIZE];
}
}
************************************************** ************************************************** *************
like this :
java -Xms256m -Xmx256m Main
and I get the following output:
created object no: 1
created object no: 2
created object no: 3
created object no: 4
created object no: 5
java.lang.OutOfMemoryError: Java heap space
at BigObject.<init>(BigObject.java:5)
at Main.main(Main.java:26)
As you can see in BigObject.java that the size of BigObject is 10 Mb . Now my problem is that why does the program indicate OutofMemoryError after making just 5 objects when the heap size is 256 mb. I even tried using netbeans profiler and noted that the application was throwing this xception even when the used heap size was way less than max heap size. Where am I going wrong?