HiraokaHyperTools / PDFsharp

Just xps2pdf
MIT License
16 stars 5 forks source link

Memory Leak #7

Closed mikihiro-t closed 4 years ago

mikihiro-t commented 5 years ago

Thank you for your kenjiuno.PdfSharp. I have made a solution with this.

Convert FlowDocument to XPS. And then Convert XPS to PDF files.

Problem is when many XPS Files to PDF files, this application crashes.

For Example,

      for (int i = 0; i < 500; i++)
      {
        var xpsPath = @"C:\test\test.xps";
        PdfSharp.Xps.XpsConverter.Convert(xpsPath);
      }

Process Memory increases: image

SnapShot of Memory: Memory sizes with PdfSharp.Fonts are increased. image

test.xps is like this image

Could you help me?

Note: PdfSharp.Drawing\XFontFamily.cs I have changed this line(267) for Visual Studio 2017 old

    families = FontFamily.GetFamilies(graphics.gfx);

new

    families = FontFamily.Families;
kenjiuno commented 5 years ago

@mikihiro-t thanks for report! I'll check about this

kenjiuno commented 5 years ago

Ok I have fixed in #8 1.1.3 will be available soon https://www.nuget.org/packages/kenjiuno.PdfSharp.Xps/1.1.3

mikihiro-t commented 5 years ago

Thank you for your help(1.1.3). I will try.

I have traced the code(1.1.2). This static Global has not disposed. When a xps file converted, Global.table.Count increses.

I'm not sure this code is good. But this works.

FontDescriptorStock.cs image

    public static void DisposeGlobal()
    {
      global = null;
    }

XpsConverter.cs

At the last in Convert Method , call DisposeGlobal.

public static void Convert(string xpsFilename, string pdfFilename, int docIndex, bool createComparisonDocument)

image

PdfSharp.Fonts.FontDescriptorStock.DisposeGlobal(); 

Maybe this method(Stream) needs the same code.

public static void Convert(Stream xpsInStream, Stream pdfOutStream, bool closePdfStream)

image

mikihiro-t commented 5 years ago

Thank you very much. It was very helpful. Ver 1.1.3 works well.

Note: FontFamily.GetFamilies(graphics.gfx) turning off "Warning as error" https://forum.pdfsharp.net/viewtopic.php?f=2&t=903 So I can use the original code.

kenjiuno commented 5 years ago
PdfSharp.Fonts.FontDescriptorStock.DisposeGlobal(); 

I'm not sure that keep using FontDescriptorStock.global is good idea.

So, in 1.1.3 I have added FontDescriptorStock.NewInstance and replaced all Global references to NewInstance not to rely on global cache mechanism.

I'm not sure about this too, however I have selected this since:

PdfSharp.Drawing\XFontFamily.cs I have changed this line(267) for Visual Studio 2017

Thanks, I have fixed it in https://github.com/HiraokaHyperTools/PDFsharp/pull/6 Current master branch should provide merged code.

kenjiuno commented 4 years ago

I'll close this issue due to long time inactivity. Please reopen or start new issue, if you still have problem...