[Favorite]What to do when your ASP.NET app throws System.OutOfMemoryException?

The obvious solution to applications throwing System.OutOfMemoryException would of course be to increase the memory. Or to change your code to use less. Still, it is often a bit of a puzzle why these exceptions are thrown anyway. And debugging them is very difficult, because you normally do not get them in your development environment and they don't sport a stack trace. Also, these exceptions seem sometimes to occur even in systems with abundant memory. A few tips:

Why no stack trace?

The System.OutOfMemeoryException is thrown when the system runs out of space for allocating new objects. So it wants to throw an exception, but the exception object itself must also be allocated: catch-22. The creators of the .NET framework got around this issue by pre-allocating an instance of OutOfMemoryException just-in-case. When the situation occurs, the CLR throws this already existing object. But of course, at the time of creation, the stack trace is not yet known.

Why so soon?

Even if you have many gigs of memory in your box, you can still experience an OutOfMemoryException. A 32bit pointer can address 4G, which is evenly split in 2G parts for kernel and user mode programs. So no matter how many RAM you have, the limit is 2GB. If your app is an ASP.NET app, the limit is even lower. I still do not really understand why, but the practice is that an ASP.NET application will start throwing OutOfMemoryExceptions when memory use get above 800MB. If you use a lot of caching, that is not insanely much!

The trick: /3GB

The real solution is 64-bit computing. But if you cannot make that move just now, there is an intermediate solution: the /3GB switch. This is a setting in the boot.ini file (yes, boot.ini). When you set it, your 4GB is not split in 2 equal parts, but in a 1GB part for the kernel (generally enough) and a 3GB part for user mode programs. This means that normal programs will now have 50% more memory available, but for ASP.NET apps the gain is even better. With the /3GB switch on, they can use approximately 1.800MB.

For more info on memory use: check this out.

Source: http://dotnetjunkies.com/WebLog/teund/archive/2005/04/01/aspnet_outofmemoryexception.aspx

posted @ 2005-04-21 23:37  dudu  阅读(2028)  评论(0)    收藏  举报