通过使用Dictionary和List创建数据, 比较那个占用的内存更多
class Program { static void Main(string[] args) { long start = GC.GetTotalMemory(true); //MultData testData=new MultData(); //var gcMultListStr = MultData.MultListStr(); var gcMultDicStr = MultData.MultDicStr(); //Test1 test = new Test1(); //Test2 test = new Test2(); GC.Collect(); GC.WaitForFullGCComplete(); long end = GC.GetTotalMemory(true); //所消耗的内存 long useMemory = end - start; Console.WriteLine("use memory: " + useMemory); Console.Read(); //4194368,4194356 // //536872908,536872896 // //67110884(List),239981092(Dictionary) Dictionary比List消耗很多性能 } } public class MultData { public static List<int> MultListStr() { //监控创建所用到的时间 System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); List<int> list = new List<int>(); for (int i = 0; i < 10000000; i++) { list.Add(i); } watch.Stop(); //显示创建时间 var useTime = (double)watch.ElapsedMilliseconds / 1000; Console.WriteLine("Use time: " + useTime); return list; } public static Dictionary<int, int> MultDicStr() { //监控创建所用到的时间 System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); Dictionary<int, int> dic = new Dictionary<int, int>(); for (int i = 0; i < 10000000; i++) { dic.Add(i, i); } watch.Stop(); //显示创建时间 var useTime = (double)watch.ElapsedMilliseconds / 1000; Console.WriteLine("Use time: " + useTime); return dic; } }