Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 974
tree-hacker Avatar asked Jan 24 '10 13:01

tree-hacker


People also ask

How do I fix stackoverflow error in eclipse?

The simplest solution is to carefully inspect the stack trace and detect the repeating pattern of line numbers. These line numbers indicate the code being recursively called. Once you detect these lines, you must carefully inspect your code and understand why the recursion never terminates.

Can we increase stack size in Java?

To increase the stack size, define the new value in the -Xss setting in the JAVA_OPTS entry in your Tomcat start script file (/opt/netiq/idm/apps/tomcat/bin/setenv.sh or C:\NetIQ\idm\apps\tomcat\bin\setenv. bat). For example, to set the stack size to 4M, change the setting to -Xss4M.

How do I fix stack overflow errors?

Increase Thread Stack Size (-Xss) lang. StackOverflowError , the thread stack size can be increased to allow a larger number of invocations. Increasing the stack size can be useful, for example, when the program involves calling a large number of methods or using lots of local variables.

Can we change the stack size?

You can do this by using the "ulimit -s" command in linux which will set the stack size for all process executed under that shell. Incase of windows, the same can be done in VC6 for that project by setting the stack size in Project Properties->link options->output->stack allocations->reserve.


1 Answers

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

like image 117
Andreas Dolk Avatar answered Sep 20 '22 02:09

Andreas Dolk