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
  • 530,221
  • 131
  • 937
  • 1,214
Debtanu Gupta
  • 65
  • 1
  • 6
  • 1
    Can you see the the error in both times they are different it means the recursion is changed to how you defined it – Yeshwin Verma Oct 12 '20 at 16:35
  • The second error isn't a recursion error. It's a memory error. You should run your programm and check the task-manager (on windows), or in general your memory usage. Maybe you don't have enough memory installed. – Foxcirc Oct 12 '20 at 16:40
  • Okay I see @ Yeshwin Verma The Programmer Thanks – Debtanu Gupta Oct 12 '20 at 16:43
  • I have 8 GB RAM. Is it enough for 1000 times recursion tree? @Foxcric – Debtanu Gupta Oct 12 '20 at 16:44
  • 2
    @ChristopherPeisert By consensus tags do *not* belong in the title (mentioning technologies as part of a regular sentence is OK). – Konrad Rudolph Oct 12 '20 at 16:49
  • Does this answer your question? [Why Python raises RecursionError before it exceeds the real recursion limit?](https://stackoverflow.com/questions/55560258/why-python-raises-recursionerror-before-it-exceeds-the-real-recursion-limit) – ggorlen Feb 21 '23 at 17:39

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
  • 21,862
  • 3
  • 86
  • 117
  • 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