title: Stack size under Mono
body: <p>I have written a tiny recursive bit of F# code to see how many levels of recursion I can fit onto the stack under .NET/Mono. It just prints the recursion depth whenever it is an exact power of 2, so I find out the maximum depth to within a factor 2.</p>
  
  <p>I start the code in a thread with a defined amount of stack space using <code>System.Threading.Thread (ThreadStart, int)</code>. Under .Net it seems to take approx 100 bytes per level of recursion, and I can get about 16 million levels on a 2G stack. The memory usage is broadly similar under Mono, however I can only get about 30 thousand levels. Increasing the stack size value passed to <code>Thread</code> past over about <code>600000</code> does not increase the recursion depth. </p>
  
  <p><code>ulimit</code> reports the stack size limit is 1G.</p>
  
  <p>An obvious explanation is that Mono will not obey the second argument of <code>Thread</code> if it is too large. Does anybody please know how to convince Mono to allocate a large stack?</p>
  
  <p>The code is trivial, but it's below just in case someone cares:</p>
  
  <pre><code>let rec f i =
      if popcount i = 1 then // population count is one on exact powers of 2
          printf "Got up to %d\n" i
          stdout.Flush ()
      if i = 1000000000 then 0 else 1 + f (i+1)
  </code></pre>