Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does StackOverflowError occur? [duplicate]

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?

like image 609
Aman Arora Avatar asked Dec 18 '13 12:12

Aman Arora


People also ask

What would cause a StackOverflowError to occur in recursion?

The most-common cause of stack overflow is excessively deep or infinite recursion, in which a function calls itself so many times that the space needed to store the variables and information associated with each call is more than can fit on the stack.

What causes Java Lang StackOverflowError?

StackOverflowError is a runtime error which points to serious problems that cannot be caught by an application. The java. lang. StackOverflowError indicates that the application stack is exhausted and is usually caused by deep or infinite recursion.

Which of the following expressions is most likely to cause a StackOverflowError?

The most common cause of StackOverFlowError is excessively deep or infinite recursion.

What does StackOverflowError mean?

A stack overflow is a type of buffer overflow error that occurs when a computer program tries to use more memory space in the call stack than has been allocated to that stack.


1 Answers

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.

like image 194
Klas Lindbäck Avatar answered Oct 10 '22 07:10

Klas Lindbäck