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

8

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.

Aerobiology answered 24/1, 2010 at 13:35 Comment(3)
something wrong here... stackoverflow(.com) is not an error! :-)Ciapha
@CarlosHeuberger you are funny bro!Net
@Net 9 years ago I was a kid - not really [:-)Ciapha
Y
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).

Yowl answered 24/1, 2010 at 13:52 Comment(1)
Be aware of this issue: bugs.sun.com/bugdatabase/view_bug.do?bug_id=6316197Vendetta
G
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.

Geodynamics answered 24/1, 2010 at 13:40 Comment(5)
I can't remember the state of tail recursion on jvms. Cue comment from tackline.Geodynamics
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.Tactile
I've updated the question in response to Jon Skeet's questions.Aerobiology
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.Sternwheeler
@Sternwheeler The heap is better because it is larger. Why is the stack so small is answered here: 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.Adrieneadrienne
U
12

Add the flag -Xss1024k in the VM Arguments.

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

Urga answered 24/1, 2010 at 13:39 Comment(0)
T
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.

Toxemia answered 9/3, 2011 at 7:43 Comment(0)
O
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.

Oxen answered 17/7, 2013 at 8:57 Comment(0)
S
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.

Schwitzer answered 24/1, 2010 at 13:51 Comment(0)
C
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).

Collectivism answered 27/2, 2015 at 10:53 Comment(0)
O
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

Ollie answered 4/9, 2020 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.