• 关于C#内存释放的BUG?


    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);
        }
    }
  • 相关阅读:
    poj 2002 Squares 几何二分 || 哈希
    hdu 1969 Pie
    hdu 4004 The Frog's Games 二分
    hdu 4190 Distributing Ballot Boxes 二分
    hdu 2141 Can you find it? 二分
    Codeforces Round #259 (Div. 2)
    并查集
    bfs
    二维树状数组
    一维树状数组
  • 原文地址:https://www.cnblogs.com/timeObjserver/p/14123788.html
Copyright © 2020-2023  润新知