How to find dependent instances of classes in a dump file. The heap has a heap.EnumerateRoots () method. Returns a collection of created instances, and how can you establish links between these instances? Those. What interests you is not just the inheritance of one class from another, but, for example, that an element belongs to a collection of another class. Or it is a property and so on.

  • Instances can be iterated using the heap.EnumerateObjectAddresses () method, but there is also no hint of dependencies with other classes. - Eugene

1 answer 1

I found a solution here https://dzone.com/articles/traversing-gc-heap-clrmd . Finished for complete clarity.

using (var dataTarget = DataTarget.LoadCrashDump(dmpFile)) { var runtime = dataTarget.ClrVersions[0].CreateRuntime(); var heap = runtime.GetHeap(); foreach (var clrRoot in heap.EnumerateRoots()) { var stack = new Stack<ulong>(); stack.Push(clrRoot.Object); var hashSet = new HashSet<ulong>(); DisplayRefChainIfReachedObject(clrRoot.Object, clrRoot, stack, hashSet); } } static void DisplayRefChainIfReachedObject(ulong objPtr, ClrRoot root, Stack<ulong> refChain, HashSet<ulong> visited) { ulong currentObj = refChain.Peek(); if (visited.Contains(currentObj)) return; visited.Add(currentObj); ClrType type = heap.GetObjectType(currentObj); type.EnumerateRefsOfObject(currentObj, (innerObj, fieldOffset) => { refChain.Push(innerObj); DisplayRefChainIfReachedObject(objPtr, root, refChain, visited); refChain.Pop(); }); } 

It turns out from the root objects of the process, we get a complete dependency graph of all instances of classes created in the process and stored in a dump.