Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I debug an OutOfMemoryException?

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

661 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