Search code examples
c#memory-managementmemory-leaksdisposepdfsharp

PDFSharp not disposing resources (memory leak)


I am writing a little tool for myself to merge PDF files using PDFSharp library. I am using the latest pre-release version (1.5) of PDFSharp.

I come across a problem where documents that are loaded into memory is not releasing when going out of scope. I tracked down this memory leak to the following part of the code:

using (var mergedDocument = new PdfDocument())
{
    for (var i = 0; i < SelectedDocuments.Count; i++)
    {
        using (var document = PdfReader.Open(SelectedDocuments[i].FilePath, PdfDocumentOpenMode.Import))
        {
            for (var j = 0; j < document.PageCount; j++)
            {
                mergedDocument.AddPage(document.Pages[j]);
            }
        }
    }
    mergedDocument.Save(savePath);
}

An example would be I have 10 pdf documents which totals on 178 Mb. The merged document that is created is also around 178 Mb. When above code finishes executing memory usage holds at 356 Mb. When I merge more documents this memory leak keeps going up and eventually cause a crash.

I tried removing using statements and using Dispose() when I wanted the document to be released from memory, however it does not work as well.

Any help would be appreciated. Thank you.

Edit:

To be more precise:

        var parentDirectory = Directory.GetParent(SelectedDocuments[0].FilePath);
        var savePath = parentDirectory + "\\MergedDocument.pdf";

        using (var mergedDocument = new PdfDocument())
        {
            for (var i = 0; i < SelectedDocuments.Count; i++)
            {
                using (var document = PdfReader.Open(SelectedDocuments[i].FilePath, PdfDocumentOpenMode.Import))
                {
                    for (var j = 0; j < document.PageCount; j++)
                    {
                        mergedDocument.AddPage(document.Pages[j]);
                    }
                }
            }
            mergedDocument.Save(savePath);
        }

SelectedDocuments is a list which holds a bunch of file paths to the selected PDF files.


Solution

  • I ended up using iTextSharp instead with the following code to avoid memory issues:

    var parentDirectory = Directory.GetParent(SelectedDocuments[0].FilePath);
    var savePath = parentDirectory + "\\MergedDocument.pdf";
    
    using (var fs = new FileStream(savePath, FileMode.Create))
    {
        using (var document = new Document())
        {
            using (var pdfCopy = new PdfCopy(document, fs))
            {
                document.Open();
                for (var i = 0; i < SelectedDocuments.Count; i++)
                {
                    using (var pdfReader = new PdfReader(SelectedDocuments[i].FilePath))
                    {
                        for (var page = 0; page < pdfReader.NumberOfPages;)
                        {
                            pdfCopy.AddPage(pdfCopy.GetImportedPage(pdfReader, ++page));
                        }
                    }
                }
            }
        }
    }