Summary of common scenarios and processing methods for. Net Memory Overflow (system.outofmemoryexception)

Source: Internet
Author: User

Summary of common scenarios and processing methods for. Net Memory Overflow (system.outofmemoryexception)

Under what circumstances will the outofmemonryexception appear? We can catch the exception when we try to create a new object, and the garbage collector cannot find any available memory. Another scenario is when the CLR needs memory, but the system does not provide it, and it throws the exception. But at this point, our application is not able to catch the error.

Debug Analysis of Memory overflow (outofmemoryexception)

The 32-bit operating system's addressing space is 4G, of which 2G is occupied by the operating system, that is left to the user process memory only 2G (which also deduct the program load when the image occupies a portion of space, generally only about 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.). Although the final performance is outofmemoryexception, but the cause may be different, to solve this problem before you need to analyze the current state of memory usage of the process, to find the correct reason, to the right remedy. Let's share some tips on debugging this type of problem.

For more information, please refer to: http://blog.csdn.net/lazyleland/article/details/6704661

IIS application pool memory overflow error system.outofmemoryexception

On the ASP. ASP. NET can use the memory, usually not equal to all the amount of memory. In the Machine.config configuration file, configuration section <processModel> has a property "MemoryLimit", the value of this property is a percent, the default is "60", that is, the ASP is specified. NET process (in Task Manager you can see that the process of ASP. IIS5 in ASPNET_WP,IIS6 for 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. For example, we have prepared a chemas-microsoft-com ffice marttags "/>t=" on ">4g memory server, then t=" on ">4gx60%=t=" on ">2.4g. However, for the WIN32 operating system, 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. That is, 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.

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. Developers need to be aware of the code to keep in mind not to use and waste memory unnecessarily. :)

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, and then join the link after the article
    2. Using Windows Server 2003 64bits Edition
Some elements to avoid memory overflow

If you want to create an array, make sure it is the correct size.

Make sure that you have enough memory for internal purposes and for new managed objects.

If you are programming on the. NET Compact Framework, the common language runtime throws this exception when there is not enough memory available for internal use or for new managed objects. To avoid this exception, you should avoid writing large methods that consume 64KB or more memory.

Excessive amount of managed memory usage is usually caused by the following factors:

    1. Reads a large data set 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 with an undefined size), and an additional message "Insufficient storage space to complete this operation" is included. 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
PublicPartialClass_default:System.Web.Uipage{protectedvoid Page_load (object Sender eventargs E) {byte< Span class= "pun" >[] bytes =file.readallbytes ( "D:\toClient.xls"  Toclient.xls size is 20mresponse. Binarywritebytes}                /span>                

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 following is the correct wording:

Response.ContentType="Application/octet-stream";Response.AddHeader( "content-disposition" , "attachment; Filename= "+httputility. Urlencode (downloadname,system text. Encoding. Utf8response. Writefile "D:\toClient.xls"  Response. Flush. 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);

The exception that is thrown when an open file is not an image file:

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.

Transferred from: http://outofmemory.cn/c/dotNet-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.