Fixing Memory Leaks in IronPDF

If you are experiencing an apparent memory leak in IronPDF, we want to know about it. Our most senior engineers will dog-pile onto a memory leak to work on a hot-fix once identified.

This is how to report a memory leak to support@ironsoftware.com :

1. Update to the latest IronPdf version

If you are not already, please update to the latest IronPdf version.

2. Make Sure You Have Disposed Your IDisposable Objects

The vast majority of reported memory leaks are caused by improper use of the .NET IDisposable interface.

If any .NET class has a Dispose() method - it is probably IDisposable and will require the developer to tell it when they are finished using it.

There is a common misunderstanding that C# is a "managed" language, requiring no developer responsibility to manage memory. Contrary to this belief, there are in fact many common .NET objects that developers fail to dispose of.

Failing to manually dispose of each IDisposable class instance can cause a memory leak in your code.

  • System.IO.Stream - which is returned by the PdfDocument.Stream property.
  • System.Drawing.Image / System.Drawing.Bitmap - which is returned by the PdfDocument.PageToBitmap method.
  • IronPdf.PdfDocument - itself is marked IDisposable as well, as it may contain unmanaged objects in our later 2021 - 2024 releases.

The most common solution

The best solution is often to use a using statement when referring to IDisposable objects.

using(var stream = myPdfDocument.Stream) {
    // Perform operations with the stream here
}
using(var stream = myPdfDocument.Stream) {
    // Perform operations with the stream here
}
Using stream = myPdfDocument.Stream
	' Perform operations with the stream here
End Using
$vbLabelText   $csharpLabel

In C# 8, there is even a shorthand version without {} closures.

using var stream = myPdfDocument.Stream;
// Perform operations with the stream here
using var stream = myPdfDocument.Stream;
// Perform operations with the stream here
Dim stream = myPdfDocument.Stream
' Perform operations with the stream here
$vbLabelText   $csharpLabel

3. Collect Garbage

The Visual Studio debugger memory profiler may continue showing an increase even if nothing is wrong. When using a high RAM system, the .NET runtime may decide that it is more efficient to allow garbage to sit in memory until your system RAM is nearly full or even use a swap file to keep.

It is possible to manually instruct the .NET garbage collector to dispose of its unused objects at a safe point in your application lifecycle when:

  • Not rendering a PDF
  • An IDisposable object is open

One way to do this is:

System.GC.Collect(); // Invokes the garbage collector
System.GC.WaitForPendingFinalizers(); // Waits for the process to complete
System.GC.Collect(); // Optional: Runs additional collection to ensure all objects are cleared
System.GC.Collect(); // Invokes the garbage collector
System.GC.WaitForPendingFinalizers(); // Waits for the process to complete
System.GC.Collect(); // Optional: Runs additional collection to ensure all objects are cleared
System.GC.Collect() ' Invokes the garbage collector
System.GC.WaitForPendingFinalizers() ' Waits for the process to complete
System.GC.Collect() ' Optional: Runs additional collection to ensure all objects are cleared
$vbLabelText   $csharpLabel

After this, the memory usage graph should drop to a normal, but non-zero level.

4. If you still have a Memory Leak - Report It

This will be respected as an extremely high priority. Please read this guide, which explains how to find your log files and report your issue in such a way that additional information will not be requested.

This 3-minute read will help us reproduce your issue with 100% accuracy, ensuring we do not waste your time.

Engineering Request PDF

Thank you - No one likes memory leaks, including us. When working with "low-level" or system objects like HTML rendering, Interop, Graphics, and Streams, they do become possible. So let's fix them!

IronPDF has only become what it is today from listening to our users' bug reports and feature requests, so thank you for your support.