Memory Leak - Loading Images


#1

Hi everyone :blush:

My app lets users pick an image from their gallery and display it in the app’s view. There is only one view, and if the user picks another image, the view will replace the old image with the new one.

When testing this app in iOS, I noticed that I have a severe memory leak; every time I select a new image from the gallery, the memory goes up, and never down.

I am using the ImagePicker plugin to load the images, and before assigning them to the view, I am having to convert the ImageAsset provided by the plugin into an ImageSource (for a variety of reasons including measuring the dimensions, and later converting to base64).

This is my code so far, with an attempt to clear some memory, but I still get the memory leaks:

exports.pick = function(){

    context.authorize().then(function() {

        return context.present();

    }).then(function(selection) {

        selection.forEach(function(selected) {

            imageSourceModule.fromAsset(selected).then(function(photo){

                var scrollViewAspectRatio = (page.getViewById("mainStack").getMeasuredWidth())/(0.65 * page.getViewById("mainStack").getMeasuredHeight());
                var photoAspectRatio = photo.width/photo.height;

                if(photoAspectRatio >= scrollViewAspectRatio){

                    pageData.set("showHorizontalScrollView", true);
                    pageData.set("showVerticalScrollView", false);
                    pageData.set("horizontalImage", selected);

                }else{

                    pageData.set("showHorizontalScrollView", false);
                    pageData.set("showVerticalScrollView", true);
                    pageData.set("verticalImage", selected);

                }

                selected.delete();
                photo.delete();
                
                if (global.android) { global.GC(); } else { global.__collect(); }
            
            }, function(){});

        });
        list.items = selection;
    }).catch(function (e) {
        // process error
    });

}

Any help would be greatly appreciated! :heart_eyes:
Nat

P.S. I have a feeling that I am not deleting the object correctly, and I’m not garbage collecting properly either. I have not included and “require()” lines at the top related to this.