I have an algorithm that uses deep recursion. How many levels deep the recursion will go varies and depends on the input. To avoid stack overflow exceptions I keep count of the depth and terminate at a specific depth.
By a bit of experimentation I previously established that a level of 500 was the point at which to stop. However, now suddenly I get stack overflow exceptions at levels just over 300.
Can anyone shed a bit of light on which factors would affect this? Is it e.g. CPU and RAM? Or related to which other processes is running on the computer?
The answer varies, see here: Stack capacity in C#
You would be wise to change your algorithm to not be recursive, it will not be easy to see how much you can recurse, and basically you are tempting fate if you attempt to predict a certian level of recursion depth. That and whatever estimation algorithm you use for the stack depth might break with the next .NET service pack.