Memory leak in BitmapImage when setting UriSource RRS feed

  • Question

  • We have an app that lets users browse their photos on DLNA devices.  DLNA provides URIs for the images on the device and so we set the UriSource of a BitmapImage object to the image uri.  We've noticed that when we use the UriSource property, the BitmapImage leaks the memory it used to download the image from the internet.  Here is some sample code

    var bitmapImage = new BitmapImage();
    bitmapImage.DecodePixelHeight = 1080;
    bitmapImage.UriSource = new Uri(imageUris[imageIndex]);

    It may be specific to large images (1MB or larger) since that's how we first noticed it.  I've created a very simple repro application.

    We've done a ton of analysis using Windbg, PerfView, VMMap and Visual Studio.  In VMMap we can see that the leaked memory is in the Private Data section in VMMap.  That means it's either thread storage or allocated with VirtualAlloc.  When examining the large memory chunks in the Private Data section in Visual Studio we could see that they contained the jpg image data for the images.  If you run the sample app, each one of the images ends up in Private Data and never gets cleaned up even though the BitmapImage objects are garbage collected.

    We've tried everything we can think of to force the BitmapImage to release the memory it used to download the image but nothing cleans it up.  We've tried using  RandomAccessStreamReference to create a RandomAccessStream over the URI and doing a SetSourceAsync.  That doesn't leak the image memory, but it does cause a hang (http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/6eaada1c-c2da-494f-bcce-d0ba2807adcb).

    I've uploaded a sample solution that repros the memory leak to skydrive:



    Thursday, February 21, 2013 1:43 AM

All replies

  • On first run there was no memory leak happening. However on subsequent runs I see the steady memory increase, this isn't to say it's leaking though.

    Could this be due to image caching that winrt is doing on your behalf?

    If you look in: C:\Users\username\AppData\Local\Packages\4e5f3807-e58f-4e6d-93dd-feb13206e055_wz8705ndd7nw4\AC\INetCache after the first run you'll find your cached images.

    Deleting the INetCache and re-running shows the first run behaviour.

    You can only really know for sure if this is a memory leak by stress testing the app and starving your system of memory. If it's not reclaimed at this point then it's a valid memory leak.

    British Airways Inspiration App & rara music

    Thursday, February 21, 2013 11:03 AM
  • @Flexman: Interesting observation about INetCache. 

    In our actual app, the memory usage steadily increases until the app crashes due to an out of memory exception at 1GB.

    Thursday, February 21, 2013 5:14 PM