0xDEADBEEF
0xDEADBEEF

Reputation: 3431

Tracking .NET-Objects in Memory

I am writing an editor in C#/.NET using AvalonDock.

If i close a document, the memory-consumption of my program doesn't decrease. Even if I call the garbage collector manually. So i assume that there is still a reference of the document somewhere.

The software is huge and the document is a very central component, so it's not easy to find every reference to it.

Does the Visual Studio 2010 debugger have a functionality to search for objects of a certain class in memory or something?

Alternatively, what would you do, if faced with such a problem?

Upvotes: 2

Views: 3468

Answers (3)

dgm
dgm

Reputation: 121

You can do what you want using free tools.

The basic steps are as follows:

  1. Run Your Application
  2. Attach windbg to its process
  3. Load the "sos" helper module (.loadby sos mscorwks)
  4. Dump the heap (!DumpHeap -stat)
  5. Find the type you're interested in and see if it's actually the thing using memory
  6. Dump the heap for your particular type (!DumpHeap -type MyNameSpace.MyType)
  7. Find the memory address of an object you think should be disposed, and see if it is "rooted" somewhere. (!gcroot "whatever the address was")

I've personally used this technique to great effect when tracking down memory leaks in graphics-intensive c# programs.

I learned this from Rico Mariani of Microsoft. Here is a blog entry that describes it in detail. * http://blogs.msdn.com/b/ricom/archive/2004/12/10/279612.aspx

Upvotes: 2

Rocklan
Rocklan

Reputation: 8130

Remember that even when .net cleans itself up, windows may not decide to actually release the memory. Often it only does so when another application actually needs memory. So, use a memory profiler :)

Upvotes: 0

Oded
Oded

Reputation: 499352

You need to use a memory profiler to find out what objects are in memory and what holds a reference to them.

There are several different options - commercial and free.

Upvotes: 7

Related Questions