This article is a mirror article of machine translation, please click here to jump to the original article.

View: 24448|Reply: 0

[Source] . Net memory overflow (System.OutOfMemoryException).

[Copy link]
Posted on 3/14/2019 9:47:39 PM | | | |
When does an OutOfMemonryException occur? If we try to create a new object and the garbage collector can't find any free memory, we can catch the exception; Another case is that when the CLR needs memory and the system cannot provide it, the exception will also be thrown. But at this point, our application cannot catch the error.

Debug analysis of memory overflow (OutOfMemoryException).

The addressing space of the 32-bit operating system is 4G, of which 2G is occupied by the operating system, which means that the memory left for the user process is only 2G (which also deducts part of the space occupied by the image when the program is loaded, generally only about 1.6G~1.8G can be used). If a process needs to request memory while it is running, and the operating system cannot allocate memory space to it, it will produce an out-of-memory exception, System.OutOfMemoryException in .net (The exception that is thrown when there is not enough memory to continue the execution of a program.). Although the final manifestation is OutOfMemoryException, the cause may be different, and before solving this problem, it is necessary to analyze the current memory usage status of the process to find the correct cause before prescribing the right medicine. Here are some tips for debugging such problems.

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

iis Application Pool Memory Overflow Error System.OutOfMemoryException

On a ASP.NET web server, the amount of memory that ASP.NET can use is usually not equal to all the amount of memory. In the machine.config configuration file, <processModel>there is a property "memoryLimit" in the configuration section, the value of this property is a percentage value, the default is "60", that is, the ASP.NET process (you can see the ASP.NET process in the task manager, aspnet_wp in IIS5, w3wp in IIS6) can use 60% of all physical memory. When the amount of memory used by ASP.NET exceeds this limit, IIS will start automatically recycling the process, that is, creating a new process to handle Http requests and reclaiming the memory occupied by the old process.

When we have a server with a large memory, the value of "memoryLimit" needs to be adjusted appropriately. For example, if we prepare a server with chemas-microsoft-com ffice marttags" />t="on"> 4G memory, then t="on">4G×60%=t="on">2.4G. However, for Win32 operating systems, all the memory space that a process can occupy is only t="on">2G. When the memory occupied by the ASP.NET process begins to reach t="on">2G, because it does not reach the "recycling threshold" of t="on">2.4G, IIS will not start the recycle process operation, but due to the limitations of Win32, it is actually impossible to allocate more memory to this process, so the OutOfMemoryException is likely to be thrown. To avoid this, we had to reduce the "memoryLimit" appropriately so that IIS could process recycle earlier.

Microsoft recommends that ASP.NET process take up no more than 60% of the memory, and it is best to make the calculated actual value no more than t="on">800M. That said, for a server with t="on" > 4G memory, it is best to set the "memoryLimit" property to "20". Setting an appropriate recycling threshold for IIS to recycle processes in a timely manner is very important to ensure the stable operation of the entire server and avoid OutOfMemoryException.

In IIS6, the recycling threshold for ASP.NET processes is no longer determined by the "memoryLimit" property in the configuration section, but by the settings in the application pool configuration in IIS Manager.

However, even if these configurations are set correctly, there is no guarantee that OutOfMemoryExceptions will be completely avoided, and the reasons can be varied and complex, such as memory reclamation operations may be too time-consuming. Developers should always keep in mind not to use and waste memory unnecessarily in their code. :)

If you have a server with a large memory and are frustrated by the limit of using t="on" >2G memory in the Win32 operating system, there are two alternative solutions:

  • Start the computer in /3GB mode, and follow the link after the method participation article
  • Use Windows Server 2003 64bits Edition


Several elements to avoid memory overflow

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

Make sure you have enough memory for internal use and new hosted objects.

If you're programming on the .NET Compact Framework, the public language runtime throws this exception when there isn't enough memory for internal use or a new managed object. To avoid this exception, you should avoid writing large methods that take up 64KB or more of memory.

Excessive managed memory usage is often caused by:

  • Read large datasets into memory.
  • Creating too many cached entries.
  • Upload or download large files.
  • Use too many regular expressions or strings when analyzing files.
  • Excessive view status.
  • There is too much data or too many sessions in the session state.
  • This exception can be raised when a method is called on a COM object and the method returns a user-defined type that contains a secure array (an array of indefinite sizes) with an additional message "Not enough storage space to complete this operation". This is because the .NET Framework cannot marshal structural fields with secure array types.


An example of a memory overrun caused by improper use of byte arrays


If the output file is particularly large, it may directly report System.OutOfMemoryException. The correct way to do this is to output the byte stream of the file in segments, but there is asp.net ready-made method Response.WriteFile(filePath) that does just that.

The following is the correct way to write:

When a asp.net experiences a memory overflow, an easy way to deal with it is to reclaim the application pool right away. But this did not completely solve the problem.

Memory overflow when creating an Image type (System.OutOfMemoryException)

Error code: System.Drawing.Image myimg=System.Drawing.Image.FromFile(file. FullName);

Exceptions that are 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.

Such abnormal information is easily misleading.

<processModel> element

Configure the ASP.NET process model settings on the Internet Information Services (IIS) web server. The section can only be set in the Machine.config file <processModel> , and it affects all ASP.NET applications running on the server.

Warning For information about this element, please read the "Notes" section.

Example of configuring the structure:

exegesis

The managed code configuration system does not read <processModel> the configuration settings. Instead, it is read directly by the unmanaged DLL aspnet_isapi.dll. The changes in this section take effect after you restart IIS.

If you install ASP.NET on a domain controller, you must take special steps or the installation will not work. For more information, see Located inhttp://support.microsoft.comThe Microsoft article in the Knowledge Base CHS315158 "ASP.NET can't use the default ASPNET account on domain controllers".

When ASP.NET is running in IIS version 6 native mode, it uses the IIS 6 process model and ignores <processModel> the settings in the section. To configure process identity, recycling, or other process model values, use the Internet Services Manager user interface to configure IIS worker processes for your application.

The time value is formatted as "hours:minutes:seconds". If only a single number is given without a colon, the value is assumed to be minutes; Therefore, timeout="4" is equivalent to timeout="00:04:00".

If a ASP.NET application causes ASP.NET worker processes (Aspnet_wp.exe on Windows 2000 and Windows XP Professional and W3wp.exe on Windows Server 2003) to restart and gives an error message indicating that the restart is due to a suspected deadlock condition, it should increase responseDeadlockInterval setting.

Store usernames and passwords in the registry

Store usernames and passwords in the registry

To encrypt usernames and passwords and store them in the registry, set the userName and password as follows.

userName="registry:HKLM\Software\AspNetProcess,Name"

password="registry:HKLM\Software\AspNetProcess,Pwd"

The part of the string that comes after the keyword registry and before the comma indicates the name of the registry key that ASP.NET opens. The part after the comma contains a string value name from which the ASP.NET will read the credentials. Commas are required and credentials must be stored in the HKLM configuration unit. If the configuration is misformatted, ASP.NET will not start the worker process and will subsequently appear in the path of the current account creation failure code.

The credentials must be in REG_BINARY format and contain the output of a call to the Windows API function CryptProtectData. You can create and store encryption credentials in the registry with the ASP.NET Settings Registry Console application (Aspnet_setreg.exe), which uses CryptProtectData to complete the encryption. To download Aspnet_setreg.exe and Visual C++ source code and help, visit the websitewww.asp.netand search for "aspnet_setreg".

You should configure access to the registry keys that store encrypted credentials so that access is only available to Administrators and SYSTEM. Because the registry key will be read by the ASP.NET process running as SYSTEM, you should set the following permissions:

Administrators:F

SYSTEM:F

CREATOR OWNER:F

ProcessAccount:R

This will provide two lines of defense to protect data:

ACL permissions require access to data with the identity of Administrator.
The attacker would have to run code (CryptUnprotectData) on the server in order to recover the credentials of the account.

example

The following example specifies several <processModel> configuration settings.

The following example specifies that the encrypted username and password are stored under the registry user-defined item AspNetProcess.

requirements

  • Included in: <system.web>
  • Web platform: IIS 5.0, IIS 5.1, IIS 6.0
  • Configuration files: Machine.config, Web.config
  • Configuration section handler: System.Web.Configuration.ProcessModelConfigurationHandler


http://doc.51windows.net/iismmc/ ... essmodelelement.htm





Previous:C# determines whether the uploaded file is an image and prevents Trojan horse uploads
Next:C language e-book collection sharing
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com