Solved

How do I debug an OutOfMemoryException?

Posted on 2009-04-12
5
300 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 74

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
comparing string in C# 3 34
Set form below another form 3 26
C# DataGridView_RowsAdded event not firing 1 40
Create XML 5 34
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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.
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now