在我的应用程序(.NET Framework 4.5)中,我正在渲染一些RDLC报告(50-60个),以便将它们导出为单个PDF文件。
不幸的是,存在一个很大的内存泄漏问题,基本上每个
以下是我的代码:
我通过查看Visual Studio的内存面板发现了内存泄漏。每次调用
不幸的是,存在一个很大的内存泄漏问题,基本上每个
LocalReport
都没有被处理。以下是我的代码:
public void ProcessReport(ReportDataSource[] reportDS, string reportPath)
{
const string format = "PDF";
string deviceInfo = null;
string encoding = String.Empty;
string mimeType = String.Empty;
string extension = String.Empty;
Warning[] warnings = null;
string[] streamIDs = null;
Byte[] pdfArray = null;
using (var report = new LocalReport())
{
report.EnableExternalImages = true;
report.ReportEmbeddedResource = reportPath;
report.Refresh();
foreach (var rds in reportDS)
{
report.DataSources.Add(rds);
}
report.Refresh();
try
{
pdfArray = report.Render(format, deviceInfo, out mimeType, out encoding,
out extension, out streamIDs, out warnings);
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException.Message);
throw;
}
report.ReleaseSandboxAppDomain();
report.Dispose();
//Add pdfArray to MemoryStream and then to PDF - Doesn't leak
}
}
我通过查看Visual Studio的内存面板发现了内存泄漏。每次调用
report.Render
时,它会增加20-30MB的内存,直到我关闭应用程序前,这些内存都不会被释放。我确定使用MemoryStream
不是问题,因为即使注释掉该行,内存中仍然有200MB-250MB没有释放。这很糟糕,因为运行此应用程序3-4次后,内存就会达到>1GB,甚至无法再运行。我还尝试手动调用GarbageCollector
,但并没有起作用。该应用程序是32位的。
我该怎么办来解决这个问题?