5

I seem to be having some big memory consumption issues. When I first load my wpf application which contains a gridview and an observablecollection the app is around 10mb.

When I click on an item in the gridview it opens another window which contains an image control which gets passed a base64 string that I then convert into a BitmapImage

The application then jumps up to around 123mb from 10mb. The original image size is 64k but all my stored images are base64 strings that I convert back to byte[] then into a BitmapImage. Yes I mean to do this.

When I close the window none of the ram gets release. I've even tried calling GC.

I use the following code to turn the base64 image into

var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.None;
bitmapImage.StreamSource = new SIO.MemoryStream(imageBytes);
bitmapImage.EndInit();
return bitmapImage;

That then gets assigned to Image.Source

Tsukasa
  • 6,342
  • 16
  • 64
  • 96

2 Answers2

2

Below are some tips and guesses, however if you used a memory profiler you would be able see what is taking up the memory. (e.g. the CLR profiler, also VS 2012 ad 2013 come in-built memory profile tools, and other commercial ones: .NET Memory Profiling Tools)


  • Why do you specify CacheOption BitmapCacheOption.None from here that says:

Do not create a memory store. All requests for the image are filled directly by the image file.

Instead you could use OnLoad:

Caches the entire image into memory at load time. All requests for image data are filled from the memory store.

Which I read as: if you are displaying the image in many locations they share the same underlying memory. So if you are showing the same image in more than one place that would certainly be preferable.

  • Another tip found in the comments of an example here:

To save significant application memory, set the DecodePixelWidth or
DecodePixelHeight of the BitmapImage value of the image source to the desired height and width of the rendered image. If you don't do this, the application will cache the image as though it were rendered as its normal size rather then just the size that is displayed.

  • Why are you using base64 strings, why not use binary data, i.e. byte[]s? How many copies of each string do you hold a reference to?
Community
  • 1
  • 1
markmnl
  • 11,116
  • 8
  • 73
  • 109
  • Thanks profiler pointed me in the right direction and have now dropped my ram usage from 123mb down to around 25mb which is reasonable. – Tsukasa Dec 18 '13 at 05:45
  • @Tsukasa, I am curious, what was the problem? – markmnl Dec 18 '13 at 05:58
  • It actually had to do with a crypto class I was working on and forgot I was calling it. That's what was eating ram as it wasn't releasing the objects. I wasn't aware of the profiling tools. Think I need to make a habit of using them from now on. – Tsukasa Dec 18 '13 at 06:15
0

agreed BitMap doesnt release its memmory (tried and got tired with the memmory leak) . however try this link

Read the last comment may be this workaround might solve the problem