ASP.NET Tips: Debugger command you may not know about - gcref

During the course of debugging, often times you will find an object that you would like to know where it came from (who allocated the object).  The first command you will want to run is !gcroot.  But this command will only find the object if it is rooted on a thread or a global currently.  If the object tree is no longer rooted anywhere, then you won't get any results since the object is ready to be collected by the GC.

But there may be other objects that haven't been collected by the GC that hold onto a reference to this object.  This is where !gcref comes in.  This command will print all the parents and children of the given object.  Because we aren't going top down, we can't print the entire tree of parents.

The output looks like:

gcref

One switch that is useful to use with this command is the -objectonly switch.  The reason for this is without the switch, !gcref will cache all the parents and children of all objects in the heap.  If you have a larger dump, that could be a lot of memory and it is possible to run out of memory in the debugger.  So this switch will just look for the one object you are asking for and nothing else.

kick it on DotNetKicks.com