Memory Leak using ToBitmap -- BitmapSource

Bar Code for WPF Forum

Posted 6 years ago by Tim
Version: 13.2.0591
Platform: .NET 4.5
Environment: Windows 8 (64-bit)
Avatar

I'm using the barcode class to generate and print labels.  I ran into an issue where the ToBitmap() was leading to a sort of memory leak.  I did a bunch of research and found out that the BitmapSource class has an issue where it will stay in memory until it goes out of scope and the GC collects it.  This is all fine and dandy, but when generating a large number images using the lib, the program will quickly crash due to memory usage.

Below is an untested example.  In order to prevent the program from eating up to much memory and crashing i've had to call GC.Collect(); and GC.WaitForPendingFinalizers(); which just feels really hacky.

double dpi = double.Parse(textboxDPI.Text.Trim());
int i = 20000000;
while (i != 20010000)
{
Code128Symbology c128 = new Code128Symbology();
c128.QuietZoneThickness = new System.Windows.Thickness(5, 0, 5, 0);
c128.Value = "test-barcode";
c128.BarHeight = 38;
c128.BarWidthRatio = 2;
c128.ValueIntrusionOffset = 5;
c128.ValueDisplayStyle = LinearBarCodeValueDisplayStyle.Centered;

BitmapSource ImageSource = c128.ToBitmap(dpi, dpi);

c128.Dispose();

using (var fileStream = new FileStream(@"C:\Temp\" + Guid.NewGuid() + "_test.png", FileMode.Create))
{
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(ImageSource));
encoder.Save(fileStream);
ImageSource = null;
}

//Hacky GC call to prevent the BitmapSource Memory Leak
//Explicitly call GC and wait for finalizers
System.GC.Collect();
GC.WaitForPendingFinalizers();
i++;
}

Comments (3)

Posted 6 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Tim,

All we are doing is creating a RenderTargetBitmap and rendering to its DrawingContext.  We aren't keeping any references to it after the fact.

If you call Freeze() on the bitmap, does it help? 

Also, this bug report to Microsoft seems related:

https://connect.microsoft.com/VisualStudio/feedback/details/489723/rendertargetbitmap-render-method-causes-a-memory-leak


Actipro Software Support

Posted 6 years ago by Tim
Avatar

Thanks for the reply. 

Calling Freeze() won't do the trick. If I call the ToBitmap from a different thread the BitmapSource will go out of scope and GC will dispose properly. It would be nice if the barcode class was able to output a bitmap that was explicitly disposible.  

I suppose it's more of an issue with the microsoft BitmapSource class, which is unfortunately the only way to output a barcode as bitmap from your library.

 

Posted 6 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Yes, sorry we are using WPF's visual rendering features to write out the bitmap.  Since this sounds like an issue in general with BitmapSources, your workaround is probably the best way to proceed.


Actipro Software Support

The latest build of this product (v2019.1 build 0682) was released 4 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.