I'd like to be able to predict the number of recursive calls that fit on the stack before a StackOverflow
exception happens. For this, I'd need to find out the 'footprint' of a given method call on the stack.
Is there a way to do this programmatically? I looked into System.Diagnostics.StackFrame
and System.Diagnostics.StackTrace
but couldn't find anything relevant.
Empirically, using this simple example, I found the footprint to be quite different for:
- 32 bit vs 64 bit: ~22k vs ~8k frames (does different pointer size account for the ~3x increase?)
- debug vs release: ~22k vs ~64k frames (Debug builds add all sorts of special-pattern bounds-checking padding + debugging instrumentation)
- Non-optimized vs optimized:
- ~22k vs ~51k in Debug (expected - less debug info)
- ~64k vs ~51k in Release (that's strange! maybe some memoization techniques are employed?)
- Debugger attached vs not attached: it's amazing what a difference this makes in the jitted code!
- 85k vs 258k - Release, Optimizations ON
- 14k vs 64k - Debug, Optimizations OFF
Most likely different versions of .NET also yield different results.
To sum it all up:
Seeing that the stack frame size varies so wildly with these parameters, is there a programmatic way to figure out the stack frame size for a given method at runtime?
How about 'offline' (manually)? Maybe at least for the straight-up, Release, non-optimized build?