Java stack overflow error - how to increase the stack size in Eclipse?

JavaEclipseJvmStack OverflowJvm Arguments

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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).

Solution 2 - Java

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.

Solution 3 - Java

Add the flag -Xss1024k in the VM Arguments.

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

Solution 4 - Java

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.

Solution 5 - Java

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.

Solution 6 - Java

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.

Solution 7 - Java

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).

Solution 8 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiontree-hackerView Question on Stackoverflow
Solution 1 - JavaAndreas DolkView Answer on Stackoverflow
Solution 2 - JavaJon SkeetView Answer on Stackoverflow
Solution 3 - JavaGordonView Answer on Stackoverflow
Solution 4 - JavaRatnakarView Answer on Stackoverflow
Solution 5 - JavaThorbjørn Ravn AndersenView Answer on Stackoverflow
Solution 6 - Javaticu0000View Answer on Stackoverflow
Solution 7 - JavaMaria SakharovaView Answer on Stackoverflow
Solution 8 - JavaMatheus SantzView Answer on Stackoverflow