Kyrylo
Kyrylo

Reputation: 533

How to list all managed objects in heap in .Net?

is it possible to list all objects stored in heap. I would like to do something like this:

IEnumerable<GCHandle> listOfObjectsInHeap = GetListOfObjectsFromHeap();

Upvotes: 14

Views: 6730

Answers (3)

Jason Stangroome
Jason Stangroome

Reputation: 4489

Using the ClrMD library you can connect to your own process and inspect the heap.

However, using ClrMD against a running process is known to limit the information available as the heap may be changing as you're trying to walk it.

http://blogs.msdn.com/b/dotnet/archive/2013/05/01/net-crash-dump-and-live-process-inspection.aspx

Upvotes: 6

Julien Lebosquain
Julien Lebosquain

Reputation: 41243

You can use the Profiling API to achieve this. Unfortunately not in managed code.

Upvotes: 5

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

I am not aware of any managed function that allows you to do this. You could retrieve this information using the HeapWalk function. Here's an example of using it (it creates a new heap but you could retrieve the current process heap with GetProcessHeap).

Upvotes: 2

Related Questions