using System;
class MyData
{
    public int[] dat;
    public MyData(int size)
    {
        dat = new int[size];
        Console.WriteLine($"构造");
    }
    ~MyData()
    {
        Console.WriteLine($"析构");
    }
}
class MyGCCollectClass
{
    static MyData theData;
    static void Main()
    {
        //测试1
        MyGCCollectClass.MakeSomeGarbage1();
        //测试2
        //MyGCCollectClass.MakeSomeGarbage2();
        //theData = null;
        //测试3
        //var vt = new MyData(1024 * 1024 * 1024);
        //vt = null;
        //测试4
        //theData = new MyData(1024 * 1024);
        //theData = null;
        Console.WriteLine("Memory used before collection:       {0:N0}", GC.GetTotalMemory(false));
        GC.Collect(); 
        Console.WriteLine("Memory used after full collection:   {0:N0}", GC.GetTotalMemory(true));
        /*** 测试结果
         * 成功回收:测试1,测试2
         * 不能回收:测试3,测试4
         */
        Console.Read();
    }
    static void MakeSomeGarbage1()
    {
        theData = new MyData(1024*1024);
        theData = null;
    }
    static void MakeSomeGarbage2()
    {
        theData = new MyData(1024 * 1024);
    }
}