Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find references to the object in runtime

Tags:

I have an object, which lives forever. I am deleteing all references I can see, to it after using it, but it still not collected. Its life cycle is pretty sophisticated so I can't be sure that all references been cleared.

if ( container.Controls.Count > 0 ) {      var controls = new Control[ container.Controls.Count ];     container.Controls.CopyTo( controls, 0 );      foreach ( var control in controls )      {           container.Controls.Remove( control );          control.Dispose();     }      controls = null;  }  GC.Collect(); GC.Collect(1); GC.Collect(2); GC.Collect(3); 

How can I find out what references does it still have? Why is it not collected?

like image 504
er-v Avatar asked Sep 24 '09 14:09

er-v


People also ask

How do I view references in Visual Studio?

In C# or Visual Basic, the Find References window has a Kind column where it lists what type of reference it found. This column can be used to filter by reference type by clicking on the filter icon that appears when hovering over the column header.

How do you reference an object?

The concept of object references becomes clear when assigning the same object to more than one property. Rather than holding a copy of the object, each assigned property holds object references that link to the same object, so that when the object changes all properties referring to the object reflect the change.

What does it mean to reference an object?

An object reference is information on how to find a particular object. The object is a chunk of main memory; a reference to the object is a way to get to that chunk of memory. The variable str does not actually contain the object, but contains information about where the object is.


2 Answers

Try using a memory profiler, (e.g. ants) it will tell you what is keeping the object alive. Trying to 2nd guess this type of problem is very hard.

Red-gate gives 14 days trial that should be more then enough time to tack down this problem and decide if a memory profiler provides you with long term value.

There are lots of other memory profilers on the market (e.g. .NET Memory Profiler) most of them have free trials, however I have found that the Red-Gate tools are easy to use, so tend try them first.

like image 179
Ian Ringrose Avatar answered Sep 28 '22 15:09

Ian Ringrose


You'll have to use Windbg and Sosex extension.

The !DumpHeap and !GCRoot commands can help you to identify the instance, and all remaining references that keep it alive.

like image 34
Romain Verdier Avatar answered Sep 28 '22 15:09

Romain Verdier