title: How can I measure thread stack depth?
body: <p>I have a 32-bit Java service with scalability problems: with high user count we run out of memory because of excessive thread count. In the long term, I plan to switch to 64-bit and to reduce the threads-per-user ratio. In the short term, I'd like to reduce the stack size (-Xss, -XX:ThreadStackSize) to get some more headroom. But this is risky because if I make it too small, I'm going to get StackOverflowErrors.</p>
  
  <p><strong>How can I measure the average and maximum stack size for my application to guide my decision for an optimal -Xss value?</strong> I'm interested in two possible approaches:</p>
  
  <ol>
  <li>Measuring a running JVM during integration testing. What profiling tools will report max stack depth?</li>
  <li>Static analysis of the application looking for deep call hierarchies. Reflection in dependency injection makes it unlikely that this would work.</li>
  </ol>
  
  <p><strong>Update</strong>: I <em>know</em> the long-term right way to fix this problem. Please focus on the question I've asked: how do I <em>measure</em> stack depth?</p>
  
  <p><strong>Update 2</strong>: I got a nice answer on a related question specifically about JProfiler: <a href="https://stackoverflow.com/questions/8330033/can-jprofiler-measure-stack-depth">Can JProfiler measure stack depth?</a> (I posted the separate question as per JProfiler's community support recommendations)</p>