8

According to Oracle, a StackOverflowError is:

Thrown when a stack overflow occurs because an application recurses too deeply.

I know what recursion is and normally recursive functions, if not terminated properly, lead to StackOverflowError. To check the number of recursive calls that happen before StackOverflowError is thrown, I wrote this code:

package ErrorCases;

public class StackOverFlowError {
static int i=0;
void a()
{

    //System.out.println("called "+(++i));
    try{
        ++i;
    a();
    }catch(Error e)
    {
        System.out.println(e.getClass());
        System.out.println(i);
    }
}

public static void main(String[] args) {

       new StackOverFlowError().a();

   }

}

the value of i gives the count of recursive calls to a() before JVM threw StackOverflowError.
The value of i is different in every run like:

output 1: class java.lang.StackOverflowError
           10466
Output 2: class java.lang.StackOverflowError
           10470

My query is ?

  1. How deep the recursion has to happen before JVM throws StackOverflowError?

  2. Can we recover once a StackOverflowError has been thrown?

2
  • Recovery from Stack Overflow requires total abstinence -- oops, I mean is pretty iffy. If the code was engaged in some complex operation at the time there may not be enough stack space to allow it to back out. And a lot of in-progress operations may not protect against such a failure and will fail to back out/reset at all.
    – Hot Licks
    Dec 18, 2013 at 12:58
  • 1
    best question i read in a long time. made me think! thanks Dec 18, 2013 at 13:07

4 Answers 4

6

The depth depends on two things:

1: The size of the stack.

2: The amount of stack space used in each recursion.

Function parameters, local variables and the return address are all allocated on the stack while objects are allocated on the heap.

Recovery

It is possible to recover.

try {
    myDeepRecursion();
} catch (StackOverflowError e) {
  // We are back from deep recursion. Stack should be ok again.
}

However, note the following regarding errors (from the java API doc):

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.

EDIT:

A note of caution: While catching exceptions inside a recursive function is ok, don't try to catch errors. If the stack is full, then the error handling will cause new errors. Simple things, such as a call to System.out.println() will fail because there is no room left on the stack for the return address.

That is why errors should be caught outside the recursive function.

7
  • Can we control the depth?
    – Aman Arora
    Dec 18, 2013 at 12:39
  • You can use the jvm option -Xss like explained here Dec 18, 2013 at 12:50
  • stackoverflow.com/questions/3700459/…
    – Amit G
    Dec 18, 2013 at 12:50
  • Not from the outside. There is no "maxRecursionDepth" parameter to give the JVM. You can, however, specify the stack size with the -Xss JVM parameter. Dec 18, 2013 at 12:50
  • 1
    Added an edit in my answer. Dec 18, 2013 at 12:58
3

How deep the recursion has to happen before JVM throws StackOverflowError?

It really depends on the machine you're working with and your JVM and its configuration. (it's 6000 - 8500 on my current setup with a lot of tasks in the background) and at most, the this you do in the application and the recursive method.

Can we recover once a StackOverflowError has been thrown?

No! I Java throws an error, there is no way back to normal. That is the main difference between Exceptions and Errors.

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch

Read more about Errors here

Read more about Java stack size myths here

Edit:

You can still do things after an error occurs, but does it make sense? You have a serious error in your code! You can't be sure that everything works correctly!

Your code is not terminating because you call the method again which is the same like an endless loop

5
  • They why the output shows different values every time i run it ? I am running on the same machine
    – Aman Arora
    Dec 18, 2013 at 12:36
  • basicly yes. a computer is a working machine and has more or less stuff to do Dec 18, 2013 at 12:38
  • in the catch(Error e), call other functions, If stack is full, how it is working?
    – Aman Arora
    Dec 18, 2013 at 12:46
  • Please check the second code . Its not terminating. How it id getting stack to print ?
    – Aman Arora
    Dec 18, 2013 at 13:10
  • it prints the stack, because the programm is still running (in an unstable condition). it is not terminating because calling b() in b() is an endless loop ;) Dec 18, 2013 at 13:13
2

The reason why you may see different stack depths is that the stack frames are not necessarily the same size. The Hotspot JVM for example has different stack frames for JIT compiled code and for interpreted code. The JIT compiler works in parallel with the running code, so external factors like load on the machine may have an effect on when / if the JVM starts using JIT stack frames.

One thing that you should keep in mind is the fact that when you get a StackOverflowError, almost anything that you do may cause another StackOverflowError. For example printing the name of the error class in the catch part may also run out of stack space, so another StackOverflowError will be thrown down the stack.

Some people claim that you cannot recover from an Error because the JVM is shutting down. That is not correct; you can recover from some errors. What's tricky about StackOverflowError and OutOfMemoryError is that once you catch one you have absolutely no guarantees of the state of the program; for example a key data structure you use might be in an inconsistent state. This makes recovery hard or impossible in general.

2
  • Thanks for the answer, if StackOverflowError means "no stack left"? why am i able to call function b() from inside the catch()?
    – Aman Arora
    Dec 18, 2013 at 13:05
  • 1
    When the error is thrown, the topmost frame is removed from the stack and the error is handled by the catch block in the calling method. If there's not enough stack space another StackOverflowError is thrown and another frame is removed. This process continues until so many frames have been removed that the catch block can complete without an error being thrown.
    – Joni
    Dec 18, 2013 at 13:10
1

Parameters and local variables are allocated on the stack (with reference types the object lives on the heap and a variable references that object). The stack typically lives at the upper end of your address space and as it is used up it heads towards the bottom of the address space (ie towards zero).

Your process also has a heap, which lives at the bottom end of your process. As you allocate memory this heap can grow towards the upper end of your address space. As you can see, there is the potential for the heap to "collide" with the stack (a bit like techtonic plates!!!). [source]

So the StackOIverflowError will depend on your stack size as well as your heap size which will depend from execution to execution because of lot of factors like GC.

Also as the name says it is Error and not Exception. There is no recovery from it. JVM will shut down.

5
  • Thanks, But if the JVM shuts down, then how can we catch it and run code ?
    – Aman Arora
    Dec 18, 2013 at 12:52
  • The JVM will only shut down if the Error is not caught.
    – Hot Licks
    Dec 18, 2013 at 13:00
  • That is same for the Exception too, then.In that sense, Exception if not handled, will cause JVM crash.
    – Aman Arora
    Dec 18, 2013 at 13:06
  • But catched Exceptions are supposed to be handled, uncatched Exceptions in some case where as Errors in no case at all. Dec 18, 2013 at 13:59
  • If you catch a Throwable before it "bubbles out" all the way, it will not take down the JVM. If you do not catch it, it will. This is true for all Throwables, be they Exceptions or Errors. The difference is in what one is supposed to do with them, which always needs to be taken with a grain of salt.
    – Hot Licks
    Dec 18, 2013 at 16:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.