Timo
Timo

Reputation: 1088

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

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.

Upvotes: 4

Views: 1285

Answers (5)

BanksySan
BanksySan

Reputation: 28500

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.

Upvotes: 1

Robert Harvey
Robert Harvey

Reputation: 180788

http://sourceforge.net/projects/profilesharp/ has a profiler with the source code available.

Upvotes: 2

S.Skov
S.Skov

Reputation: 707

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).

Upvotes: 1

leppie
leppie

Reputation: 117230

The only way, besides using a profiler, is with WinDbg and with the SOS extension loaded.

IIRC, you call !EEHeap.

Upvotes: 1

Keltex
Keltex

Reputation: 26426

You can use the CLR Profiler to see this information:

http://www.microsoft.com/downloads/details.aspx?familyid=86ce6052-d7f4-4aeb-9b7a-94635beebdda&displaylang=en

Upvotes: 1

Related Questions