當前位置:
首頁 >
关于C#内存释放的BUG?
發布時間:2025/3/18
38
豆豆
生活随笔
收集整理的這篇文章主要介紹了
关于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(){//測試1MyGCCollectClass.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);}
}
與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
以上是生活随笔為你收集整理的关于C#内存释放的BUG?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于VS附加调试Unity引起卡顿无响应
- 下一篇: C++C#联合调试