Python's sys
module provides a function setrecursionlimit
that lets you change Python's maximum recursion limit. The docs say:
The highest possible limit is platform-dependent.
My question is: What is the highest possible limits for various platforms, under CPython? I would like to know the values for Linux, Mac and Windows.
UPDATE: Can we please avoid "You're doing it wrong" answers? I know that trying to do very deep recursion is usually a bad idea. I've considered the pros and cons in my specific situation and decided that I want to do it.
The recursion limit is usually 1000.
No, C++ does not have an explicit recursion depth. If the maximum stack size is exceeded (which is 1 MB by default on Windows), your C++ program will overflow your stack and execution will be terminated.
The maximal recursion depth is limited by JavaScript engine. We can rely on it being 10000, some engines allow more, but 100000 is probably out of limit for the majority of them.
Python's default recursion limit is 1000, meaning that Python won't let a function call on itself more than 1000 times, which for most people is probably enough. The limit exists because allowing recursion to occur more than 1000 times doesn't exactly make for lightweight code.
On Windows (at least), sys.setrecursionlimit
isn't the full story. The hard limit is on a per-thread basis and you need to call threading.stack_size
and create a new thread once you reach a certain limit. (I think 1MB, but not sure) I've used this approach to increase it to a 64MB stack.
import sys import threading threading.stack_size(67108864) # 64MB stack sys.setrecursionlimit(2 ** 20) # something real big # you actually hit the 64MB limit first # going by other answers, could just use 2**32-1 # only new threads get the redefined stack size thread = threading.Thread(target=main) thread.start()
I haven't tried to see what limits there might be on threading.stack_size
, but feel free to try... that's where you need to look.
In summary, sys.setrecursionlimit
is just a limit enforced by the interpreter itself. threading.stack_size
lets you manipulate the actual limit imposed by the OS. If you hit the latter limit first, Python will just crash completely.
You shouldn't overuse recursive calls in CPython. It has not tail optimization, the function calls use a lot of memory and processing time. Those limits might not apply to other implementations, it's not in the blueprints.
In CPython, recursion is fine for traversing data structures (where a limit of 1000 should be enough for everybody) but not for algorithms. If I were to implement, say, graph related algorithms and hit the recursion limit, I would either implement my own stack and use iterations, or look for libraries implemented in C/C++/whatever before raising the limit by hand.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With