title: How to increase the Java stack size?
body: <p>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 relevant to how Java handles the situation where a large runtime stack is needed. I've extended my question with the summary of the responses.</p>
  
  <p>Originally I wanted to increase the JVM stack size so programs like runs without a <code>StackOverflowError</code>.</p>
  
  <pre><code>public class TT {
    public static long fact(int n) {
      return n &lt; 2 ? 1 : n * fact(n - 1);
    }
    public static void main(String[] args) {
      System.out.println(fact(1 &lt;&lt; 15));
    }
  }
  </code></pre>
  
  <p>The corresponding configuration setting is the <code>java -Xss...</code> command-line flag with a large enough value. For the program <code>TT</code> above, it works like this with OpenJDK's JVM:</p>
  
  <pre><code>$ javac TT.java
  $ java -Xss4m TT
  </code></pre>
  
  <p>One of the answers has also pointed out that the <code>-X...</code> flags are implementation dependent. I was using</p>
  
  <pre><code>java version "1.6.0_18"
  OpenJDK Runtime Environment (IcedTea6 1.8.1) (6b18-1.8.1-0ubuntu1~8.04.3)
  OpenJDK 64-Bit Server VM (build 16.0-b13, mixed mode)
  </code></pre>
  
  <p>It is also possible to specify a large stack only for one thread (see in one of the answers how). This is recommended over <code>java -Xss...</code> to avoid wasting memory for threads that don't need it.</p>
  
  <p>I was curious how large a stack the program above exactly needs, so I've run it <code>n</code> increased:</p>
  
  <ul>
  <li>-Xss4m can be enough for <code>fact(1 &lt;&lt; 15)</code></li>
  <li>-Xss5m can be enough for <code>fact(1 &lt;&lt; 17)</code></li>
  <li>-Xss7m can be enough for <code>fact(1 &lt;&lt; 18)</code></li>
  <li>-Xss9m can be enough for <code>fact(1 &lt;&lt; 19)</code></li>
  <li>-Xss18m can be enough for <code>fact(1 &lt;&lt; 20)</code></li>
  <li>-Xss35m can be enough for <code>fact(1 &lt;&lt; 21)</code></li>
  <li>-Xss68m can be enough for <code>fact(1 &lt;&lt; 22)</code></li>
  <li>-Xss129m can be enough for <code>fact(1 &lt;&lt; 23)</code></li>
  <li>-Xss258m can be enough for <code>fact(1 &lt;&lt; 24)</code></li>
  <li>-Xss515m can be enough for <code>fact(1 &lt;&lt; 25)</code></li>
  </ul>
  
  <p>From the numbers above it seems that Java is using about 16 bytes per stack frame for the function above, which is reasonable.</p>
  
  <p>The enumeration above contains <em>can be enough</em> instead of <em>is enough</em>, because the stack requirement is not deterministic: running it multiple times with the same source file and the same <code>-Xss...</code> sometimes succeeds and sometimes yields a <code>StackOverflowError</code>. E.g. for 1 &lt;&lt; 20, <code>-Xss18m</code> was enough in 7 runs out of 10, and <code>-Xss19m</code> wasn't always enough either, but <code>-Xss20m</code> was enough (in all 100 runs out of 100). Does garbage collection, the JIT kicking in, or something else cause this nondeterministic behavior?</p>
  
  <p>The stack trace printed at a <code>StackOverflowError</code> (and possibly at other exceptions as well) shows only the most recent 1024 elements of the runtime stack. An answer below demonstrates how to count the exact depth reached (which might be a lot larger than 1024).</p>
  
  <p>Many people who responded has pointed out that it is a good and safe coding practice to consider alternative, less stack-hungry implementations of the same algorithm. In general, it is possible to convert to a set of recursive functions to iterative functions (using a e.g. <code>Stack</code> object, which gets populated on the heap instead of on the runtime stack). For this particular <code>fact</code> function, it is quite easy to convert it. My iterative version would look like:</p>
  
  <pre><code>public class TTIterative {
    public static long fact(int n) {
      if (n &lt; 2) return 1;
      if (n &gt; 65) return 0;  // Enough powers of 2 in the product to make it (long)0.
      long f = 2;
      for (int i = 3; i &lt;= n; ++i) {
        f *= i;
      }
      return f;
    }
    public static void main(String[] args) {
      System.out.println(fact(1 &lt;&lt; 15));
    }
  }
  </code></pre>
  
  <p>FYI, as the iterative solution above shows it, the <code>fact</code> function cannot compute the exact factorial of numbers above 65 (actually, even above 20), because the Java built-in type <code>long</code> would overflow. Refactoring <code>fact</code> so it would return a <code>BigInteger</code> instead of <code>long</code> would yield exact results for large inputs as well.</p>