3

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?

  • My guess would be that the constructor is having to perform some conversion when it initializes your object, and it's having to allocate memory to do that. It then releases the resources, but the memory is not immediately garbage-collected. Is this an actual problem, or you're just worried about graphs of memory usage you see on the screen? – Cody Gray - on strike Feb 05 '16 at 13:29
  • I'm worried about the memory consumption because it will live in a 32-bit IIS worker process that already runs hot in terms of the memory usage. I've seen the worker process completely bomb out when the memory safeguards are hit in IIS (though other problems) and adding several hundred MB of usage even if it's only for a matter of seconds while the lifecycle of the object is used could cause some serious problems in production. I don't think that getting the general memory usage of the worker process is realistic, so I'm left considering a separate process which just feels dirty. – Neil Pendlington Feb 05 '16 at 15:23
  • When you load multiple images, the memory consumption does not linearly increase. In that case, it should not be such a big concern if you are worried about the scalability of your approach – Vikhram Feb 06 '16 at 12:42
  • You're right that the memory usage doesn't increase with further loads, but my concern is that a single image load could trip up the worker process and bring down a production web service. Ultimately, if it's just working the way `WriteableBitmap` is supposed to (and that's going to take either some expert input or some serious reflection) then I'm left either making something off-process or using a different way to manipulate images, which would be a shame since I've written so much code! :) – Neil Pendlington Feb 08 '16 at 14:42

0 Answers0