Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to cause stack overflow and heap overflow in python
    primarykey
    data
    text
    <p>I am trying to understand how python manages stack and heap. So I wanted to do some "bad" programming and cause a stack overflow and heap overflow. What I don't understand is why strings for example go to stack while all others go to heap. Is it just agreement of the designers? Are the examples correct? From what I have read everything in python is generated in heap since its object oriented, right? </p> <p><strong><em>EDITED</em></strong>: I suppose that stack in languages like C have a fixed length but in python even the stack is dynamically allocated as Anycorn said in his answer. Thats why I also get full memory if I try both a large string (on stack) or a list (on heap). If i am wrong please correct me. Thanks</p> <p>From <a href="http://docs.python.org/c-api/memory.html" rel="nofollow noreferrer">http://docs.python.org/c-api/memory.html</a></p> <blockquote> <p>Memory management in Python involves a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the Python memory manager. The Python memory manager has different components which deal with various <strong><em>dynamic storage management</em></strong> aspects, like sharing, segmentation, preallocation or caching.</p> <p>At the lowest level, a raw memory allocator ensures that there is enough room in the private heap for storing all Python-related data by interacting with the memory manager of the operating system. On top of the raw memory allocator, several object-specific allocators operate on the same heap and implement distinct memory management policies adapted to the peculiarities of every object type.</p> </blockquote> <p>Here are some examples. You can copy paste them in <a href="http://www.pythontutor.com/visualize.html#mode=edit" rel="nofollow noreferrer">Python official visualizer</a> but with smaller values cause it wont run... </p> <p>For stack overflow:</p> <pre><code>import time word = "test " x = word*1000000000 time.sleep(10) print ("this message wont appear if stack overflow has occurred!") </code></pre> <p>I get </p> <pre><code>x = word*1000000000 MemoryError </code></pre> <p>If I delete one zero it runs. I get max memory usage when I use <code>x = word*500000000</code> So I can't make a stack overflow because even the stack is dynamically allocated?</p> <p>For heap overflow:</p> <pre><code>i = 10000 test_list = [0] while i &gt; 0 : test_list [:0] = test_list #insert a copy of itself at the beginning i -= 1 </code></pre> <p>Now what I don't understand is how the garbage collector kicks in the programs.Does it run on both stack and heap since they are both dynamically allocated? Is it due to O/S memory manager? What do those things tell us about the characterization of python programming language? Does this justify the term "dynamic language" or "interpreted"? Sorry for the long question but i just need to clarify some things in my mind. Thanks in advance!</p> <p><strong><em>EDITED</em></strong><br> I've found what i was looking for: You can cause a 'real' stack overflow if you call <code>sys.setrecursionlimit(N)</code> with a value of N larger than your system can actually handle and then try to recurse to that depth. At some point your system will run out of stack space and the Python interpreter will crash.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload