开发者

Finding out how many objects were reclaimed by the C# garbage collector

开发者 https://www.devze.com 2023-01-17 05:11 出处:网络
I am interested to find out how many objects were reclaimed by the garbage co开发者_如何转开发llector after I run the following code.

I am interested to find out how many objects were reclaimed by the garbage co开发者_如何转开发llector after I run the following code.

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


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);
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消