I've been writing an assembly to handle some basic image manipulation and have hit a stumbling block that I am a bit stuck on resolving.
The crux of the issue is when I create a new System.Windows.Media.Imaging.WriteableBitmap
, the resultant private bytes for the process jumps to an unbelievable amount.
My test console app has the following code and a couple of breakpoints to see when the memory usage spikes.
using (Stream imageStreamSource = new FileStream(@"c:\temp\input\snow.jpg", FileMode.Open, FileAccess.Read, FileShare.Read))
{
BitmapImage bmpi = new BitmapImage();
bmpi.BeginInit();
bmpi.CreateOptions = BitmapCreateOptions.None;
bmpi.StreamSource = imageStreamSource;
bmpi.EndInit();
var bmp = new WriteableBitmap(bmpi);
}
At the end of the .EndInit()
call, the memory usage is only a few hundred bytes, but once the WriteableBitmap
is instantiated, the memory jumps to several hundred MB.
Memory Usage shown in Visual Studio
The image I'm trying to process isn't the world's largest (4134x2924) and is less than 300KB in size. A quick calculation of what it would take in memory to represent 32bpp comes to about 40MB. Unless I'm missing something obvious, something looks wrong.
Image I'm working on as a test (snow.jpg)
I've read about possible memory leaks in WritableBitmap in older versions of Silverlight, but I'm using .net 4.0 and I've yet to see reports of a leak in this version.
Does anyone have any insight or solutions to this as it does not scale to what will be a web process eating that much memory in production?