Can I programmatically view the contents of a managed heap from a .NET application?

Is it possible to access the managed heap in a .NET application and, for example, list the objects that are currently allocated there?

I know that there are various tools that allow you to do this, but I would prefer to do it myself from the code so that I can use it in automated tests, for example, to check whether everything is disposed of and cleared after closing the form.

+4
source share
5 answers
+2
source

The only way besides using the profiler is to use WinDbg and the SOS extension.

IIRC, you are calling! EEHeap.

+1
source

Profiles (using the profiling API) are "external" components (not exactly COM) in the sense that they are loaded by the CLR and have various options for calling the invoked method of any enter / leave / tail method in managed code (and many other things). They are written in unmanaged code.

AFAIK there is no way to get this information internally without accessing the profiler / debugger that monitors the CLR. Also remember that you cannot always get such information, since part of it exists only after your code completes (many Dispose () calls, completion, etc.).

+1
source

Now you can use the ClrMD Nuget package.

The Nuget package is actually called Microsoft.Diagnostics.Runtime . This will allow you to view objects on the heap, as well as other CLR runtime properties.

+1
source

All Articles