Asp. NET Memory overflow error system.outofmemoryexception

Source: Internet
Author: User
Tags try catch

Cause 1: Database server performance issues cause memory overflow cause 2: Configuration in IIS application pool, causing memory allocation problems in IIS server, causing memory overflow Analysis:The 32-bit operating system's addressing space is 4G, of which 2G is occupied by the operating system, leaving the memory of the user process only 2G (deduction of the program load when the image occupies a portion of space, generally only around 1.6g~1.8g can be used). If the process is running in which memory needs to be requested, and the operating system cannot allocate memory space for it, an out-of-memory exception is generated, in. NET System.OutOfMemoryException (the exception is thrown when there     Is isn't enough memory tocontinue the execution of a program.). ASP on the ASP. NET Web server. NET can use the memory, usually not equal to all the amount of memory. In the Machine.config configuration file,The distribution section <processModel> has a property "MemoryLimit", the value of this property is a percent, the default is "60", That is, you specify an ASP. (in Task Manager, you can see that the process of ASP. IIS5 in ASPNET_WP,IIS6 is w3wp) can use 60% of all physical memory. When the amount of memory that ASP. NET uses exceeds this limit, IIS starts the Auto-recycle (recycle) process, which is to create a new process to handle the HTTP request and reclaim the memory consumed by the old process.
when we have a very large memory server, the value of "memorylimit" needs to be adjusted appropriately. e.g: a chemas-microsoft-com ffice marttags "/>t=" on ">4g memory server, thent= "on" >4gx60%=t=" on ">2.4g".   however, for WIN32 operating systems, all memory space that a process can occupy is only t= "on" >2g. When the amount of memory that the ASP. NET process consumes starts to reach t= "on" >2g, IIS does not start the recycle process operation because it does not meet the "recycle threshold" of t= "on" >2.4g, but because of the Win32 limit, In fact, it is not possible to allocate more memory to this process, so outofmemoryexception is likely to be thrown out. In order to avoid this, we have to make the "memorylimit" appropriately smaller so that IIS can recycle the process earlier.
Microsoft recommends that the ASP. NET process occupy memory is not more than 60%, and it is best to make the calculated actual value not more than t= "on" >800M. for a server t= "on" >4g memory, it is best to set the "memorylimit" property to "20". Setting an appropriate recycling threshold to allow IIS to process recycling in a timely manner is important to ensure stable operation of the entire server and avoid outofmemoryexception.
in IIS6, ASP. NET process is no longer determined by the "memorylimit" property in the configuration section, but is determined by the settings in the application pool configuration in IIS Manager. IIS5 must use the Iisrecyle tool.
However, even if these configurations are set up correctly, there is no guarantee that outofmemoryexception will be completely avoided, for reasons such as multiple and complex, such as a memory recovery operation may take too much time, and so on.
  Workaround:If you have a large memory server, and the Win32 operating system for the highest process use t= "on" >2g memory limit is depressed, there are two options to solve:
1. Use/3GB mode to start the computer
2. Using Windows Server 2003 64bits Edition
3, IIS automatically recycle resources, scheduling during the minimum system load period for recycling (such as 5 o'clock in the morning) 4, try catch to avoid memory overflow a few factors too much managed memory usage is usually caused by the following factors: 1. Read large datasets into memory. 2. Create too many cache entries. 3.upload or download large files. 4. Excessive use of regular expressions or strings when parsing files. 5, excessive view state. 6. There is too much data or too many sessions in the session state. 7. This exception may be thrown when a method is called on a COM object and the method returns a user-defined type that contains a safe array (an array of an undefined size), with an additional message "Insufficient storage space to complete this operation."  This is because the. NET Framework cannot marshal a structure field with a secure array type. An example of improper use of byte arrays to cause memory overflow <wiz_code_mirror>
System. Web. UI. Page  
{
  Page_Load (e) 
  {
   File.  ReadAllBytes ("D:\toClient.xls");  Toclient.xls size is 20M   
Response. binarywrite (bytes);  
  }
}
The above program if the output of the file is particularly large, it may be reported directly: System.OutOfMemoryException. The correct way is to put the file byte stream output, in fact, ASP. NET has a ready- made Method Response.WriteFile (FilePath) is doing so.  the correct wording is:<wiz_code_mirror>
Response.   "Application/octet-stream";
Response. AddHeader (httputility.  UrlEncode (System.  Text. Encoding. UTF8));      
Response. WriteFile ("D:\toClient.xls"); 
Response. Flush ();
Response. End ();
when ASP. NET memory overflows, a simple way to do this is to recycle the application pool immediately. But this does not solve the problem completely.
 Memory overflow when creating image type (System.OutOfMemoryException)Error Code: System.Drawing.Image myimg=System.Drawing.Image.FromFile(file.FullName);
当打开的文件不是图像文件时会引发的异常:MSDN: This method throws an OutOfMemoryException exception if the file does not have a valid image format, or if GDI + does not support the file's pixel format. This kind of unusual information is easy to misunderstand.

Asp. NET Memory overflow error system.outofmemoryexception

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.