简体   繁体   中英

System.OutOfMemoryException on Windows Server 2008 R2 + ASP.NET 3.5

Today, a production server at my work started to get this OutOfMemoryException, but as far as I know, there has not been any code updates to the website.

I pasted the stack trace below, but any ideas on what might be happening? Basically, this header control, downloads a portions of a website from a remote server and includes it as part of the HTML.

Server Error in '/' Application. Exception of type 'System.OutOfMemoryException' was thrown. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.

Source Error:

Line 58:         else
Line 59:         {
Line 60:             thisHeader = Cache[headerCacheKey].ToString() + "\n<!-- pulled from cache -->\n";
Line 61:         }
Line 62:         if (!String.IsNullOrEmpty(thisHeader))


Source File: ...

Stack Trace:

[OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.]
   System.String.Concat(String str0, String str1) +66
   ASP.controls_header_ascx.Page_Load(Object sender, EventArgs e) in ...:60
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +42
   System.Web.UI.Control.OnLoad(EventArgs e) +132
   System.Web.UI.Control.LoadRecursive() +66
   System.Web.UI.Control.LoadRecursive() +191
   System.Web.UI.Control.LoadRecursive() +191
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2428

The line that the runtime is pointing you to ( Line 60 I believe) is not necessarily a cause of System.OutOfMemoryException . There might be some code that feeds the web cache with lots of information and doesn't release that information when it's no longer "valid" or "actual".

Anyway I suggest you to read these two articles first:

  1. Troubleshooting System.OutOfMemoryException
  2. Tracking down managed memory leaks (windbg)

The second article helped me ALOT when I faced the same problem one day. Actually now I prefer windbg over other (often proprietary) software to find out where is a memory leak in the software.

You could also try using one of these very helpful tools to find out what causes memory leaks:

  1. dotTrace Memory profiler
  2. ANTS Memory profiler

In general I think it's a good practice to examine the software execution process using a memory profiler before deploying the software on the production environment.

Are you are looping in Page_Load and within that loop concatenating a string? String concatenation is VERY slow and with each concatenation a new string object is allocated and put on the heap. System.Text.StringBuilder is a much more efficient way of doing this sort of operation. Note: if only concatenating a few short strings it is not an issue, but if you believe it'll go beyond that, then StringBuilder is your friend.

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM