Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Memory usage when changing image in an ui.ImageView
-
But this works
v['img'].image = None
-
Yes, the use of
all
in my example was very sloppy. I don’t know what came over me.Thanks for all your suggestions.
I’ve tried setting image to None and deleting the looping variable p but the increasing memory consumption continues.
I was trying to avoid using the objc because I am much more comfortable in Python but I’ll look into it this evening.
-
@mrjk could you tell me how you check the memory consumption, so I can also try
-
@mrjk said:
I’ve tried setting image to None
That is only if you don't recreate the ImageView in the loop, thus no remove subview
-
In the left panel in Xcode which usually shows the project navigation / directory structure there’s a row of icons along the top. Click the 7th one across : “Debug navigator”.
If you select the “memory” from the list it shows charts in the main panel.
-
-
-
@cvp there was some code a while back that checked memory usage..
https://forum.omz-software.com/topic/3146/share-code-get-available-memoryLukasKollmer's code I think checks system mem usage. I had some code that checked the process usage, however it likely does not work on 64 bit. Now that I have a 64 bit iPad available to me, I should probably try to get it working again.
@mrjk have you tried the same code, but without the imageview? I.e just creating the ui.Image's?
-
@JonB Thanks. I had seen this topic and I have tested the script with/without image=None but not really sure that it has an impact
-
Another few things to try:. Put this while loop inside a thread. Where you have put it, it is run on the same background thread as some other iOS background tasks.
Number 2, setting an image should technically be done on the main thread -- so wrap the code that sets v["img"].image = ... with a def wrapped with on_main_thread (from objc_util library)
Number 3:. Draining an AutoRelease pool. Blah, this problem has been encountered before and solved, lol
https://forum.omz-software.com/topic/3844/photos-asset-get_image_data-is-there-a-memory-leak/6
I'm not sure if the objc_util AutoRelease retaining thing has been fixed or if you still need to patch it. Also, I seem to remember some built in AutoRelease pool stuff in pythonista ..
Don't put this while loop in the main code -- iOS background tasks can't run until
-
It appears that objc_util now has an autorelease context handler:
with objc_util.autorelease():
# set the new imageMight work. I'm not sure if the other bug -- related to clearing the cached methods is still needed.
-
@mrjk have you tried the same code, but without the imageview? I.e just creating the ui.Image's?
Thanks. I hadn't but it does suffer from the same issue.
import photos import time all_photos = photos.get_assets() while True: for p in all_photos: image = p.get_ui_image(size=(300,300)) time.sleep(.5) time.sleep(5)
-
@JonB said:
Another few things to try:.
Thanks very much. That link looks like what I've been trying to wrestle together in objc without success. I'll try to implement some of these suggestions and report back.
-
This is a working version without significant leak.
import photos import time import concurrent.futures import objc_util import ui @objc_util.on_main_thread def update(image_view, ui_image): image_view.image = ui_image def loop_photos(): all_photos = photos.get_assets() while True: for p in all_photos: with objc_util.autoreleasepool(): ui_image = p.get_ui_image(size=(300,300)) #image_data = p.get_image_data() update(v['img'], ui_image) time.sleep(.5) time.sleep(5) v = ui.View() v.add_subview(ui.ImageView(name='img')) v.present() with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: executor.submit(loop_photos)
Thanks very much for all the suggestions. I'm grateful for the tip about threading and will try to incorporate it into the rest of the project but the thing that does the lifting here is the autoreleasepool context manager; with just this addition and removing the threading it works just fine.