views:

70

answers:

2

Guys,

I am interested to find out how many objects were reclaimed by the garbage collector after I run the following code.

if (ObjectsOutstanding > GCThreshold) {
    System.GC.Collect();
}

Could you please help me out with this one?

A: 

In the Visual Studio debugger you can use the SOS library in the immediate window. There are several GC methods that give you the information you need. I don't know if this is what you want, but it is a starting point.

testalino
+1  A: 

One easy way of doing this for your own custom-types would be to increment and track some counter in their finalizers.

e.g.

public class MyCustomType
{
    public static int NumFinalizersCalled;

    ~MyCustomType()
    {
        Interlocked.Increment(ref NumFinalizersCalled);
    }
}
Ani
@ Henk Holterman: `GC.WaitForPendingFinalizers` would fix that, wouldn't it?
Ani
@Ani: I'd throw an `#if DEBUG` precompiler directive in there so that it does not make it into a release build.
Brian Gideon