Reputation: 597
From what I read, whenever you assign an image to a XAML element via its Source property, there is some sort of smart caching going on.
Unfortunately for my app (Flash Cards), I am dynamically switching the image.Source property every time the user flicks (aka swipes) the card in front of him/her. It's the same XAML element, but its Source property gets replaced every time.
I have 4 sets of Flash Cards, and the app holds on just fine if I pick up to 3 sets at a time, but if I pick 4 sets when the count reaches around the 52nd or 54th card, the memory use surpasses 150MB (which is the set limit) and my test device (Nokia Lumia 520) crashes with an out of memory error.
After profiling the app, I pinned down the issue to be caused by the image loading event.
Any ideas?
Upvotes: 1
Views: 400
Reputation: 15006
From my experience, nulling the things you won't be needing anymore out can be helpful when working with images.
Considering you're using a BitmapImage and create a new one every time, I would try setting the bitmap image UriSource to null
bmpImage.UriSource = null;
before nulling the BitmapImage itself
bmpImage = null;
before the BitmapImage object you created goes out of scope and before you create a new one and assign it to Image as source. (of course)
In case you're using multiple Image objects, I'd set their Source to null, too. (for example, when creating some complex image overlays in code behind etc.)
That helped me a lot when working with BitmapImages, especially in background task.
Upvotes: 1