62

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?

Andrew Orsich
  • 52,935
  • 16
  • 139
  • 134
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • possible duplicate of [How do you change default stack size for managed executable.net ](http://stackoverflow.com/questions/1042345/how-do-you-change-default-stack-size-for-managed-executable-net) – Byron Whitlock Dec 22 '10 at 20:34
  • 1
    Infinitely deep. Or at least in languages supporting tail calls. – Juliet Dec 22 '10 at 20:39
  • 7
    3 levels deep... – Rafe Kettler Dec 22 '10 at 20:42
  • @Juliet: ...and if you can write tail-recursive functions. :) – elmattic Dec 22 '10 at 20:58
  • C# does not support tail calls. There is an opcode for it in CIL, but as far as I know no .NET languages actually use it (except maybe F#) – MattDavey Jan 05 '11 at 13:22
  • 3
    Afaik the jitter doesn't necessarily need the tail call instruction to rewrite a tail call. From what I recall on .net 3.5 only the 64 bit jitter rewrites tail calls. – CodesInChaos Jan 05 '11 at 13:26
  • 3
    You have to understand that 99.99% of code doesn't blow the stack and 99.99% of the code that does does so because it is infinitely recursive. That leaves only 0.02% of code that blows the stack by using a finite amount. It's only these programs that can benefit by increasing the stack. – Enigmativity Jul 18 '14 at 02:03
  • 3
    can I have the source of your statistics? :) – Keith Nicholas Jul 18 '14 at 02:06

4 Answers4

60

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.

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
Andrew Orsich
  • 52,935
  • 16
  • 139
  • 134
  • 12
    I really doubt this is what anyone needs. You simply shouldn't be creating code like this, I would sack a programmer who tried. Any recursive algorithm can be implemented without using recursion and sometimes it can be implemented in ways that reduces the order of the problem by a magnitude. This is like a car mechanic using sledge hammer to fix an engine. – Mick Jul 18 '14 at 01:32
  • 1
    @Mick, what if the code that uses recursion is outside of your control? For example, you could be using a third-party library that uses recursion. – Sam Aug 07 '14 at 04:44
  • 9
    I'd find fourth-party ;) – Mick Aug 07 '14 at 06:18
  • 2
    Seriously... Unless they an exceptional case for such an implementation I would lose confidence in the ability of the vendor and would be reluctant to rely on their products. – Mick Aug 07 '14 at 06:31
  • 1
    I coded something like `a crawler` say a spider which gather a result from a web request and according to result it calls the same method for new result, I mean there is a recursion. It was giving a `StackOverFlow` error after 3k request in `UI Thread`. Now, I created a thread as described above and it passed that range. Still working 4.6k...+ at the moment. Thanks! – Davut Gürbüz Apr 19 '17 at 09:04
  • I just stumbled on this. @Mick, Am I to believe that recursion should never be done? What if you're not executing through a linear structure, what if you're executing through a large complex TREE structure? Recursion also makes staying immutable much easier which also has advantages. Should we all not think about the trade-offs and just bow to the dogmatic wisdom that all recursion is always bad? – Stewart Anderson Jun 07 '18 at 19:21
  • @StewartAnderson no I'm saying an algorithm that uses recursion with an indefinite level of recursion, especially one as in this case that requires you to start fiddling with the available stack definitely needs a rethink and should be re-implemented using a non-recursive approach. I've never encountered a recursive algorithm that couldn't be implemented just as easily without using recursion. – Mick Jun 08 '18 at 01:08
  • 17
    just for reference, the question is a "can", as for "why", there are various reasons none of which are sackable offences :) I think sometimes people get too focused on their own little bubble and what they are trying to achieve that they don't consider the vast world of what people want to do with programming languages. Having super strong opinions about what you should and shouldn't do often constrains your thinking. Having said that, this is something most people generally shouldn't need and you should always evaluate the tradeoffs you are making – Keith Nicholas Feb 04 '19 at 21:15
24

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.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • 21
    +1 Every recursive algorithm can be written as non recursive with a loop and a stack data structure. – Byron Whitlock Dec 22 '10 at 20:35
  • 7
    @Byron: Thanks for making me remember my data structures class in college. Prof: "Write this tree-traversal program in a non-recursive format." Me: "Why do you hate us?" : ) – Nick Vaccaro Dec 22 '10 at 20:40
  • 6
    My question was how to do it, I was more curious than wanting to actually do it. While this is good advice, it doesn't answer the question as posed. – Keith Nicholas Dec 23 '10 at 01:49
  • Don't forget these [considerations](https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/recursive-procedures#considerations-with-recursive-procedures) when choosing recursion. – Matt Roy Sep 12 '19 at 14:10
6

The default stack size is stored in the PE header.

If you spawn the thread yourself, Thread has a constructor that takes the stack size as a parameter.

However, the default .NET stack size of 1 MB should be enough for most tasks, so before you change it you should at least review the task.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
5

Even if you manage to get greater recursion depths, simply for performance reasons I would implement this algorithm without recursion. Method calls are way more expensive than iterations within a while loop. I'd strongly advise against implementing anything that requires fiddling with the default stack size.

I occasionally use recursion but only when the call depth is defined and low (as in less than 100). When creating commercial software, using recursive algorithms that have an indefinite number of iterations is completely unprofessional and likely to give you very angry customers.

Mick
  • 6,527
  • 4
  • 52
  • 67