Solved

How do I debug an OutOfMemoryException?

Posted on 2009-04-12
5
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

617 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