测试垃圾回收次数

static int collectionCount = 0;
static Stopwatch watch = null;
static int testCount = 100000;

static void TestBegin()
{
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    collectionCount = GC.CollectionCount(0);
    watch = new StopWatch();
    watch.Start();
}

static void TestEnd()
{
    wtach.Stop();
    Console.WriteLine("Time Cost:" + watch.ElapsedMilliseconds.ToString());
    Console.WriteLine("GC Count:" + (GC.CollectionCount(0) - collectionCount));
}
TestBegin();

// xxx

TestEnd();

你可能感兴趣的:(测试垃圾回收次数)