0

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);
}
trincot
  • 317,000
  • 35
  • 244
  • 286
Anurag Ramdasan
  • 4,189
  • 4
  • 31
  • 53
  • 1
    Maybe by posting the code we could help you? Even better, post an SSCCE http://sscce.org – Guillaume Polet Apr 16 '12 at 20:32
  • 1
    it's a start, but I don't see any real issue in that code. Maybe in addEdge or addNode, there is a recursion which quickly explodes? You should probably expose the whole classes. – Guillaume Polet Apr 16 '12 at 20:39
  • made some more edits for better understanding. i hope this helps. – Anurag Ramdasan Apr 16 '12 at 20:43
  • You can try increasing your stack size with -Xss option, but generally speaking, Guillaume Polet is right -- its difficult to exhaust a stack without having an uncontrolled recursion somewhere in your code. It may be a split() also, if your test lines are really-really long with a lot of white spaces. – mazaneicha Apr 16 '12 at 20:45
  • the above given code is where i get a heap error. after this i perform some recursive DFS where i obtain a stackoverflow error. – Anurag Ramdasan Apr 16 '12 at 20:48
  • So please, how large is your input, how many lines are we talking about? Millions? – Marko Topolnik Apr 16 '12 at 21:07
  • over 5 millions i guess. but thanks anyways, the problem has been solved :) – Anurag Ramdasan Apr 16 '12 at 21:11

2 Answers2

1

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.

tskuzzy
  • 35,812
  • 14
  • 73
  • 140
  • and if i need to increase my stack too for the same run, how can i achieve that? – Anurag Ramdasan Apr 16 '12 at 20:47
  • This has been addressed in this question here: http://stackoverflow.com/questions/3408633/dynamically-increasing-java-heap-space tldr: You can't. You are limited by the initial VM size. – tskuzzy Apr 16 '12 at 20:49
  • no what i actually meant was, i tried what you wrote for heap size, and it worked for me, but for the further recursive calls, my stack seemed insufficient. So my question to you was, can i set the stack size as well as the heap size at the same time for the VM in eclipse? something like -Xms1000m, -Xss256m or of that sort. – Anurag Ramdasan Apr 16 '12 at 20:52
  • i put the arguments in VM configuration as -Xms1000m -Xss64m and it worked. thanks. – Anurag Ramdasan Apr 16 '12 at 21:04
0

To follow up on tskuzzy's answer:

For heap,

-Xms -Xmx

For stack

-Xss

I would recommend 1g for Xmx and Xmx, and 8m for -Xss

Community
  • 1
  • 1
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124