title: understanding memory consumption: free the memory of a StringIO?
body: <p>I'm curious to understand why in the first example, the memory consumption happens like I was imaging:  </p>
  
  <pre><code>s = StringIO()
  s.write('abc'*10000000)
  # Memory increases: OK
  s.seek(0)
  s.truncate()
  # Memory decreases: OK
  </code></pre>
  
  <p>while in this second example, at the end, I use the same thing but the memory does not seem to decrease after the truncate method.
  The following code is in a method of a class. </p>
  
  <pre><code>from StringIO import StringIO
  import requests
  
  self.BUFFER_SIZE = 5 * 1024 * 2 ** 10 # 5 MB
  self.MAX_MEMORY = 3 * 1024 * 2 ** 10 # 3 MB
  
  r = requests.get(self.target, stream=True)  # stream=True to not download the data at once
  
  chunks = r.iter_content(chunk_size=self.BUFFER_SIZE)
  buff = StringIO()
  
  # Get the MAX_MEMORY first data
  for chunk in chunks:
      buff.write(chunk)
      if buff.len &gt; self.MAX_MEMORY:
          break
  
  # Left the loop because there is no more chunks: it stays in memory
  if buff.len &lt; self.MAX_MEMORY:
      self.data = buff.getvalue()
  
  # Otherwise, prepare a temp file and process the remaining chunks
  else:
      self.path = self._create_tmp_file_path()
  
      with open(self.path, 'w') as f:
          # Write the first downloaded data
          buff.seek(0)
          f.write(buffer.read())
  
          # Free the buffer ?
          buff.seek(0)
          buff.truncate()
  
          ###################
          # Memory does not decrease
          # And another 5MB will be added to the memory hiting the next line which is normal because it is the size of a chunk
          # But if the buffer was freed, the memory would stay steady: - 5 MB + 5 MB
  
          # Write the remaining chunks directly into the file
          for chunk in chunks:
              f.write(chunk)
  </code></pre>
  
  <p>Any thoughts?
  Thanks.</p>