Is there a limit to the number of calls on a call stack? If so then what is the maximum number?

Or is memory the limiting factor to use recursion?


The number of calls is usually limited by the size of the stack, but the details differ between programming languages, operating systems, and how they handle memory. Operating systems usually require the stack to be in a statically sized contiguous memory block (because that makes it easier to handle), those are precious, so you are limited to what you got. Most programming languages want to use that native stack (because it's fast), so those limits apply, and the more stack memory each call requires, the less recursion depth it needs to crash.

Even if your stack could grow, you would still be limited by the physical memory. And out of memory is worse (affects all programmes) than a stack overflow (crashing a single application).

In JavaScript, which uses its own call stack and therefore is not bound to those limits, there are exact and implementation-specific numbers of maximum recursion depth (These results by Dr. Axel are probably outdated). In theory, this should be eased now for doing tail call recursion (the last action being the recursive call, returning its result without modification) by turning the recursion into a loop automatically, but I haven't seen that working so far (ES6 compatibility table says NO), and I doubt it will actually happen even though it is part of the ES2015 standard.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .