您的位置:首页 > 其它

关于Winform中rdlc报表内存溢出问题解决方案

2015-10-10 15:51 134 查看
最近在做一个winform的项目,由于是优化他人代码,根据用户反馈的问题:

每次弹出rdlc报表时,内存会不断的up,up。

使用了多种释放的方式,效果不尽人意,比如像GC.collect,并且加了定时器,每隔几秒钟回收一次内存,仍然不得解。通过查阅相关文献,发现有不少人也有类似问题。

这是微软vs的bug,据说在vs2010中已经解决,但实际问题仍在。

以下是我的环境以及针对个人情况的解决方案:

VS2010 + .net framework 4.0,使用datatable填充数据集,rdlc报表

软件运行过程中出现的问题:OutOfMemory(内存溢出)

解决方案:LocalReport.ReleaseSandboxAppDomain();

将AppDomain域释放。

贴上我的代码:

        private void InitLocalReport(LocalReport localReport, DataTable dt, String rdlcTemplate)

        {

            try

            {

                ReportDataSource reportDataSource1 = new ReportDataSource("DataSet1", dt);

                localReport.DataSources.Clear();

                localReport.DataSources.Add(reportDataSource1);

                // localReport.ReportPath = "d:\\ZrytFRS.UserRPTDetail.rdlc";

                localReport.ReportEmbeddedResource = rdlcTemplate;  // 报表模板

                String strTitle = null;

                if (this.m_strReportTitle == null)

                {

                    strTitle = ZrytFRS.Properties.Settings.Default.ReportTitle;

                }

                String strRPTMaker = null;

                if (this.m_strReportMaker == null)

                {

                    strRPTMaker = ZrytFRS.Properties.Settings.Default.LoginUserAsReportMaker

                                 ? Program.UserName

                                 : ZrytFRS.Properties.Settings.Default.ReportMaker;

                }

                bool bEnableRPTMaker = this.m_bEnableReportMaker;

                if (!bEnableRPTMaker)

                {

                    bEnableRPTMaker = ZrytFRS.Properties.Settings.Default.EnableReportMaker;

                }

                List<ReportParameter> parameterList = new List<ReportParameter>();

                parameterList.Add(new ReportParameter("RPT_Title", strTitle));    // 制表人

                parameterList.Add(new ReportParameter("RPT_Maker", strRPTMaker));

                parameterList.Add(new ReportParameter("RPT_ShowReportMaker", bEnableRPTMaker ? "true" : "false"));

                // parameterList.Add(new ReportParameter("RPT_GenerateTime", Now.));

                localReport.SetParameters(parameterList);

                //localReport.DataSources.Clear();

                //localReport.Dispose();

            }

            catch (Exception ex)

            {

                m_strErrorMsg = ex.Message;

                System.Diagnostics.Debug.WriteLine(ex.Message);

                throw new Exception("Init Local Report\r\n" + ex.Message, ex);

            }

            finally

            {

                System.GC.Collect();

                localReport.ReleaseSandboxAppDomain();

            }

        }

在网上大家可以看看其他地解决方式
http://stackoverflow.com/questions/6220915/very-high-memory-usage-in-net-4-0 http://www.xcoder.cn/html/web/dotNet/2013/0516/6864.html https://connect.microsoft.com/VisualStudio/feedback/details/527451/ms-report-viewer-memory-leak-any-update-fix-winforms-application#tabs
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: