1

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.

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • Do you dispose the bitmap when you remove it from the scrollviewer? – Gian Paolo Oct 13 '15 at 17:42
  • @GianPaolo a) It is a static group of BitmapImages that totals 100MB. b) How exactly would I dispose of a BitmapImage anyway (before you answer this - see [this](http://stackoverflow.com/questions/33108983/dispose-of-a-bitmapimage)). – ispiro Oct 13 '15 at 17:45
  • How do you add the Images to the ScrollViewer, directly in XAML? – Dani Oct 13 '15 at 17:57
  • BitmapImages created from URIs are cached by the runtime. Try to load them from a stream by means of the SetSourceAsync method. – Clemens Oct 13 '15 at 18:02
  • @Dani There is only one Image (added programmatically). It's Source changes. It's always one of a specific collection of BitmapImages that are all in memory. – ispiro Oct 13 '15 at 18:03
  • @Clemens The BitmapImages are, in fact, loaded by streams. Although I doubt it matters in this case because they're never reloaded. They are loaded once. – ispiro Oct 13 '15 at 18:04
  • Did you see that? https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.imaging.bitmapcreateoptions – Dani Oct 13 '15 at 18:14
  • @Dani That is for _creating_ the BitmapImage. In my case - the problem is later when using them. But thanks. – ispiro Oct 13 '15 at 18:31

1 Answers1

3

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.

Community
  • 1
  • 1
Carl
  • 898
  • 9
  • 16
  • Thanks. Freeze isn't available in UWP. As for the others - thanks. They are, at least, a partial answer. +1. – ispiro Oct 13 '15 at 18:39