Search code examples
c#.netxamlwindows-store-appswin-universal-app

Memory leak by an Image in a ScrollViewer


I have an Image in a ScrollViewer and I'm changing the BitmapImage that's the Source of the Image from time to time. I've discovered that the App's memory usage surpasses 2GB even though the images are only around a total of 100MB! (I didn't know more than 2GB can ever be allocated to one app) And sometime it crashes. The memory usage does get smaller every so often, but not fast enough. My assumption (due to lack of a better one, any other ideas are welcome) is that the ScrollViewer is caching the images and does not release that cache even after the BitmapImage is replaced by another one.

Is there a way to tell the ScrollViewer not to cache the images?

Additionally, how can I check where is the memory going to? I'm using VS2015, and all that Diagnostic Tools shows me is the total memory used.


Solution

  • VisualStudio 2013 will allow you to profile memory usage. Alternatively you can use PerfView (provided by Microsoft). Both will show you which objects are eating up your memory. I have successfully used both these options to determine the root cause of problems similar to yours. There are also several third party tools that do the same thing.

    Perhaps the replies to this other question are helpful. It seems you need to freeze the BitmapImages before using them as a source for your Image and ScrollViewer.

    I would have posted this as a comment since I don't actually have a solution but I don't have enough rep.