Solved

How do I debug an OutOfMemoryException?

Posted on 2009-04-12
5
311 Views
Last Modified: 2013-12-17
I have a c# program that visits web wanders around a website using IE (via shdocvw.dll).  It runs fine for a while but after running for a while (an hour or two) it throws an OutOfMemoryException.  This is a legitimate exception because my app is taking up a ton of memory.  My question is how do i find out where all my memory is being taken up (just looking at my code i dont see anything that should be taking up that much memory, the call stack is not deep, i don't see anything that would build up over time).

It seems to me that the Visual Studio developers would have thought of this situation and provided a tool that shows you what is going on with memory in your program (like how big each variable is) but I cannot find this tool.  If it is not part of Visual Studio (i am using version 2008) then is there a third party app that does this?  If it is in VS then how can i get to it?

Thanks!
0
Comment
Question by:dave4dl
5 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24126657
To find out how much memory you are using you can use

   int totalMem = GC.GetTotalMemory(False);

it will return all the managed memory of the current thread.

If you are "loosing" memory somehow, or if this figure is low compared to the amount of memory you consider yourself using, you can also use the Diagnostics methods.

A likely cause of memory loss is using objects that expose the IDispose interface. Not calling Dispose in the try/finalize block can leave memory leaks.

-- Abel --


0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 250 total points
ID: 24128328
u can use memroy profiler from  http://memprofiler.com/  i find it handy.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24131199
Are you using recursion or any COM objects? If you are using COM, perhaps you are creating new objects repeatedly but not releasing them.
0
 
LVL 39

Assisted Solution

by:abel
abel earned 250 total points
ID: 24136127
As you surely have found out by now, trying to catch an OutOfMemoryException is not possible (you can catch it, but not recover). However, there's a way the CLR provides to test a certain piece of code whether it would throw an OutOfMemoryException if executed normally: consider it a way of adding an extra boxing layer.

The key to this method is using the MemoryFailPoint class. You give it the expected amount of memory you think you need (you can calculate that using a beforeAmount/afterAmount calculation on a regular call using GC.TotalMemory, see Microsoft example further on). Then you process your code. If you end up using too much memory, you will receive a catchable InsufficientMemoryException and you can still act and close down your application gracefully.

If you use MemoryFailPoint remember to deal with it as with any class implementing the IDispose interface: either with a try/finally block or a using-block.

The background story from the BCL team: http://blogs.msdn.com/bclteam/archive/2006/10/17/clr-behavior-on-outofmemoryexceptions-brian-grunkemeyer.aspx

A rather complex example from Microsoft using MemoryFailPoint, showing quite some of the concepts involved (only C#): http://msdn.microsoft.com/en-us/library/system.runtime.memoryfailpoint.aspx

An easy example including the using-block: http://mitchfincher.blogspot.com/2008/11/c-outofmemoryexception-and.html

-- Abel --
0
 
LVL 15

Author Closing Comment

by:dave4dl
ID: 31569356
Thanks guys
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question