Skip to content

Memory leak when dealing with images. iOS #3887

Closed

Description

@3rror404

I display two images on the page. When either is tapped, two new images are loaded. I've tried binding the src properties to an observable and updating this. I've also tried removing the Image elements and entirely replacing them. Both of these lead to high memory usage and crashes. It looks like the old images are never removed from memory.

Here is part of the code using binding. Memory usage jumps by ~30mb each time these images are loaded - even though the images themselves are only 87kb.

function loadQuestionImages(question) {
    // ## Load both images before binding to ensure
    // ## both display at same time.

    var loadedImages = 0;
    var imageSRC_1;
    var imageSRC_2;

    imageSourceModule.fromUrl('https://placehold.it/2000x2000')
        .then(function (res) {
            loadedImages = loadedImages + 1;
            imageSRC_1 = res;

            if (loadedImages == 2) {
                imagesLoaded();
            }
        }, function (error) {
            //console.log("Error loading image: " + error);
        });

    imageSourceModule.fromUrl('https://placehold.it/2000x2000')
        .then(function (res) {
            loadedImages = loadedImages + 1;
            imageSRC_2 = res;

            if (loadedImages == 2) {
                imagesLoaded();
            }
        }, function (error) {
            //console.log("Error loading image: " + error);
        });

    function imagesLoaded() {
        pageData.set('imageSRC_1', imageSRC_1);
        pageData.set('imageSRC_2', imageSRC_2);

        animateInImages();
    }
}

There is also a project here that replaces the image elements https://github.com/3rror404/ImageMemoryTest. This performs exactly the same as the code above.

Am I doing something wrong here?

Thanks

Activity

  1. NickIliev commented on Mar 28, 2017

    @NickIliev
    Contributor

    Hey @3rror404 thank you for reporting this one - indeed the memory consumption is rising and sometimes the app is crashing or freezing due to out of memory before the cache is cleared. We will look into this. Meanwhile, you can use nativescript-web-image-cache to load your online images.
    With this plugin the memory consumption is normal but if needed you can force clean the cache as well.

    e.g. (based on your test application)
    main-page.js

    var imageCacheModule = require("nativescript-web-image-cache");
    
    
    function loadQuestionImages() {
        var imageSRC_1 = 'https://placehold.it/2000x2000';
        var imageSRC_2 = 'https://placehold.it/2000x2000';
    
        imagesLoaded();
    
        function imagesLoaded() {
            var image1_orig = page.getViewById('question-1-image');
            var image2_orig = page.getViewById('question-2-image');
    
            image1_orig.src = imageSRC_1;
            image2_orig.src = imageSRC_2;
    
            animateInImages();
        }
    }
    

    main-page.xml

     <Page loaded="onLoaded" navigatedTo="onNavigatedTo" actionBarHidden="true" xmlns="http://schemas.nativescript.org/tns.xsd" xmlns:IC="nativescript-web-image-cache">
        <GridLayout id="questions-wrap" row="0" rows="50*, 50*">     
            <GridLayout id="image1_wrap" class="question" tap="tapQuestion" row="0" rows="*" columns="*">
                <IC:WebImage id="question-1-image" stretch="aspectFill" ></IC:WebImage> 
            </GridLayout>
            <GridLayout id="image2_wrap" class="question" tap="tapQuestion" row="1" rows="*" columns="*">
                <IC:WebImage id="question-2-image" stretch="aspectFill" ></IC:WebImage> 
            </GridLayout>
        </GridLayout>
    </Page>
    

    App to reproduce this issue here

    Steps to reproduce:

    • start the application for iOS
    • start Instruments and track the memory usage for ImageMemoryTest app
    • tap on the image (this will load new iage from provided URL) - result the cache will not be cleaned which will lead to Out of Memory. Using nativescript-web-image-cache will resolve this issue)
  2. 3rror404 commented on Mar 30, 2017

    @3rror404
    Author

    Thanks @NickIliev,

    I've just tested on Android and the same memory issue exists.

    Using the image-cache plugin does seem to work but I can't now use the technique above to ensure both images are loaded before displaying. I don't suppose you have any ideas about this issue I've just opened? VideoSpike/nativescript-web-image-cache#24

  3. NickIliev commented on Mar 30, 2017

    @NickIliev
    Contributor

    @3rror404 for your scenario you can use and manually trigger the garbage collector before loading the new images.
    Here is how to do it in iOS:

    function loadQuestionImages(question) {
          if (app.ios) {
               global.__collect(); 
          }
    
          // the loading of the new images follows here
    }
    

    After the GC is called the memory usage no longer is rising on each tap and is at stable level.
    (using Instruments >> Allocations for tracking the memory consumption)

    You can also trigger GC for Android - look here for more details

  4. 3rror404 commented on Mar 30, 2017

    @3rror404
    Author

    That works great. Thanks for the help.

  5. NickIliev commented on Mar 31, 2017

    @NickIliev
    Contributor

    @3rror404 use this only as a last resort and where needed as it might lead to side effects (mainly with page transitions) and is not the recommended way to handle memory usage in iOS app (that is the reason the method is private). Meanwhile, we will investigate the reasons for this and post additional info here.

  6. 3rror404 commented on Mar 31, 2017

    @3rror404
    Author

    Ok cool.

    The idea of this app is for the user to choose an image quickly so I'm switching images a lot. Maybe 30-40 times in 10-20 seconds or less dependant on network speed.
    Could triggering the GC in rapid succession cause any problems?
    Would it be better to trigger the GC every few image changes rather than every time?
    What side effects might this cause?

  7. NickIliev commented on Apr 3, 2017

    @NickIliev
    Contributor

    One of the thing that might happen is to clear reference to objects that are still needed (for example object needed for navigation). .I can not answer your question directly - it is best to test the scenario when you are using the GC and then navigation to another page and back - and if there is no low performance in the page transitions then you should be ok.

  8. tsonevn commented on Feb 21, 2018

    @tsonevn
    Contributor

    Hi @3rror404,
    We tested the provided project in your very first comment, and we are not able to recreate this issue while using latest tns-core-modules and tns-ios (3.4.1).
    If you still facing the issue, please provide steps for reproducing, and I will reopen the issue.

  9. ghost removed on Feb 21, 2018
  10. lock commented on Aug 27, 2019

    @lock

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  11. locked and limited conversation to collaborators on Aug 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions