title: Python: Memory leak?
body: <h2>Query in Python interpreter:</h2>
  
  <pre><code>Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win
  32
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; k = [i for i in xrange(9999999)]
  &gt;&gt;&gt; import sys
  &gt;&gt;&gt; sys.getsizeof(k)/1024/1024
  38
  &gt;&gt;&gt;
  </code></pre>
  
  <h2>And here - see how much it takes from RAM:</h2>
  
  <p><img src="https://i.stack.imgur.com/ASc70.png" alt=""><br></p>
  
  <h2>Memory usage after statement <code>del k</code>:</h2>
  
  <p><img src="https://i.stack.imgur.com/kOEn7.png" alt=""></p>
  
  <h2>And after <code>gc.collect()</code>:</h2>
  
  <p><img src="https://i.stack.imgur.com/d3sA3.png" alt=""></p>
  
  <p>Why list of integers with expected size of 38Mb takes 160Mb?<br></p>
  
  <p><strong>UPD:</strong> <em>This part of question was answered (almost immediately and multiple times :))</em> </p>
  
  <h2>Okay - here is another riddle:</h2>
  
  <pre><code>Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win
  32
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; import sys
  
  &gt;&gt;&gt; str = 'abcdefg'
  &gt;&gt;&gt; sys.getsizeof(str)
  28
  &gt;&gt;&gt; k = []
  &gt;&gt;&gt; for i in xrange(9999999):
  ...     k.append(str)
  ...
  &gt;&gt;&gt; sys.getsizeof(str)*9999999/1024/1024
  267
  </code></pre>
  
  <h2>How much do you think it will consume now?</h2>
  
  <p><a href="https://i.stack.imgur.com/QK9CL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QK9CL.png" alt=""></a><br>
  <sub>(source: <a href="http://i.imm.io/P5Yq.png" rel="nofollow noreferrer">i.imm.io</a>)</sub>  </p>
  
  <p>Size of <code>str</code> is 28, vs 12 in past example. So, expected memory usage is 267Mb - even more then with integers. But it takes only ~40Mb!</p>