How to increase the Java stack size?

后端 未结 9 1634
余生分开走
余生分开走 2020-11-22 01:59

I asked this question to get to know how to increase the runtime call stack size in the JVM. I\'ve got an answer to this, and I\'ve also got many useful answers and comments

9条回答
  •  情话喂你
    2020-11-22 02:06

    I assume you calculated the "depth of 1024" by the recurring lines in the stack trace?

    Obviously, the stack trace array length in Throwable seems to be limited to 1024. Try the following program:

    public class Test {
    
        public static void main(String[] args) {
    
            try {
                System.out.println(fact(1 << 15));
            }
            catch (StackOverflowError e) {
                System.err.println("true recursion level was " + level);
                System.err.println("reported recursion level was " +
                                   e.getStackTrace().length);
            }
        }
    
        private static int level = 0;
        public static long fact(int n) {
            level++;
            return n < 2 ? n : n * fact(n - 1);
        }
    }
    

提交回复
热议问题