62

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.

Ether
  • 53,118
  • 13
  • 86
  • 159
tree-hacker
  • 5,351
  • 9
  • 38
  • 39

8 Answers8

83

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 stack size). The default value is 512 kByte (SUN JDK 1.5 - don't know if it varies between vendors and versions).

Tvaroh
  • 6,645
  • 4
  • 51
  • 55
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
38

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I can't remember the state of tail recursion on jvms. Cue comment from tackline. – Jon Skeet Jan 24 '10 at 14:08
  • 1
    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 Jan 24 '10 at 15:01
  • I've updated the question in response to Jon Skeet's questions. – tree-hacker Jan 25 '10 at 14:07
  • 2
    Basically stack space is memory just like heap. Why is it better to use heap? It depends on your application and the way you solve your problems. There are situations where memory management is more efficient using the stack instead of the heap. There are also situations where stack-based algorithms are more concise; for example parsers. – ziggystar Mar 18 '12 at 12:21
  • @ziggystar The heap is better because it is larger. Why is the stack so small is answered here: http://stackoverflow.com/questions/10482974/. I agree that the recursion is often more concise and clearer. But when one cannot assume any limit on the depth of the recursion, it is safer to "de-recursify" as Jon Skeet suggests. After all, the stack as a data structure in a programming language and the stack as the part of the memory are called the same for a reason. – pepan Oct 11 '13 at 16:01
11

Add the flag -Xss1024k in the VM Arguments.

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

Gordon
  • 4,823
  • 4
  • 38
  • 55
5

i also have the same problem while parsing schema definition files(XSD) using XSOM library,

i was able to increase Stack memory upto 208Mb then it showed heap_out_of_memory_error for which i was able to increase only upto 320mb.

the final configuration was -Xmx320m -Xss208m but then again it ran for some time and failed.

My function prints recursively the entire tree of the schema definition,amazingly the output file crossed 820Mb for a definition file of 4 Mb(Aixm library) which in turn uses 50 Mb of schema definition library(ISO gml).

with that I am convinced I have to avoid Recursion and then start iteration and some other way of representing the output, but I am having little trouble converting all that recursion to iteration.

ElderMael
  • 7,000
  • 5
  • 34
  • 53
Ratnakar
  • 59
  • 1
  • 1
4

When the argument -Xss doesn't do the job try deleting the temporary files from:

c:\Users\{user}\AppData\Local\Temp\.

This did the trick for me.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
ticu0000
  • 41
  • 1
3

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
  • 73,784
  • 33
  • 194
  • 347
0

Look at Morris in-order tree traversal which uses constant space and runs in O(n) (up to 3 times longer than your normal recursive traversal - but you save hugely on space). If the nodes are modifiable, than you could save the calculated result of the sub-tree as you backtrack to its root (by writing directly to the Node).

Maria Sakharova
  • 1,389
  • 15
  • 15
0

When using JBOSS Server, double click on the server:

enter image description here

Go to "Open Launch Configuration"

enter image description here

Then change min and max memory sizes (like 1G, 1m):

enter image description here

Matheus Santz
  • 538
  • 6
  • 7