C# - Is it Possible to Iterate GC Heap?

c#, garbage-collection, multithreading

Solution

The answers to you questions are:

Unfortunately no, you cannot. The CLR's garbage collector works in a mark, sweep, compact pattern so in between runs there is no information about the heap (other than size of the heap or the current generation of a specific type instance) that would allow you to iterate all objects in it.

The best way to monitor the GC is to use `perfmon` and watch (or log) the CLR memory counters.

Problem

I'm not sure whether the following questions are valid.To educate myself i am just asking. ( 1 ) Can I programmatically iterate the GC Heap of all generations ? ( 2 ) Is it possible to watch how does the GC operate on my assembly by launching a thread ?

Original source