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.

**How can I measure the average and maximum stack size for my application to guide my decision for an optimal -Xss value?** I'm interested in two possible approaches:

1. Measuring a running JVM during integration testing. What profiling tools will report max stack depth?
2. Static analysis of the application looking for deep call hierarchies. Reflection in dependency injection makes it unlikely that this would work.

**Update**: I *know* the long-term right way to fix this problem. Please focus on the question I've asked: how do I *measure* stack depth?

**Update 2**: I got a nice answer on a related question specifically about JProfiler: [Can JProfiler measure stack depth?](https://stackoverflow.com/questions/8330033/can-jprofiler-measure-stack-depth) (I posted the separate question as per JProfiler's community support recommendations)