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?

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 Commented Oct 12, 2020 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. Commented Oct 12, 2020 at 16:40
  • Okay I see @ Yeshwin Verma The Programmer Thanks Commented Oct 12, 2020 at 16:43
  • I have 8 GB RAM. Is it enough for 1000 times recursion tree? @Foxcric Commented Oct 12, 2020 at 16:44
  • 2
    @ChristopherPeisert By consensus tags do not belong in the title (mentioning technologies as part of a regular sentence is OK). Commented Oct 12, 2020 at 16:49

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks a lot @Christopher Peisert. Now it's totally cleared to me.
But can I access the Stack which is overflowing due to large recursion and change the size of it?
@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 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.
@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).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.