Results 1 to 2 of 2
Thread: problem with Java Vm options
- 11-21-2007, 06:02 AM #1
Member
- Join Date
- Nov 2007
- Posts
- 3
- Rep Power
- 0
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?
- 11-21-2007, 03:22 PM #2
Member
- Join Date
- Nov 2007
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Opening frame/page options
By Java Tip in forum Java TipReplies: 0Last Post: 03-10-2008, 02:50 PM -
JOptionPane - showConfirmDialog(...) options
By Java Tip in forum Java TipReplies: 0Last Post: 12-17-2007, 09:38 AM -
ComboBox with database options
By Goldy in forum Advanced JavaReplies: 0Last Post: 12-01-2007, 09:43 PM -
How to set General options in NetBeans IDE
By JavaForums in forum NetBeansReplies: 0Last Post: 08-02-2007, 12:11 PM -
java SE 6 problem
By techlance in forum Java AppletsReplies: 1Last Post: 06-28-2007, 10:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks