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.