title: Scala recursion depth differences and StackOverflowError below allowed depth
body: <p>The following program was written to check how deep recursion in Scala can go deep on my machine. I assume that it mostly depends on stack's size assigned to this program. However, after calculating the maximal depth of recursion (catching exception when it appears) and trying to simulate a recursion of this depth I got odd outputs.</p>
  
  <pre><code>object RecursionTest {
  
    def sumArrayRec(elems: Array[Int]) = {
  
      def goFrom(i: Int, size: Int, elms: Array[Int]): Int = {
        if (i &lt; size) elms(i) + goFrom(i + 1, size, elms)
        else 0
      }
      goFrom(0, elems.length, elems)
    }
  
    def main(args: Array[String]) {
      val recDepth = recurseTest(0, 0, Array(1))
      println("sumArrayRec = " + sumArrayRec((0 until recDepth).toArray))
    }
  
    def recurseTest(i: Int, j: Int, arr: Array[Int]): Int = {
      try {
        recurseTest(i + 1, j + 1, arr)
      } catch { case e: java.lang.StackOverflowError =&gt;
        println("Recursion depth on this system is " + i + ".")
        i
      }
    }
  
  }
  </code></pre>
  
  <p>The results vary among executions of the program.</p>
  
  <p>One of them is a desirable output:</p>
  
  <pre><code>/usr/lib/jvm/java-8-oracle/bin/java
  
  Recursion depth on this system is 7166.
  sumArrayRec = 25672195
  
  Process finished with exit code 0
  </code></pre>
  
  <p>Nevertheless, the second possible output which I get indicates an error:</p>
  
  <pre><code>/usr/lib/jvm/java-8-oracle/bin/java
  
  Recursion depth on this system is 8129.
  Exception in thread "main" java.lang.StackOverflowError
      at RecursionTest$.goFrom$1(lab44.scala:6)
      at RecursionTest$.goFrom$1(lab44.scala:6)
      at RecursionTest$.goFrom$1(lab44.scala:6)
      (...)
      at RecursionTest$.goFrom$1(lab44.scala:6)
      at RecursionTest$.goFrom$1(lab44.scala:6)
  
  Process finished with exit code 1
  </code></pre>
  
  <p>I didn't observe any dependence or relationship of them, I just once got the first, and other time the second</p>
  
  <p>All of that lead me to the following questions:</p>
  
  <ol>
  <li>Why do I get an error and what causes it?</li>
  <li>Is this the stack overflow error and if yes...</li>
  <li>Why the size of stack changes in the same time when the program is running? Is it even possible?</li>
  </ol>
  
  <p>When I changed <code>println("sumArrayRec = " + sumArrayRec((0 until recDepth).toArray))</code> into 
  <code>println("sumArrayRec = " + sumArrayRec((0 until recDepth - 5000).toArray))</code>, so much less, the behaviour remains the same.</p>