Hi all
I've written a script which loops through a list of photos, displaying the current one while some processing happens behind the scenes. I've wrapped this up into an app in Xcode using the Pythonista3AppTemplate and have noticed that the memory consumption grows every time the photo changes. Minimum code to reproduce:
# Code 1
import photos
import ui
import time
v = ui.View()
v.add_subview(ui.ImageView(name = 'img'))
v.present()
all = photos.get_assets()
while True:
for p in all:
v['img'].image = p.get_ui_image(size=(300,300))
time.sleep(.5)
time.sleep(5)
Xcode shows memory usage steps up every time get_ui_image() is called. The rate of increase is linked to the size argument.
I tried a few things to narrow down where the issue is. This one still loops and changes the image forever but it gets all the ui_images once at the start.
# Code 2
import photos
import ui
import time
v = ui.View()
img = ui.ImageView(name='img')
v.add_subview(img)
v.present()
all = photos.get_assets()[0:5]
images = [p.get_ui_image(size=(300,300)) for p in all]
while True:
for i in images:
v['img'].image = i
time.sleep(.5)
time.sleep(2)
The memory usage jumps up at the beginning, as you'd expect, but then it stays constant while it loops. This is ok for a small number of photos but impractical if you want to look at the whole photo roll.
I changed the original code so it removes the ImageView entirely to try to get some garbage collection.
import photos
import ui
import time
v = ui.View()
v.present()
all = photos.get_assets()
while True:
for p in all:
for sv in v.subviews:
v.remove_subview(sv)
img = ui.ImageView(name = 'img')
img.image = p.get_ui_image(size=(300,300))
v.add_subview(img)
time.sleep(.5)
time.sleep(5)
The memory usage still grows with every change.
I think this shows it's caused by calling get_ui_image(). But is there anything I should (or could) be doing to kick start any garbage collection here? Is this a memory leak in the Pythonista photos module? And can I mitigate it?
Grateful for any advice. J