none
rdlc report winforms 2015 memory leak

    Question

  • I'm currently working on windows application with a .rdlc report within my .NET 4.5 Framework.

    Everything works well but for every PDF the memory increases about 0.5-1 MB :( I've created a loop where is the whole logic about recording PDF and I'm doing some of next things.

    foreach (var bill in billsForPDF)
    {
        //...
        this.newBillsTableAdapter.Fill(this.XXXDataSet.newBills, bill.BillNumber)
        //...
        contractBindingSource.DataSource = bill.contract;
        //...
        rptViewerBill.LocalReport.SetParameters(Adresa);
        //....
        rptViewerBill.RefreshReport();
        //...
        var pages = new List<byte[]>();
        var xxx = rptViewerBill.LocalReport.Render("PDF");
        pages.Add(xxx);
    
        PdfDocument docX;
        PdfDocument fulldocX = new PdfDocument();
    
        foreach (var item in pages)
        {
            var PDFstream = new MemoryStream(item);
            docX = PdfReader.Open(PDFstream, PdfDocumentOpenMode.Import);
            CopyPages(docX, fuldocX);
        }
        fulldocX.Save(@"C:..."+bill.billNumber.ToString()+".pdf");
        /*here i'm tried almost everything with GC and disposing LocalReport, clear, null...but nothing works */    
    }

    In Bill.rdlc I have an expr like =

    "Bill number: " & First(Fields!billNumber.Value, "DataSet1")"

    Can anyone help?

    • Moved by Hart Wang Friday, May 26, 2017 1:36 AM
    Thursday, May 25, 2017 10:11 AM

All replies

  • Hi amirpi,

    Thank you for posting here.

    Since your issue is related to winform forum. I will move the case to there.

    Best  Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 26, 2017 1:34 AM
  •  

    I'm currently working on windows application with a .rdlc report within my .NET 4.5 Framework.

    Everything works well but for every PDF the memory increases about 0.5-1 MB :( I've created a loop where is the whole logic about recording PDF and I'm doing some of next things.

    foreach (var bill in billsForPDF)
    {
        //...
        this.newBillsTableAdapter.Fill(this.XXXDataSet.newBills, bill.BillNumber)
        //...
        rptViewerBill.LocalReport.SetParameters(Address);
        //....
        rptViewerBill.RefreshReport();
        //...
        var pages = new List<byte[]>();
        var xxx = rptViewerBill.LocalReport.Render("PDF");
        pages.Add(xxx);
    
        PdfDocument docX;
        PdfDocument fulldocX = new PdfDocument();
    
        foreach (var item in pages)
        {
            var PDFstream = new MemoryStream(item);
            docX = PdfReader.Open(PDFstream, PdfDocumentOpenMode.Import);
            CopyPages(docX, fuldocX);
        }
        fulldocX.Save(@"C:..."+bill.billNumber.ToString()+".pdf");
        /*here i'm tried almost everything with GC and disposing LocalReport, clear, null...but nothing works */    
    }

    In Bill.rdlc I have an expr like =

    "Bill number: " & First(Fields!billNumber.Value, "DataSet1")"

    Can anyone help?

    P.S. Same code executed within 3.5 .NET Framework works perfect, memory does not growing.


    Tuesday, May 30, 2017 8:27 AM
  • Hi,

    Look at these:

    MS Report Viewer Memory Leak - Any Update / Fix? (WinForms Application)

    https://connect.microsoft.com/VisualStudio/feedback/details/527451/ms-report-viewer-memory-leak-any-update-fix-winforms-application#tabs

    Report Viewer 2010 SP1 Memory Leak

    https://connect.microsoft.com/VisualStudio/feedback/details/666364/report-viewer-2010-sp1-memory-leakHow to fix memory leak in Microsoft Report (rdlc)

    https://www.codeproject.com/Questions/636950/How-to-fix-memory-leak-in-Microsoft-Report-rdlc

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 30, 2017 8:32 AM
    Moderator
  • Hey all!  I'm pretty late to this, but I have a real solution and can explain why!

    It turns out that LocalReport here is using .NET Remoting to to dynamically create a sub appdomain and run the report in order to avoid a leak internally somewhere.  We then notice that, eventually, the report will release all the memory after 10 to 20 minutes.  For people with a lot of PDFs being generated, this isn't going to work.  However, the key here is that they are using .NET Remoting.  One of the key parts to Remoting is something called "Leasing".  Leasing means that it will keep that Marshal Object around for a while since Remoting is usually expensive to setup and its probably going to be used more than once.  LocalReport RDLC is abusing this.

    By default, the leasing time is... 10 minutes!  Also, if something makes various calls into it, it adds another 2 minutes to the wait time! Thus, it can randomly be between 10 and 20 minutes depending how the calls line up.  Luckily, you can change how long this timeout happens.  Unluckily, you can only set this once per app domain... Thus, if you need remoting other than PDF generation, you will probably need to make another service running it so you can change the defaults.  To do this, all you need to do is run these 4 lines of code at startup:

            LifetimeServices.LeaseTime = TimeSpan.FromSeconds(5);
            LifetimeServices.LeaseManagerPollTime = TimeSpan.FromSeconds(5);
            LifetimeServices.RenewOnCallTime = TimeSpan.FromSeconds(1);
            LifetimeServices.SponsorshipTimeout = TimeSpan.FromSeconds(5);

    You'll see the memory use start to rise and then within a few seconds you should see the memory start coming back down.  Took me days with a memory profiler to really track this down and realize what was happening.

    Hope this helps!
    • Proposed as answer by Daniel Lorenz Thursday, April 18, 2019 9:02 PM
    Thursday, April 18, 2019 9:02 PM