WkHtmlToPdf-DotNet icon indicating copy to clipboard operation
WkHtmlToPdf-DotNet copied to clipboard

Calling in. net8 webapi failed to free memory

Open jinghun1999 opened this issue 1 year ago • 8 comments

We used to generate pdf in .net5 in 2021, then we upgraded to .net6 in 2022 and upgraded to .net8 in 2024,But we found this caused failed to free memory in .net8. I don't know the problem caused by .net8 Or by the WkHtmlToPdf version. The api called 10000 times maybe every day, the memory trend like this. 微信截图_20240712142639

code in my project below:

Startup.cs

services.AddSingleton<WkHtmlToPdfDotNet.Contracts.IConverter>(sp =>
{
    var logger = sp.GetService<ILogger<WkHtmlToPdfDotNet.Contracts.IConverter>>();
    var converter = new WkHtmlToPdfDotNet.SynchronizedConverter(new WkHtmlToPdfDotNet.PdfTools());
    converter.Warning += (sender, e) =>
    {
        logger.LogWarning(e.Message);
    };
    converter.Error += (sender, e) =>
    {
        logger.LogError(e.Message);
    };
    return converter;
});

ValueController.cs

private IConverter converter;
public ValueController(IConverter _converter) 
{
    converter = _converter;
}
[HttpGet]
public async Task<IActionResult> DownChainPdf()
{    
    var doc = new HtmlToPdfDocument()
    {
        GlobalSettings = {
            ColorMode = ColorMode.Color,
            Orientation = Orientation.Portrait,
            PaperSize = PaperKind.A4Plus,
        },
        Objects = {
            new ObjectSettings() {
                PagesCount = true,
                HtmlContent = html, // html string, 100kb
                WebSettings = { DefaultEncoding = "utf-8" },
                HeaderSettings = { FontSize = 9, FontName = "微软雅黑", Line = true, Spacing = 5, Left = $"Gen by:{model.User.UserName}【{model.User.DisplayName}】", Right = $"Gen date:{model.ReportDate.ToOffset(TimeSpan.FromHours(8)).ToString("yyyy-MM-dd HH:mm:ss")}" },
                FooterSettings = { FontSize = 9, FontName = "微软雅黑", Line = true, Spacing = 5, Center = "[page] / [topage]" }
            }
        }
    };
    byte[] pdfData;
    await _PdfSlim.WaitAsync();
    try
    {
        pdfData = converter.Convert(doc);
    }
    finally
    {
        _PdfSlim.Release();
    }            
    Response.Headers.Append("Content-Disposition", "attachment; filename=\"output.pdf\"");
    return File(pdfData, "application/pdf", "output.pdf");
}

jinghun1999 avatar Jul 12 '24 06:07 jinghun1999

It's hard to know exactly what could cause it, but the GC process is a little different in .NET8 vs earlier versions, so maybe that's related. The WkHtmlToPdf package doesn't have specific builds for .NET8, so it's odd that it would be related.

HakanL avatar Jul 12 '24 14:07 HakanL

@Han Thanks a lot. May you test it in .net8, and if it show you the memory issue and can repair it , that's very important and amazing for us.

jinghun1999 avatar Jul 15 '24 03:07 jinghun1999

@han Thanks a lot. May you test it in .net8, and if it show you the memory issue and can repair it , that's very important and amazing for us.

I run it in .NET8, but I don't have good test set up for memory leak testing. We welcome PRs though :)

HakanL avatar Jul 15 '24 14:07 HakanL

See this as well: https://github.com/HakanL/WkHtmlToPdf-DotNet/issues/129

HakanL avatar Jul 17 '24 16:07 HakanL

Also see this: https://github.com/HakanL/WkHtmlToPdf-DotNet/issues/114

HakanL avatar Jul 17 '24 16:07 HakanL

@HakanL I think it is wkhtmltopdf has memory leak. They destroy converter using deleteLater of QT. But it will not process events and never dispose objects because there is no dispatcher.

Kation avatar Jan 02 '25 02:01 Kation

@Kation You're probably right, and unfortunately the main wkhtmltopdf project is not active any more.

HakanL avatar Jan 02 '25 02:01 HakanL

@jinghun1999 Are you running it on Linux?

itamarbareket avatar Jan 10 '25 10:01 itamarbareket