关于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
每次弹出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
相关文章推荐
- mysql设置root密码
- long long 与 _int64使用总结
- Cordova Android 禁用长按选中功能
- c/c++中指针与二维数组的关系demo程序
- Android apk反编译
- 单引号,双引号,反引号的区别
- (转)TOMCAT JAVA_HOME or JRE_HOME environment variable is not defined correctly(2013-03-29 13:04:14)
- 关于数据库查询一个表中的数据插入到另一个表中
- linux 动态库和静态库的区别、制作、使用
- javascript内置属性——arguments
- 【RMQ+二分】poj 2637 WorstWeather Ever
- js弄出加载效果,加载完后跳转到指定的页面
- 对C++的一点抱怨
- android ndk Using Existing Libraries
- C++和java中类成员定义时候的区别
- Java之WeakReference与SoftReference使用讲解
- 大端模式与小端模式
- cloudera manager下的hive权限配置
- 调试magento程序的方法
- 实验一 DOS命令解释程序的编写