Is it possible to access the managed heap in a .NET application and e.g. enumerate the objects that are currently allocated there?
I know there are various tools out there that allow you to do that, but I would rather do this myself from code so that I can use it in automated tests, like for checking if everything is disposed and cleaned up after closing a form.
http://sourceforge.net/projects/profilesharp/ has a profiler with the source code available.
You can use the CLR Profiler to see this information:
The only way, besides using a profiler, is with WinDbg and with the SOS extension loaded.
IIRC, you call !EEHeap.
Profilers (using the Profiling API) are ‘external’ components (not quite COM) in the sense that they get loaded by the CLR and has various options of getting called on any method enter/leave/tail call in the managed code (and lots of other stuff as well). They are written in unmanaged code.
AFAIK there is no way to get this info internally without calling out to a profiler/debugger that monitors the CLR.
Also remember that you can not always get this kind of information as much of it only exists after your code has finished execution (many Dispose() calls, finalization and such).
Now you can use the ClrMD Nuget package.
The Nuget package is actually called
Microsoft.Diagnostics.Runtime. It will allow you to view objects in the heap as well of other properties of the CLR runtime.