Memory leak when saving HTML to PDF conversion (.net 5)

Hello. I’ve been chasing down a memory leak in our app for some time. We have a .net 5 web api whose function is to convert HTML into a PDF. We are using Aspose.pdf to convert the HTML string into a PDF as below. What I’m finding is my memory increases over load to the point that we start to run out of memory for the service. I’ve found several instances of this in the forums, and I’ve implemented several of the suggested fixes - such as moving all of the aspose-related code to it’s own class, implementing the IDisposable interface on that class, and then doing a manual garbage collection call in the dispose method of the class to remove everything. While this does help keep the heap under control somewhat, I’m still seeing a huge number of objects from the Aspose.dll being added to the heap, causing this memory leak.

Here’s my class method with the dispose method below.

public async Task WritePDFContent(HtmlPropertiesModel htmlResponse)
{

        #region ASPSOSE.PDF

        // Create HTML load options
        HtmlLoadOptions HtmlLoadoptions = new HtmlLoadOptions();

        // set the PageInfo,in Aspose.Pdf 1 inch = 72 points

        HtmlLoadoptions.PageInfo.Margin.Bottom = 0;
        HtmlLoadoptions.PageInfo.Margin.Top = 0;
        HtmlLoadoptions.PageInfo.Margin.Left = 0;
        HtmlLoadoptions.PageInfo.Margin.Right = 0;

        HtmlLoadoptions.PageInfo.IsLandscape = false;
        HtmlLoadoptions.PageInfo.Height = 792;
        HtmlLoadoptions.PageInfo.Width = 612;

        var pdfFilePath = htmlResponse.PDFFilepath;
        var pdfFileName = Path.GetFileName(pdfFilePath);
        var pdfFileDirectoryName = Path.GetDirectoryName(pdfFilePath);

        // Create Directory If Doesn't exists
        if (!Directory.Exists(Path.GetDirectoryName(pdfFilePath)))
        {
            Directory.CreateDirectory(Path.GetDirectoryName(pdfFilePath));
        }

        // Load HTML into new document
        // convert string to stream

        byte[] byteArray = Encoding.UTF8.GetBytes(htmlResponse.HtmlBody);
        using (var stream = new MemoryStream(byteArray))
        {
            using (Document doc = new Document(stream, HtmlLoadoptions))
            {
                doc.Save(Path.Join(pdfFileDirectoryName, pdfFileName));
                foreach (Page page in doc.Pages)
                {
                    page.FreeMemory();
                    page.Dispose();
                }
                doc.FreeMemory();
                doc.Dispose();
            }
        }

        await Task.CompletedTask;

        #endregion

    }
    public void Dispose()
    {
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
    }

When running and comparing snapshots, I can see the memory increase and never come down. These objects all have references back to the aspose library when I right click and view definition.

image.png (34.0 KB)
image.png (26.1 KB)

This is the same with even the latest stable version (although I get way more exceptions when I try to run the latest version with .net 5, so my next option is to upgrade to .net 6 and try the latest version.

If you have any suggestions on how I might start to solve this, please let me know.

@tstahl9479

If you can please share your sample source HTML in .zip format with us, we will test the scenario in our environment and address it accordingly.

Hi, do you have an email I can send it to? It would be better if I can send an example to you without it being on the forum publicly. Thanks!

@tstahl9479

You can send your file in a private message as shown in the attached image. image.png (11.3 KB)

@tstahl9479

We have received your private message and noticed that you are using 22.1 version of the API which is quite old. Also, you mentioned that you are facing some errors when you try to upgrade the API version? Can you please share some details because these errors are not expected and no such issue is know while upgrading the API version. It would be recommended to once test the scenario using 22.10 version of the API as it has performance improvements.

Hi! When I upgrade to version 22.10.0 (while still running .net 5) I’m getting the following exceptions being thrown from Aspose.PDF.dll:

Exception caught: ‘System.InvalidOperationException’ in Aspose.PDF.dll (“Cannot find ‘GSUB’ in the font file.”)

Exception thrown: ‘System.InvalidOperationException’ in Aspose.PDF.dll ('Cannot find table ‘GPOS’ in the font file.")

These seem to be thrown constantly while the files are being processed. The files are generated, but the performance is considerably slower and the exception events in the debuging profile seem to indicate big problems in the code. Is there another component I need to install to correct this?

@tstahl9479

We are checking it and will get back to you shortly.