C# Recursion Depth - How Deep can you go

Keith Nicholas picture Keith Nicholas · Dec 22, 2010 · Viewed 29.1k times · Source

Is there any control how much you can Recursively call something?

From a basic test program I get a recursion depth of just over 18k

which depends on the stacksize....

is there a way to set up a chunk of memory (perhaps a thread) with a massive stack to increase recursion depth?

Answer

Andrew Orsich picture Andrew Orsich · Dec 22, 2010

I've increased the stack size during some documents recognition. It was really needed.

So you can increase stack size for thread using following code:

var stackSize = 10000000;
Thread thread = new Thread(new ThreadStart(BigRecursion), stackSize);

Thread(ThreadStart, Int32) -- Initializes a new instance of the Thread class, specifying the maximum stack size for the thread.

Source

Hope this what you need.