How to predict the maximum call depth of a recursive method?

后端 未结 6 1302
太阳男子
太阳男子 2020-12-12 23:39

For the purposes of estimating the maximum call depth a recursive method may achieve with a given amount of memory, what is the (approximate) formula for calculating the mem

相关标签:
6条回答
  • 2020-12-13 00:15

    The answer is, it all depends.

    First of all, the Java Stack size can be changed.

    Second of all, the stack frame size of a given method can vary based on different variables. You can look at the Call Stack Frame Size section of Call stack - Wikipedia for more details.

    0 讨论(0)
  • 2020-12-13 00:15

    depends on your system-architecture (32 or 64 bit addresses), the amount of local variables and parameters of the method. If it is tail-recursive no overhead, if the compiler optimize it to a loop.

    You see, no simple answer.

    0 讨论(0)
  • 2020-12-13 00:27

    You can take the empirical road and experiment with your code and the -Xss setting. See here for more: JVM option -Xss - What does it do exactly?

    0 讨论(0)
  • 2020-12-13 00:32

    This is clearly JVM- and possibly also architecture-specific.

    I've measured the following:

      static int i = 0;
      public static void rec0() {
          i++;
          rec0();
      }
    
      public static void main(String[] args) {
          ...
          try {
              i = 0; rec0();
          } catch (StackOverflowError e) {
              System.out.println(i);
          }
          ...
      }
    

    using

    Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
    Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
    

    running on x86.

    With a 20MB Java stack (-Xss20m), the amortized cost fluctuated around 16-17 bytes per call. The lowest I've seen was 16.15 bytes/frame. I therefore conclude that the cost is 16 bytes and the rest is other (fixed) overhead.

    A function that takes a single int has basically the same cost, 16 bytes/frame.

    Interestingly, a function that takes ten ints requires 32 bytes/frame. I am not sure why the cost is so low.

    The above results apply after the code's been JIT compiled. Prior to compilation the per-frame cost is much, much higher. I haven't yet figured out a way to estimate it reliably. However, this does mean that you have no hope of reliably predicting maximum recursion depth until you can reliably predict whether the recursive function has been JIT compiled.

    All of this was tested with a ulimit stack sizes of 128K and 8MB. The results were the same in both cases.

    0 讨论(0)
  • 2020-12-13 00:33

    Only a partial answer: from JVM Spec 7, 2.5.2, stack frames can be allocated on the heap, and the stack size may be dynamic. I couldn't say for certain, but it seems it should be possible to have your stack size bounded only by your heap size:

    Because the Java virtual machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated.

    and

    This specification permits Java virtual machine stacks either to be of a fixed size or to dynamically expand and contract as required by the computation. If the Java virtual machine stacks are of a fixed size, the size of each Java virtual machine stack may be chosen independently when that stack is created.

    A Java virtual machine implementation may provide the programmer or the user control over the initial size of Java virtual machine stacks, as well as, in the case of dynamically expanding or contracting Java virtual machine stacks, control over the maximum and minimum sizes.

    So it'll be up to the JVM implementation.

    0 讨论(0)
  • 2020-12-13 00:34

    Addig to NPEs answer:

    The maximum stack depth seems to be flexible. The following test program prints vastly different numbers:

    public class StackDepthTest {
        static int i = 0;
    
        public static void main(String[] args) throws Throwable {
            for(int i=0; i<10; ++i){
                testInstance();
            }
        }
    
        public static void testInstance() {
            StackDepthTest sdt = new StackDepthTest();
            try {
                i=0;
                sdt.instanceCall();
            } catch(StackOverflowError e){}
            System.out.println(i);
        }
    
        public void instanceCall(){
            ++i;
            instanceCall();
        }
    }
    

    The output is:

    10825
    10825
    59538
    59538
    59538
    59538
    59538
    59538
    59538
    59538
    

    I've used the default of this JRE:

    java version "1.7.0_09"
    OpenJDK Runtime Environment (IcedTea7 2.3.3) (7u9-2.3.3-0ubuntu1~12.04.1)
    OpenJDK 64-Bit Server VM (build 23.2-b09, mixed mode)
    

    So the conclusion is: If you pus had enough (i.e. more than twice) you get a second chance ;-)

    0 讨论(0)
提交回复
热议问题