Getting System.OutOfMemoryException when using ReportViewer contol in Local mode
PROBLEM:
========
- Consider you've a ASP.NET application that contains Report Viewer control (2005 / 2008) in Local Mode.
- You have an RDLC report file, that loads large amount of data / has lots of expressions. (Both are not recommended in Local mode)
- Everytime you refresh the web page, the Report Viewer stores objects in the session.
- The behaviour of Report Viewer storing objects in the session is by design.
- Each time the report viewer page is refreshed the complete report info object is added to session.
- These objects obviously gets deeply rooted in session and so Garbage collector never collects them untill the complete app unloads itself.
- And that is apparently going to increase the memory pressure in multiple folds, ending up with System.OutOfMemoryException.
WORKAROUND: (Please note: This doesn't guarantee to resolve the exception. The Out of Memory exception can be caused due to different reasons and the below workaround is for one such scenario, which can help to avoid this error to a certain extent. You should avoid using it at any given point of time and try to switch to the ExecuteReportInSandboxAppDomain mode)
===========
== In the page_load event, add this,
== VB.NET
If Session.Count > 0 Then
For i As Integer = 0 To Session.Count - 1
If Session(i).GetType().ToString() = "Microsoft.Reporting.WebForms.ReportHierarchy" Then
Session.RemoveAt(i)
End If
Next
End If
== C#,
if(Session.Count > 0)
{
for (int i = 0; i < Session.Count; i++)
{
if (Session[i].GetType().ToString() == "Microsoft.Reporting.WebForms.ReportHierarchy")
{
Session.RemoveAt(i);
}
}
}
Comments
Anonymous
April 07, 2009
it made absolutely no difference. It dies even on a single report if your recordset is substantially large. The problems are much deeper-rooted than the session.Anonymous
May 07, 2009
The comment has been removedAnonymous
August 03, 2009
It works for me. Thanks for sharing out this useful info. :-)Anonymous
May 14, 2010
The following works: If Session.Count > 0 Then For i As Integer = 0 To Session.Count - 1 If i < Session.Count AndAlso Not Session(i) Is Nothing AndAlso Session(i).GetType().ToString() = "Microsoft.Reporting.WebForms.ReportHierarchy" Then Session.RemoveAt(i) End If Next End IfAnonymous
June 28, 2010
The comment has been removedAnonymous
August 19, 2010
One option to try is to move the session state out of the process by using AppFabric Cache. msdn.microsoft.com/.../ee695849.aspx The link here gives you details for setting up the cache. www.hanselman.com/.../InstallingConfiguringAndUsingWindowsServerAppFabricAndTheVelocityMemoryCacheIn10Minutes.aspx Even if the cached session state is not being exprired for some reason, you have abilities in AppFabric to script explict deleting of old session state.