Hidden references to function arguments causing big memory usage?
<p><strong>Edit:</strong> Never mind, I was just being completely stupid.</p>

<p>I came across code with recursion on smaller and smaller substrings, here's its essence plus my testing stuff:</p>

<pre><code>def f(s):
    if len(s) == 2**20:
        input('check your memory usage again')
    else:
        f(s[1:])
input('check your memory usage, then press enter')
f('a' * (2**20 + 500))
</code></pre>

<p>Before the call, my Python process takes about 9 MB (as checked by Windows task manager). After the 500 levels with ~1MB strings, it's at about 513 MB. No surprise, as each call level is still holding on to its string in its <code>s</code> variable.</p>

<p>But I tried to fix it by <strong>replacing</strong> the reference to the string with a reference to the new string and it still goes up to 513 MB:</p>

<pre><code>def f(s):
    if len(s) == 2**20:
        input('check your memory usage again')
    else:
        s = s[1:]
        f(s)
input('check your memory usage, then press enter')
f('a' * (2**20 + 500))
</code></pre>

<p>Why doesn't that let go off the memory? The strings even only get smaller, so later strings would easily fit into the space of earlier strings. Are there hidden additional references to the strings somewhere or what is going on?</p>

<p>I had expected it to behave like this, which only goes up to 10 MB (a change of 1 MB, as expected because the new string is built while the old string still exists):</p>

<pre><code>input('check your memory usage, then press enter')
s = 'a' * (2**20 + 500)
while len(s) != 2**20:
    s = s[1:]
input('check your memory usage again')
</code></pre>

<p>(Never mind the poor time complexity, btw, I know that, don't bother.)</p>

<p>Your function is recursive, so when you call <code>f()</code>, your current frame is put onto a stack, and a new one is created. So basically each function call keeps a reference to the new string it creates to pass down to the next call.</p>

<p>To illustrate the stack</p>

<pre><code>import traceback

def recursive(x):
    if x:
        recursive(x[1:])
    else:
        traceback.print_stack()

recursive('abc')
</code></pre>

<p>Gives</p>

<pre><code>$ python tmp.py
  File "tmp.py", line 10, in &lt;module&gt;
    recursive('abc')
  File "tmp.py", line 5, in recursive
    recursive(x[1:])
  File "tmp.py", line 5, in recursive
    recursive(x[1:])
  File "tmp.py", line 5, in recursive
    recursive(x[1:])
  File "tmp.py", line 7, in recursive
    traceback.print_stack()
</code></pre>

<p>When the final call to <code>recursive()</code> returns, it returns back into the next call above it which still has the reference to <code>x</code>.</p>

<blockquote>
  <p>But I tried to fix it by <strong>replacing</strong> the reference to the string with a reference to the new string and it still goes up to 513 MB</p>
</blockquote>

<p>Well you did in the current function being called, but the function which called it still has the reference to what was passed in. e.g.</p>

<pre><code>def foo(x):
    print "foo1", locals()
    bar(x)
    print "foo2", locals()

def bar(x):
    print "bar1", locals()
    x = "something else"
    print "bar2", locals()

foo('original thing')
</code></pre>

<p>When <code>foo()</code> is called, it passes the string <code>'original thing'</code> to <code>bar()</code>. And even though <code>bar()</code> then gets rid of the reference, the current call above to <code>foo()</code> still has the reference</p>

<pre><code>$ python tmp_x.py 
foo1 {'x': 'original thing'}
bar1 {'x': 'original thing'}
bar2 {'x': 'something else'}
foo2 {'x': 'original thing'}
</code></pre>

<p>I hope that illustrates it. I have been a little vague in my first statement about stack frames.</p>

python function memory