Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Recursion Depth - How Deep can you go

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?

like image 220
Keith Nicholas Avatar asked Dec 22 '10 20:12

Keith Nicholas


2 Answers

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.

like image 132
Andrew Orsich Avatar answered Oct 04 '22 21:10

Andrew Orsich


I think you are risking problems here. It's hard to determine exactly how much stack a recursive algorithm will use. And, if you are to the point where there's some question about if there'll be enough, I'd look for another approach.

Most recursive algorithms could be rewritten to not be recursive. You can then allocate as much memory as you need and even recover gracefully if there's not enough.

like image 32
Jonathan Wood Avatar answered Oct 04 '22 21:10

Jonathan Wood