locked
Out of Memory Exception - WinRT

    Question

  • Greetings,

    I have a fairly simple program that tries to read all the graphic files in my Picture Library.  I create a bitmapImage for each file and I never use it or reference it.  After reading 61 files, I get an Out of Memory exception - it is reproducible, it always dies on the 61st file.

    BitmapImage bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
    Windows.Storage.Streams.IRandomAccessStream fileStream =
          await pic.OpenAsync(Windows.Storage.FileAccessMode.Read);
          await bitmapImage.SetSourceAsync(fileStream);
    

    The above code snippet shows the gist of what I am doing.  I create a bitmapImage in a method, return it to the caller and never keep it or reference it.

    Given that I have kept no references to the bitmapImages, shouldn't GC collect the unreferenced bitmapImages?

    Any help or pointers would be much appreciated.

    Thanks,

    Frank

    • Moved by CoolDadTxMVP Tuesday, September 16, 2014 2:42 PM WinStore related
    Tuesday, September 16, 2014 2:37 PM

Answers

  • Necessarily dispose stream after set it to image.

    Your code may fails due to the big images or images which more than your image in xaml.

    You can try set source only after resize images for ex.


    Wednesday, September 17, 2014 5:49 PM

All replies

  • Hi, flynch55.

    Try dispose image stream after you set it to image.

    Tuesday, September 16, 2014 6:23 PM
  • I tried adding filestream.dispose() but no change in results.

    Thanks for the suggestion.

    Tuesday, September 16, 2014 8:36 PM
  • Which size at images and which size at bitmap?
    Wednesday, September 17, 2014 5:22 AM
  • I had the same issue when dealing with Data.Pdf in the Windows 8.1 SDK, which basically splits *.pdf files into images and puts them into a StackPanel.

    Searched on StackOverflow but haven't found anything so far.

    Wednesday, September 17, 2014 5:53 AM
  • The images are varying sizes most are around 500 k but there some that are 22 meg.

    I did not specify a size for the bitmap (as shown in the code).  I am assuming this means that it will be full size.

    In separate experiments, when I use a thumbnail bitmap size of 320, it works like a charm.  But that does not explain the problem I am having with the code above.  I would like to understand why the code above fails - I don't think it should fail but it does therefore there is something wrong with my understanding of how things work.

    Wednesday, September 17, 2014 2:35 PM
  • Necessarily dispose stream after set it to image.

    Your code may fails due to the big images or images which more than your image in xaml.

    You can try set source only after resize images for ex.


    Wednesday, September 17, 2014 5:49 PM
  • I developing Gallery and i have problem with out of memory exception too. 

    When image what i try to show has big resolution (10000x 5000px) my application shows consumption about 450 MB of memory initially and after few seconds 250MB.

    I resolved it to size down resolution. Consumption decreased to 170mb initialy and after few seconds 120MB.

    When i tried to load and show 10000x 5000px resolution photos sometimes it end with success sometimes failed on OutOfMemoryEx.

    I use WriteableBitmap.



    • Edited by Peter Malík Wednesday, October 1, 2014 11:31 PM
    Wednesday, October 1, 2014 11:28 PM