Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 383
Kirill Avatar asked May 29 '10 08:05

Kirill


People also ask

What is managed heap in C#?

The managed heapAfter the CLR initializes the garbage collector, it allocates a segment of memory to store and manage objects. This memory is called the managed heap, as opposed to a native heap in the operating system.

Does C# have automatic garbage collection?

The garbage collector serves as an automatic memory manager. You do not need to know how to allocate and release memory or manage the lifetime of the objects that use that memory. An allocation is made any time you declare an object with a “new” keyword or a value type is boxed. Allocations are typically very fast.

What is pinned object heap?

Pinned objects are prohibited from moving within the heap. Typically, such objects are used by some unmanaged code or may be a result of using the fixed statement. Total size of all objects (excluding pinned objects) allocated in the heap segment.

What is the difference between stack and heap memory in C#?

Stack accesses local variables only while Heap allows you to access variables globally. Stack variables can't be resized whereas Heap variables can be resized. Stack memory is allocated in a contiguous block whereas Heap memory is allocated in any random order.


1 Answers

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

like image 56
Jason Stangroome Avatar answered Sep 27 '22 18:09

Jason Stangroome