title: Why is the max recursion depth I can reach non-deterministic?
body: <p>I decided to try a few experiments to see what I could discover about the size of stack frames, and how far through the stack the currently executing code was. There are two interesting questions we might investigate here:</p>
  
  <ol>
  <li>How many levels deep into the stack is the current code?</li>
  <li>How many levels of recursion can the current method reach before it hits a <code>StackOverflowError</code>?</li>
  </ol>
  
  <h1>Stack depth of currently executing code</h1>
  
  <p>Here's the best I could come up with for this:</p>
  
  <pre><code>public static int levelsDeep() {
      try {
          throw new SomeKindOfException();
      } catch (SomeKindOfException e) {
          return e.getStackTrace().length;
      }
  }
  </code></pre>
  
  <p>This seems a bit hacky. It generates and catches an exception, and then looks to see what the length of the stack trace is.</p>
  
  <p>Unfortunately it also seems to have a fatal limitation, which is that the maximum length of the stack trace returned is 1024. Anything beyond that gets axed, so the maximum that this method can return is 1024.</p>
  
  <blockquote>
    <p><h2>Question:</h2> Is there a better way of doing this that isn't so hacky and doesn't have this limitation?</p>
  </blockquote>
  
  <p>For what it's worth, my guess is that there isn't: <code>Throwable.getStackTraceDepth()</code> is a native call, which suggests (but doesn't prove) that it can't be done in pure Java.</p>
  
  <h1>Determining how much more recursion depth we have left</h1>
  
  <p>The number of levels we can reach will be determined by (a) size of stack frame, and (b) amount of stack left. Let's not worry about size of stack frame, and just see how many levels we can reach before we hit a <code>StackOverflowError</code>.</p>
  
  <p>Here's my code for doing this:</p>
  
  <pre><code>public static int stackLeft() {
      try {
          return 1+stackLeft();
      } catch (StackOverflowError e) {
          return 0;
      }
  }
  </code></pre>
  
  <p>It does its job admirably, even if it's linear in the amount of stack remaining. But here is the very, very weird part. On 64-bit Java 7 (OpenJDK 1.7.0_65), the results are perfectly consistent: 9,923, on my machine (Ubuntu 14.04 64-bit). But Oracle's Java 8 (1.8.0_25) gives me <strong>non-deterministic results</strong>: I get a recorded depth of anywhere between about 18,500 and 20,700.</p>
  
  <p>Now why on earth would it be non-deterministic? There's supposed to be a fixed stack size, isn't there? And all of the code looks deterministic to me.</p>
  
  <p>I wondered whether it was something weird with the error trapping, so I tried this instead:</p>
  
  <pre><code>public static long badSum(int n) {
      if (n==0)
          return 0;
      else
          return 1+badSum(n-1);
  }
  </code></pre>
  
  <p>Clearly this will either return the input it was given, or overflow.</p>
  
  <p>Again, the results I get are non-deterministic on Java 8. If I call <code>badSum(14500)</code>, it will give me a <code>StackOverflowError</code> about half the time, and return 14500 the other half. but on Java 7 OpenJDK, it's consistent: <code>badSum(9160)</code> completes fine, and <code>badSum(9161)</code> overflows.</p>
  
  <blockquote>
    <p><h2>Question:</h2> Why is the maximum recursion depth non-deterministic on Oracle's Java 8? And why is it deterministic on OpenJDK 7?</p>
  </blockquote>