I am using the latest version of .NET Aspose.HTML 25.2.0. I noticed that converting a single HTML to PDF document does not consume a large amount of resources, and running that process multiple times sequentially does not seem to cause any memory issues.
However, when running the code on multiple tasks in parallel, the memory usage jumps significantly and will remain high without being cleaned up. I can provide a code sample to illustrate this.
class Program
{
static void Main(string[] args)
{
MainAsync(args).Wait();
}
static async Task MainAsync(string[] args)
{
Action convertHtmlToPdf = () =>
{
const string TestingHtml = @".\testing.html";
Converter.ConvertHTML(TestingHtml, new PdfSaveOptions(), $@".\{Guid.NewGuid()}.pdf");
Console.WriteLine($"Converted 1 document");
};
Func<int, Task> convertManyHtmlToPdfAsync = async (convertAmount) =>
{
List<Task> convertTasks = new List<Task>();
for (int counter = 0; counter < convertAmount; counter++)
{
convertTasks.Add(Task.Run(convertHtmlToPdf));
}
await Task.WhenAll(convertTasks).ConfigureAwait(false);
Console.WriteLine($"Converted {convertAmount} documents");
};
while (true)
{
Console.WriteLine("Press any key to convert the next batch...");
Console.ReadKey();
//convertHtmlToPdf(); // One at a time to see memory usage is fine
await convertManyHtmlToPdfAsync(20); // Many at a time to see memory usage increase over time
Console.WriteLine("Check memory footprint of this process!");
await Task.Delay(0).ConfigureAwait(false);
}
}
}
It seems you are experiencing significant memory usage when converting HTML to PDF using Aspose.HTML in parallel tasks. This issue is not uncommon, especially when dealing with multiple concurrent operations that can lead to resource contention and memory not being released properly.
Here are a few suggestions to help mitigate the memory leak issue:
Dispose of Resources: Ensure that you are properly disposing of any resources after the conversion. Use using statements to automatically dispose of objects like HTMLDocument and PdfSaveOptions once they are no longer needed.
using (var document = new HTMLDocument("your_html_content"))
{
var options = new PdfSaveOptions();
Converter.ConvertHTML(document, options, "output.pdf");
}
Limit Parallelism: If possible, limit the number of parallel tasks. This can help reduce the peak memory usage. You can use a SemaphoreSlim to control the number of concurrent conversions.
var semaphore = new SemaphoreSlim(5); // Limit to 5 concurrent tasks
async Task ConvertHtmlToPdfAsync(string htmlContent)
{
await semaphore.WaitAsync();
try
{
using (var document = new HTMLDocument(htmlContent))
{
var options = new PdfSaveOptions();
Converter.ConvertHTML(document, options, "output.pdf");
}
}
finally
{
semaphore.Release();
}
}
Monitor Memory Usage: Use profiling tools to monitor memory usage during the conversion process. This can help identify specific areas where memory is not being released.
Update to Latest Version: Ensure you are using the latest version of Aspose.HTML, as memory management improvements and bug fixes are often included in new releases.
Report the Issue: If the problem persists, consider reporting it to Aspose support with details about your environment, the HTML content being converted, and any profiling data you have collected. This can help the development team address the issue in future updates.
By following these steps, you should be able to manage memory usage more effectively during HTML to PDF conversions. If you have a specific code sample that illustrates the issue, feel free to share it for more tailored advice.
I am just bumping this because I haven’t yet seen a response from any support. I think the real issue is that memory spikes are caused by parallel use of Converter.ConvertHTML. Is this class/method intended to be used with a gate to prevent parallel use? Is there a better alternative for a high-traffic web environment serving many requests at a time?
Thanks for your patience. We need to investigate this behavior of the API and have opened the following new ticket(s) in our internal issue tracking system. We will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): HTMLNET-6193
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
1 Like
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
Enables storage, such as cookies, related to analytics.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.