Completed
Last Updated: 01 Nov 2021 16:08 by ADMIN
Release R3 2021 SP1
Victor
Created on: 08 Sep 2021 05:54
Category: PdfViewer
Type: Bug Report
1
PdfViewer: Memory leaks when opening a large file several times.
Memory leaks when opening a large file several times. 
1 comment
ADMIN
Hristo
Posted on: 19 Oct 2021 16:02

Hello Victor,

A possible workaround is to handle the DocumentUnloaded event of the element and clear the objects causing the memory leak:

private void PdfViewerElement_DocumentUnloaded(object sender, EventArgs e)
{
    RadPdfViewerContainer container = this.PdfViewer.PdfViewerElement.Parent as RadPdfViewerContainer;
    if (container != null)
    {
        foreach (var node in container.BookmarksTree.TreeViewElement.GetNodes())
        {
            node.Tag = null;
        }

        container.BookmarksTree.Nodes.Clear();
    }

    typeof(RadPdfViewerElement).GetField("previousPage", BindingFlags.NonPublic | BindingFlags.Instance).
                                 SetValue(this.PdfViewer.PdfViewerElement, null);

}

Additionally, it may also be necessary to force the GC:

// If running on .NET 4.5.1 and later
// https://docs.microsoft.com/en-us/dotnet/api/system.runtime.gcsettings.largeobjectheapcompactionmode?view=net-5.0#System_Runtime_GCSettings_LargeObjectHeapCompactionMode
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
//

GC.Collect();
GC.WaitForPendingFinalizers();
GC.WaitForFullGCApproach();
GC.WaitForFullGCComplete();
GC.Collect();

Regards,
Hristo
Progress Telerik

Remote troubleshooting is now easier with Telerik Fiddler Jam. Get the full context to end-users' issues in just three steps! Start your trial here - https://www.telerik.com/fiddler-jam.