You can cause a stack overflow quite easily in python, as in any other language, by building an infinately recursive funcion. This is easier in python as it doesn't actually have to do anything at all other than be recursive.
>>> def foo():
... return foo()
...
>>> foo()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
.......
File "<stdin>", line 2, in foo
RuntimeError: maximum recursion depth exceeded
>>>
As for the heap, that is managed by a garbage collector. You can allocate lots of objects and eventually run out of heap space and Python will raise a MemoryError, but it's going to take a fair amount of time. You actually did that with your 'stack overflow' example in the question. You stored a reference to a string on the stack, this string took up all the free memory available to the process. As a rule of thumb, Python stores a reference to a heap structure on the stack for any value that it can't guarantee the size of.
As for how it all works, from the fist example you can see that python has a built-in limit to the depth of the call stack that it will not exceed. The amount of memory available for heap space is defined by the OS however and will depend upon many factors.
These are should be the appropriate parts of the python docs for infomation on the errors themselves:
- http://docs.python.org/release/3.2.3/library/exceptions.html#RuntimeError
- http://docs.python.org/release/3.2.3/library/exceptions.html#MemoryError
Hello! I am new to python. I created a script/bot, and its function is work until closed.
That bot has a main() function that one way or another calls itself after a 1 minute timer.
After some time i noticed that it crashed and closed Tested again but now in Visual Studio Code and after 18 hours it crashed again with fatal error stack overflow.
Went in and created a separate script with a main function calling itself and after 1035 loops it caused stack overflow. Now 1035*1minute is about 18 hours.
How can i create an infinite script without causing stack overflow?
Thanks!
How to cause stack overflow and heap overflow in python - Stack Overflow
Newest 'python' Questions - Stack Overflow
Am i still a python programmer if use stack overflow a lot and finish projects?
Highest scored 'python' questions - Stack Overflow
You can cause a stack overflow quite easily in python, as in any other language, by building an infinately recursive funcion. This is easier in python as it doesn't actually have to do anything at all other than be recursive.
>>> def foo():
... return foo()
...
>>> foo()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
.......
File "<stdin>", line 2, in foo
RuntimeError: maximum recursion depth exceeded
>>>
As for the heap, that is managed by a garbage collector. You can allocate lots of objects and eventually run out of heap space and Python will raise a MemoryError, but it's going to take a fair amount of time. You actually did that with your 'stack overflow' example in the question. You stored a reference to a string on the stack, this string took up all the free memory available to the process. As a rule of thumb, Python stores a reference to a heap structure on the stack for any value that it can't guarantee the size of.
As for how it all works, from the fist example you can see that python has a built-in limit to the depth of the call stack that it will not exceed. The amount of memory available for heap space is defined by the OS however and will depend upon many factors.
These are should be the appropriate parts of the python docs for infomation on the errors themselves:
- http://docs.python.org/release/3.2.3/library/exceptions.html#RuntimeError
- http://docs.python.org/release/3.2.3/library/exceptions.html#MemoryError
please correct me if wrong:
As far as I know, when it comes to actual stack implementation, the python stack (in the default distribution) is actually based in the heap memory (memory allocated with malloc). So you cannot cause the stack overflow, but you can run out of memory. The computer slowdown you seen is because memory is being swapped to disk, very slow procedure.
generally, you have no idea how the interpreted/byte-compiled language implements its stack, but most like it is not implemented in the stack memory, so you cannot cause stack overflow. it is possible to implement Python using alloca, but why?
Cf. CPython - Internally, what is stored on the stack and heap?
Try the same experiment with compiled language, C++, Fortran, etc. which compiles to machine code.
so i am sort of a python developer, i am an automation developer, my job is to automate tests, create framework etc.
but i find that i keep using stackoverflow a lot, question is am i still a programmer if i keep using stack overflow a lot?
import sys
sys.setrecursionlimit(10**8)
def ackermann(m,n):
if m == 0:
return (n + 1)
elif n == 0:
return ackermann(m - 1, 1)
else:
return ackermann(m - 1, ackermann(m, n - 1))
for x in range(5):
for y in range(5):
print(ackermann(x, y))
Python's default recursion limit is 10**4. Changable with setrecursionlimit() so you can get a 'Stack overflow' error because Ackermann function's output is too long.
I like to use
def test(n):
if n == 1:
return 1
else:
return test(n-1)*n
to test max recursion. It will throw a 'RecursionError: maximum recursion depth exceeded in comparison' if n is to large.