I am working on a python program, that fetches images via requests
, 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).
I am writing this in python3 and using TKinter, Pillow and requests.
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.
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:
def resize(self, img, width, height):
img = img.resize((width, height), Image.ANTIALIAS)
tkimg = ImageTk.PhotoImage(img)
return tkimg
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?
Any help appreciated!
EDIT:
Wrapping the culprit lines in while True
, memory usage does not increase with each pass. It seems to only increase when the function is called with a new img
as argument.
while True:
img = img.resize((width, height), Image.ANTIALIAS)
tkimg = ImageTk.PhotoImage(img)
Deleting the returned img object after having assigned it to the TKinter label also does not seem to have any effect.