C#之数组索引范围

一般情况,C#数组仅支持2GB大小,对于int[]来讲,大概就是:2*1024*1024*1024/4=530870912,但实际上
比这个数值略小。

 int bb = 536870897;
 while (true)
 {
     bb++;
     try
     {
         int[] aa = new int[bb];
     }
     catch
     {
         int[] cc = new int[bb / 2];
         Console.WriteLine($"是{bb},{((double)bb) * 4.0 / 1024 / 1024 / 1024}GB");
        
         break;
     }
 }
 Console.ReadLine();

image

但是通过修改exe.config中的配置选项,可以将默认2GB大小,提高到8GB!

image

 int bb = 536870897 * 4;
 while (true)
 {
     bb++;
     try
     {
         int[] aa = new int[bb];
     }
     catch
     {
         int[] cc = new int[bb / 2];
         Console.WriteLine($"是{bb},{((double)bb) * 4.0 / 1024 / 1024 / 1024}GB");
        
         break;
     }
 }
 Console.ReadLine();

image

posted @ 2025-07-04 22:28  JohnYang819  阅读(12)  评论(0)    收藏  举报