title: Python: memory leak when resizing images
body: <p>I am working on a python program, that fetches images via <code>requests</code>, displays them five at the time, updating one at a time with a given interval. A separate thread is responsible for updating the queue with fresh images once in a while.
  It is supposed to run on a raspberry pi (original model b with .5gb ram) running raspbian (debian wheezy).</p>
  
  <p>I am writing this in python3 and using TKinter, Pillow and requests.</p>
  
  <p>It works quite well - on my windows development machine. It runs fine on the pi as well, but after a while it gets slower at updating the images on the screen, and eventually it gets killed by linux. Checking /var/log/syslog reveals, that it is killed because the system runs out of memory. </p>
  
  <p>Running it on windows, i noticed in the taskmanager, that the memory usage increases ~1mb every time an onscreen image is updated. Stepping through the code in debugging mode, i can see that the increase in memory usage happens in the following function:</p>
  
  <pre><code>def resize(self, img, width, height):
      img = img.resize((width, height), Image.ANTIALIAS)
      tkimg = ImageTk.PhotoImage(img)
      return tkimg
  </code></pre>
  
  <p>It seems like the img object hangs around.
  So my question is: Why does it hang around, when i assign the resized image to the same variable? Shouldn't the garbage collector handle this automatically?</p>
  
  <p>Any help appreciated!</p>
  
  <p><strong>EDIT</strong>:<br>
  Wrapping the culprit lines in <code>while True</code>, memory usage does <em>not</em> increase with each pass. It seems to only increase when the function is called with a new <code>img</code> as argument.</p>
  
  <pre><code>    while True:
          img = img.resize((width, height), Image.ANTIALIAS)
          tkimg = ImageTk.PhotoImage(img)
  </code></pre>
  
  <p>Deleting the returned img object after having assigned it to the TKinter label also does not seem to have any effect.</p>