views:

2792

answers:

5

I am running a program that I've written in Java in Eclipse. The program has a very deep level of recursion for very large inputs. For smaller inputs the program runs fine however when large inputs are given, I get the following error:

Exception in thread "main" java.lang.StackOverflowError

Can this be solved by increasing the Java stack size and if so, how do I do this in Eclipse?

Update:

@Jon Skeet

The code is traversing a parse tree recursively in order to build up a datastructure. So, for example the code will do some work using a node in the parse tree and call itself on the node's two children, combining their results to give the overall result for the tree.

The total depth of the recursion depends on the size of the parse tree but the code seems to fail (without a larger stack) when the number of recursive calls gets into the 1000s.

Also I'm pretty sure the code isn't failing because of a bug as it works for small inputs.

+1  A: 

Add the flag -Xss1024k in the VM Arguments.

You can also increase stack size in mb by using -Xss1m for example .

Gordon
+10  A: 

It may be curable by increasing the stack size - but a better solution would be to work out how to avoid recursing so much. A recursive solution can always be converted to an iterative solution - which will make your code scale to larger inputs much more cleanly. Otherwise you'll really be guessing at how much stack to provide, which may not even be obvious from the input.

Are you absolutely sure it's failing due to the size of the input rather than a bug in the code, by the way? Just how deep is this recursion?

EDIT: Okay, having seen the update, I would personally try to rewrite it to avoid using recursion. Generally having a Stack<T> of "things still do to" is a good starting point to remove recursion.

Jon Skeet
..or by tail-recursion.
BalusC
I can't remember the state of tail recursion on jvms. Cue comment from tackline.
Jon Skeet
JVM's do not yet as a rule optimize tail recursions. I believe this is one of the things that the generalization of the JVM to non-Java languages is to fix.
Thorbjørn Ravn Andersen
I've updated the question in response to Jon Skeet's questions.
tree-hacker
A: 

You can set that in Eclipse.ini file as well which is in the Eclipse Directory for EX : -vmargs -Dosgi.requiredJavaVersion=1.5 -Xms40m -Xmx512m

GK
That doesn't work in tree-hackers case, applications that are started from the eclipse IDE neither run in the same JVM nor inherit these settings. You need to edit the run configurations for each applications. (-1 because it doesn't solve the problem)
Andreas_D
Nor are these the correct arguments anyway - the given arguments modify the *heap* size, not the stack size.
Andrzej Doyle
+1  A: 

You need to have a launch configuration inside Eclipse in order to adjust the JVM parameters.

After running your program with either F11 or Ctrl-F11, open the launch configurations in Run -> Run Configurations... and open your program under "Java Applications". Select the Arguments pane, where you will find "VM arguments".

This is where -Xss1024k goes.

If you want the launch configuration to be a file in your workspace (so you can right click and run it), select the Common pane, and check the Save as -> Shared File checkbox and browse to the location you want the launch file. I usually have them in a separate folder, as we check them into CVS.

Thorbjørn Ravn Andersen
+7  A: 

Open the Run Configuration for your application (Run/Run Configurations..., then look for the applications entry in 'Java application').

The arguments tab has a text box Vm arguments, enter -Xss1m (or a bigger parameter for the maximum heap size). The default value is 512 kByte (SUN JDK 1.5 - don't know if it varies between vendors and versions).

Andreas_D
Be aware of this issue: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6316197
Jim Rush