首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LocalReport呈现导致OutOfMemory异常

LocalReport呈现导致OutOfMemory异常
EN

Stack Overflow用户
提问于 2015-10-27 18:54:53
回答 1查看 2.4K关注 0票数 1

我有一个WindowsService,它通过LocalReports (RDLC报告)将XML文件转换为PDF。它工作得很好,而且速度很快,但我正在努力解决类似于那些here的内存泄漏问题。

看来,.Render("PDF")方法是问题出现的地方。VS2015中的分析会话显示了ServerIdentity使用的大量内存。如果我注释掉.Render("PDF"),就不会出现这种情况。

规格

  • 应用程序是.NET 4.5
  • 应用程序使用来自VS2012的VS2012库(我也尝试过2015年,以及相同的行为)
  • 为“任意CPU”构建的,可能以x86的形式运行
  • 报告不是很大,但是我们运行了很多次,所以几周后内存就用完了(一页报告,一天可能有十几个报告)。

代码

代码语言:javascript
复制
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);
        }
    }

其他注释

  • 如果我从不调用.Render("PDF"),内存就不会超过20 so左右。
  • 类似地,如果我从不呈现,堆中就没有ServerIdentityServerIdentity对象。这就是消耗内存的90%+的地方,但是我没有办法去GC那个内存。
EN

回答 1

Stack Overflow用户

发布于 2020-11-26 16:42:07

将应用程序的编译目标更改为x64并调用

代码语言:javascript
复制
report.ReleaseSandboxAppDomain();

后渲染法请记住释放报表对象。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33376285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档