Results 1 to 2 of 2
  1. #1
    DonnieDarko is offline Member
    Join Date
    Nov 2007
    Posts
    3
    Rep Power
    0

    Default 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?

  2. #2
    DonnieDarko is offline Member
    Join Date
    Nov 2007
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Opening frame/page options
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-10-2008, 02:50 PM
  2. JOptionPane - showConfirmDialog(...) options
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 09:38 AM
  3. ComboBox with database options
    By Goldy in forum Advanced Java
    Replies: 0
    Last Post: 12-01-2007, 09:43 PM
  4. How to set General options in NetBeans IDE
    By JavaForums in forum NetBeans
    Replies: 0
    Last Post: 08-02-2007, 12:11 PM
  5. java SE 6 problem
    By techlance in forum Java Applets
    Replies: 1
    Last Post: 06-28-2007, 10:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •