关于string.empty 与 "" 内存分配

    int num = 50000000;
            Stopwatch wat = new Stopwatch();
            string[] arr=new string[num];
            
            for (int i = 0; i < num; i++)
            {
                // arr[i] = "";与 arr[i] = string.Empty;和 arr[i] = "字符串";所占内存差不错
                //但是,arr[i] = "5"+i;内存会暴涨,因为前面,三个数组每个元素引用相同,但是这一句数组每个元素都会指向堆中一个新的内存空间
                arr[i] = "的说法";
               
            }
            Console.WriteLine("完成");
            Console.ReadLine();          
            arr = null;
            GC.Collect();
            Console.WriteLine("回收");
            Console.ReadLine();

你可能感兴趣的:(String)