2

I define a infinite recursive function as:

>>>def f():
>>>   f()
>>>

Then I called the function and this happend:

>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in f
File "<stdin>", line 2, in f
File "<stdin>", line 2, in f
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded
>>>

Next I do this:

>>>import sys
>>>sys.getrecursionlimit()
1000
>>>sys.setrecursionlimit(2147483647) #as 2147483647 is the highest number I can set for recursion in Python 3.8.5

Then I again call the function, but...

>>> f()
Traceback (most recent call last):
File "<stdin>", line 2, in f
File "<stdin>", line 2, in f
File "<stdin>", line 2, in f
[Previous line repeated 997 more times]
MemoryError: Stack overflow

I want to know, after changing the recursion limit to 2147483647 , why Python is still restricting the recursion to 1000?

Konrad Rudolph
  • 506,650
  • 124
  • 909
  • 1,183

1 Answers1

1

The recursion limit was successfully updated, since the first errors message was:

RecursionError: maximum recursion depth exceeded

and then after increasing the recursion depth, the error message changed to:

MemoryError: Stack overflow

From the documentation on sys.setrecursionlimit():

Set the maximum depth of the Python interpreter stack to limit. This limit prevents infinite recursion from causing an overflow of the C stack and crashing Python.

Hence, by increasing the recursion limit, the program crashed the Python interpreter.

Since Python does not optimize tail recursion, unlimited recursion causes the stack to overflow (run out of memory).

In many real-world applications it is necessary to implement functions without recursion to avoid stack overflow memory errors.

See also: Setting stacksize in a python script

Christopher Peisert
  • 18,667
  • 3
  • 73
  • 100
  • Thanks a lot @Christopher Peisert. Now it's totally cleared to me. – Debtanu Gupta Oct 12 '20 at 16:50
  • But can I access the Stack which is overflowing due to large recursion and change the size of it? – Debtanu Gupta Oct 12 '20 at 16:50
  • 1
    @DebtanuGupta: The actual underlying stack is limited, because computers are finite. An infinitely recursing function will always bust the stack; what you're trying to do makes no sense. – ShadowRanger Oct 12 '20 at 16:52
  • @ShadowRanger at first I was confused about those two errors, one is RecursionError and another is MemoryError. Now I see that, the size of stack is capable to hold data for 1000 recursions only. So I want to know if there is any way to increase the stack size to do the recursion more than 1000 times. – Debtanu Gupta Oct 12 '20 at 16:55
  • 1
    @DebtanuGupta: I suspect it's going more than 1000, it may just be trimming the traceback to the most recent 1000. If I try to do what you did, Python just crashes as it blows the C stack; I don't even get a `MemoryError`, just a hard crash (Segmentation fault). Your setup differs, but I can't say how for sure (I'm running `ipython` in Alpine Linux under WSLv2). – ShadowRanger Oct 12 '20 at 17:00
  • Okay! Thanks for another clearance @ShadowRanger :) – Debtanu Gupta Oct 12 '20 at 17:01