1

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();
    }
fnx
  • 369
  • 1
  • 5
  • 16

2 Answers2

0

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.

Community
  • 1
  • 1
Oblivious Sage
  • 3,326
  • 6
  • 37
  • 58
  • @fnx [MSDN](https://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage_methods%28v=vs.110%29.aspx) says there is... – Oblivious Sage Jun 01 '15 at 19:29
  • 1
    @fnx Certainly possible, though it seems a little odd that they wouldn't give it a slightly different name or something to help distinguish it. At any rate, from what you've shown it seems likely that BitmapImage isn't properly being garbage collected. You might want to find a free memory profiler that works with Windows Phone and see if you can figure out what's keeping a reference to it. – Oblivious Sage Jun 01 '15 at 20:15
0

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?

Community
  • 1
  • 1
Dinesh balan
  • 495
  • 4
  • 15