Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I debug an OutOfMemoryException?

Posted on 2009-04-12
5
Medium Priority
?
334 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 1000 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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

885 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