据我所知,.NET中的单个实例有2 GB的限制.我没有太多关注,因为到目前为止我主要使用32位操作系统.在32但它或多或少是一个人为的限制.但是,我很惊讶地发现这个限制也适用于64位.NET.
由于诸如List<T>使用数组来存储项目之类的集合,这意味着与在64位上运行的相同应用程序相比,在32位上运行的.NET应用程序将能够在列表中保存两倍的引用类型项.这非常令人惊讶.
有谁知道这个限制是否在CLR 4.0中得到解决(目前我手头没有安装4.0).
我试图使用以下代码确定.NET数组(在32位进程中)标头的开销:
long bytes1 = GC.GetTotalMemory(false);
object[] array = new object[10000];
for (int i = 0; i < 10000; i++)
array[i] = new int[1];
long bytes2 = GC.GetTotalMemory(false);
array[0] = null; // ensure no garbage collection before this point
Console.WriteLine(bytes2 - bytes1);
// Calculate array overhead in bytes by subtracting the size of
// the array elements (40000 for object[10000] and 4 for each
// array), and dividing by the number of arrays (10001)
Console.WriteLine("Array overhead: {0:0.000}",
((double)(bytes2 - bytes1) - …Run Code Online (Sandbox Code Playgroud) 我想创建两个200M int元素数组.像那样:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Arr
{
class Program
{
static void Main(string[] args)
{
int Min = 0;
int Max = 10;
int ArrSize = 200000000;
int[] test2 = new int[ArrSize];
int[] test3 = new int[ArrSize];
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,在VS2013下,我出现了内存异常,黄色箭头指向上int[] test3 = new int[ArrSize];线.这台机器有12GB的RAM.
如果我将元素数量减少到150M,则不会抛出任何异常.
如果我只初始化一个大小为200M的数组,则不会抛出任何异常.
为什么?
FileStream fs = File.OpenRead(fullFilePath);
try
{
Console.WriteLine("Read file size is : " + fs.Length);
byte[] bytes = new byte[fs.Length]; //// **error this line**
fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
fs.Close();
return bytes;
}
finally
{
fs.Close();
}
Run Code Online (Sandbox Code Playgroud)
read file size 2,885,760 KB.是错误//
**Arithmetic operation resulted in an overflow.**