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
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)
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.
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.
byte[]
s? How many copies of each string do you hold a reference to?If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With