Reputation: 5921
I'm working on Image gallery for window phone 8.1, where user can see images one by one like book of images. where you can turn image and view another image.
Right now, this gallery application consume to much memory and I'm getting out of memory exception after some time while using application.
I'm using below code to load image into usercontrol.
StorageFile f1 = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx://" + Physical path of application for getting image);
using (IRandomAccessStream stream1 = await f1.OpenAsync(FileAccessMode.Read))
{
bitmapImage1 = new BitmapImage();
await bitmapImage1.SetSourceAsync(stream1);
nextimg = bitmapImage1;
stream1.Dispose();
}
Can anyone help to guide here regarding memory consumption?
Upvotes: 1
Views: 627
Reputation: 683
First of all, it seems that somebody already asked a similar question: Memory not being relieved when setting BitmapImage to null.
But still, I googled a little bit and found a complete investigation of the issue here: Memory leak with BitmapImage. While it's worth reading, here are a few solutions suggested in this article.
Set your BitmapImage
's source to an empty stream, OR
Set your BitmapImage.UriSource
to null, OR
When the image is opened (BitmapImage.ImageOpened
event is raised), unsubscribe from BitmapImage.ImageOpened
and BitmapImage.ImageFailed
events.
And by the way, why do you call stream1.Dispose()
? The using
statement implies this call.
Upvotes: 1