Search code examples
visual-studiodebuggingwindbggarbage-collectionsos

Is it possible to implement a GC.GetAliveInstancesOf<T>() (for use in debugging)?


I know this was answered before, but I'd like to pose a somewhat different question.

Is there any conceivable way to implement GC.GetAliveInstancesOf(), that can be evaluated in Visual Studio Debug Watch window? Sasha Goldstein shows one solution in this article, but it requires every class you want to query inherit from a specific base class.

I'll emphasize that I only want to use this method during debugging, so I don't care that the GC may change an object's address in memory during runtime.

One idea might be to somehow harness the !dumpheap –type command of SOS, and do some magic trick to create a temporary variable and have it point out to the memory address printed by SOS.

Does anyone have a solution that works?


Solution

  • It looks like something very similar to this was implemented by Alois Kraus in the WMemoryProfiler project.