How to cycle through images without blowing up memory?

AliceLAliceL USMember
edited May 2015 in Xamarin.iOS

I've run into an issue where the memory blows up when I reach a screen that cycles through a group of images when loading the image using the FromFile call.

Due to the nature of the app I'm writing, each image is only needed once, so I really have no need to cache them.

I currently have a very simple test app where one of the screens will cycle through 64 images every time you press the next button.
From the documentation on FromFileUncached I've learned that FromFile and FromFileUncached are basically identical and should not cache the image when I load it.

The UIView of the screen literally only has 2 things added to it, a UIImageView and a UIButton.
When it is first loaded it sets the image of the UIImageView and after that, every time the button is pressed it sets UIImageView.Image to a new image.

The only code that I have added is the following to initially set the image:

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        // Perform any additional setup after loading the view, typically from a nib.
        imgCenter.Image = UIImage.FromFile("dotcompare_" + i.ToString() + ".png");
    }

And then to set the new image when the button is pressed:

    partial void btnNext_TouchUpInside(UIButton sender)
    {
        if (i < 64)
        {
            i++;
            imgCenter.Image = UIImage.FromFile("dotcompare_" + i.ToString() + ".png");
            btnNext.SetTitle("Image " + i.ToString(), UIControlState.Normal);
        }
    }

When I run it in the memory profiler the memory goes from 84MB to 584MB by the time I'm done cycling through the images.
Is there a way I can somehow force a deallocation of memory? Those images being loaded aren't being referenced by anything other than the UIImageView so there shouldn't be any references to any of them once the button is pressed (except of course for the last one).

Thanks in advance! Any help is greatly appreciated!

Edit: The memory also does not go down after I've navigated away from the screen and to my previous screens.

Best Answer

Answers

  • NikiNiki BGMember ✭✭✭

    You could try to manually dispose of the image before loading the new one. Keep in mind that the GC sees the image as a 20 byte pointer and doesn't really know how much memory is allocated for the native object. You could check this out: http://developer.xamarin.com/guides/cross-platform/deployment,_testing,_and_metrics/memory_perf_best_practices/

  • AliceLAliceL USMember

    @Niki Thanks for the link Niki!
    @KMullins Thanks Kevin, that did it!

  • JoeProJoePro CAUniversity ✭✭✭

    What I usually do is keep no more than 3 images in memory at all times:
    -previous image
    -current image
    -next image

    Everything else is disposed of and set to null when no longer needed. From my experience, this provides a more fluid scrolling/browsing experience for the user and doesn't eat up too much memory.

Sign In or Register to comment.