omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular
    1. Home
    2. mrjk

    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.


    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 7
    • Best 1
    • Controversial 0
    • Groups 0

    mrjk

    @mrjk

    2
    Reputation
    583
    Profile views
    7
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    mrjk Unfollow Follow

    Best posts made by mrjk

    • RE: Memory usage when changing image in an ui.ImageView

      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.

      posted in Pythonista
      mrjk
      mrjk

    Latest posts made by mrjk

    • RE: Memory usage when changing image in an ui.ImageView

      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.

      posted in Pythonista
      mrjk
      mrjk
    • RE: Memory usage when changing image in an ui.ImageView

      @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.

      posted in Pythonista
      mrjk
      mrjk
    • RE: Memory usage when changing image in an ui.ImageView

      @JonB

      @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)
      
      posted in Pythonista
      mrjk
      mrjk
    • RE: Memory usage when changing image in an ui.ImageView

      @cvp said:

      @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

      Yes exactly. But I did also try doing it in addition to removing the subview.

      posted in Pythonista
      mrjk
      mrjk
    • RE: Memory usage when changing image in an ui.ImageView

      @cvp

      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.

      posted in Pythonista
      mrjk
      mrjk
    • RE: Memory usage when changing image in an ui.ImageView

      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.

      posted in Pythonista
      mrjk
      mrjk
    • Memory usage when changing image in an ui.ImageView

      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

      posted in Pythonista
      mrjk
      mrjk