Out Of Memory-exception when navigating to page

145 Views Asked by At

I'm making a simple Windows Phone 8.1 Silverlight app. The idea is that I can make an entry with a picture (taken with the camera) and add a title and description text to it. Once an entry is saved, a button appears on the main page to view it. So I made 3 entries and they are listed on the main page, but after navigating to their pages a few times, I get a NavigationFailed along with OutOfMemoryException. The pages are simple, they only contain 1 image along with some textblocks.

I thought the issue is that the images are still in memory, that's why I try to set them to null and force the garbage collector, but that didn't help at all. What could cause the OutOfMemory-exception?

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        string id= "";
        if (NavigationContext.QueryString.TryGetValue("id", out id))
        {
            foreach (cEntry entry in helper.entries)
            { 
                if (entry.id.ToString() == id)
                {
                    textBlock_viewText.Text = entry.text;
                    textBlock_viewTitle.Text = entry.title;

                    using (IsolatedStorageFile userStore = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (userStore.FileExists(entry.imageFileName))
                        {
                            using (IsolatedStorageFileStream imgStream = userStore.OpenFile(entry.imageFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                            {
                                BitmapImage bmp = new BitmapImage();
                                bmp.SetSource(imgStream);
                                image_viewEntryImage.Source = bmp;
                                bmp = null;
                            }
                        }
                    }
                }
            }
        }
    }

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);
        image_viewEntryImage.Source = null;
        GC.Collect();
    }
2

There are 2 best solutions below

2
Oblivious Sage On

You may need to freeze the BitmapImage.

As described here there is an issue with WPF (the typical framework for Windows Phone development) where BitmapImages can be incorrectly kept alive. While it was supposedly fixed a while back, people have reported still seeing the problem in some cases.

0
Dinesh balan On

Instead of setting bmp as null try this.

 public static void DisposeImage(BitmapImage image)
{
    Uri uri= new Uri("oneXone.png", UriKind.Relative);
    StreamResourceInfo sr=Application.GetResourceStream(uri);
    try
    {
        using (Stream stream=sr.Stream)
        {
            image.DecodePixelWidth=1; //This is essential!
            image.SetSource(stream);
        }
    }
    catch { }
}

call this method and set source as this custom method after that assing bmp as null. The GC not able to clear the memory. details are here Why do I get an OutOfMemoryException when I have images in my ListBox?