System.OutOfMemoryException with ASP.NET on a high-ram machine

If you have a machine with 4gb of ram, you may still see out of memory errors coming from ASP.NET. It seems impossible, since you have so much memory, yet it can happen for a simple reason…

As a safeguard against memory leaks, an ASP.NET process will consume virtual memory up to a defined limit before automatically recycling the process and releasing the memory. This limit is controlled by the memoryLimit attribute in machine.config, which indicates a percentage of physical ram. By default, memoryLimit is set to 60.

So for example, if your server has 1gb of physical ram installed, and memoryLimit is set to 60, then ASP.NET processes will recycle if they utilize over 600mb of memory (60% of 1gb).

The problem occurs if you have a server with a lot of ram, e.g. 4gb. 60% of 4gb is 2.4gb, which means that ASP.NET won’t recycle until it uses over 2.4gb of ram. If it’s using less, it doesn’t think there’s a memory problem. However, since by default a process can only address 2gb of ram, you will run into memory allocation errors as you approach 2gb.

The errors will actually occur before 2gb is reached, because as your application allocates, releases, and consumes memory, the available pool becomes more fragmented. That means that fewer contiguous blocks of memory are available, and the blocks that are available are smaller. In a simplified example, let’s say there are 100 blocks of memory left, and each block is 1mb. It will look like there’s 100mb of memory free. However, if your application requests a 2mb block, it will get an out of memory error, because there aren’t any available blocks that are 2mb (or larger).

So, back to the server with 4 gigs of ram. ASP.NET will try to go up to 2.4gb, but 2gb is an address barrier. So there are a few solutions:

  • enable the /3gb switch in boot.ini. That will allow a process to address up to 3gb of ram instead of 2gb
  • change machine.config’s memoryLimit parameter to a lower value like 40. That would cause ASP.NET to recycle at 1.6gb (40% of 4gb).
  • configure your app pool in IIS to recycle if it hits 1.6gb of virtual memory.
  • As an aside, you should also try to understand why your application is using so much memory in the first place. 2gb is a lot of data, so you may want to see if you have a memory leak.

    MSDN has a great article on tuning and performance monitoring that you should check out if you want to learn more.

    0