Search code examples
javaeclipseout-of-memoryheap-memorystack-overflow

heap and stack in eclipse


I have written a code in Eclipse which runs properly for small input values but as soon as my test cases increase in size, i get OutOfMemoryException or StackOverFlow error.

i tried to use eclipse.exe -vmargs -Xmx1g to make my heap go out to 1G but i still get the same error. and When i try 2G it says unable to start JVM.

so i m wondering if there is any ways at all for me to run this code. any help would be appreciated. thanks in advance.

EDIT: this is where my heaps overflows. the input sample is too huge and causes the momory problem.

 while ((line = br.readLine()) != null) {

        String[] linevalue= (line.trim().split("\\s+"));
        int l= linevalue.length;
        dg.addNode(Long.parseLong(linevalue[0]));
        dg.addNode(Long.parseLong(linevalue[1]));
        dg.addEdge(Long.parseLong(linevalue[0]), Long.parseLong(linevalue[1]));

    }

In the other class the following code is present, here mGraph is a HashMap.

public boolean addNode(T node) {
    /* If the node already exists, don't do anything. */
    if (mGraph.containsKey(node))
        return false;

    /* Otherwise, add the node with an empty set of outgoing edges. */
    mGraph.put(node, new HashSet<T>());
    return true;
}


public void addEdge(T start, T dest) {
    /* Confirm both endpoints exist. */
    if (!mGraph.containsKey(start) || !mGraph.containsKey(dest))
        throw new NoSuchElementException("Both nodes must be in the graph.");

    /* Add the edge. */
    mGraph.get(start).add(dest);
}

Solution

  • In Eclipse, you can set the size of the VM when you execute your code.

    Go to Run > Run configurations. Then in the tab Arguments, put -Xms1000m under VM arguments.