I have a WindowsService that converts XML files to a PDF via LocalReports (RDLC reports). It works very well and is fast, but I'm struggling with memory leak problems similar to those here.
It appears the .Render("PDF")
method is where the problems are appearing. A profiling session in VS2015 shows a huge about of memory used by ServerIdentity
. This doesn't appear if I comment out .Render("PDF")
.
Specs
- Application is .NET 4.5
- Application uses ReportViewer library from VS2012 (I've tried 2015, too, and same behavior)
- Built for 'Any CPU', runs as x86 presumably
- Reports aren't huge, but we run quite a few so after a few weeks memory gets used up (1 page reports, maybe a dozen or more a day).
Code
public static byte[] GenerateReport(string name, IEnumerable<ReportDataSource> dataSources)
{
try
{
byte[] pdfBytes;
using (Stream reportStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
{
LocalReport localReport = new LocalReport();
localReport.LoadReportDefinition(reportStream);
foreach (var dataSource in dataSources)
{
localReport.DataSources.Add(dataSource);
}
pdfBytes = localReport.Render("PDF");
//Cleanup!
localReport.DataSources.Clear();
localReport.ReleaseSandboxAppDomain();
localReport.Dispose();
}
return pdfBytes;
}
catch (Exception ex)
{
throw new Exception("Error generating report at " + name, ex);
}
}
Other notes
- If I never call
.Render("PDF")
, the memory never grows beyond 20MB or so. - Similarly, if i never render, there's no
ServerIdentity
or Microsoft.ReportingServices.OnDemanProcessing.*` objects in the heap. This is where 90%+ of the memory is consumed but I have no way to GC that memory.