When does StackOverflowError occur?

This question already has an answer here:

  • What is the maximum depth of the java call stack? 4 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.


    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


    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.

    链接地址: http://www.djcxy.com/p/80460.html

    上一篇: >堆栈溢出错误

    下一篇: 什么时候发生StackOverflowError?