Memory Leak 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 unrealizing developers fail to dispose.

Failing to manually dispose each and every  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 and 2022 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){// do stuff}

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

using var stream=myPdfDocument.Stream;

3.Collect Garbage

The Visual Studio debugger memory profiler may go up and up 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
  • There is have an iDisposable object open

One way to do this is:

System.GC.Collect();

System.GC.WaitForPendingFinalizers();

System.GC.Collect();

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

4. If you still have a Memory Leak - Report It. This will be respected as an extremely high priority:

Please read this guide, which instructs on how to find your log files and report in such a way that there will not be a requirement to request additional information.

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

https://iron.helpscoutdocs.com/article/118-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.