对于我在C#中的编程练习,我试图创建一个long数组,长度为0x1fffffff(base10中为536,870,911),但是我得到了System.OutOfMEmoryException.
对于构建,我针对x64系统,我在Windows7 x64上运行VisualStudio2008,内存为8GB.它应该是数组的足够内存(它适用于JDK x64和CPP项目)
有什么想法吗 ?
const long MAX = 0x1fffffff; // 536870911 in base10
program.arr = new long[MAX];
for (long i = 0; i < MAX; i++)
{
program.arr[i] = i;
}
Run Code Online (Sandbox Code Playgroud) 考虑以下:
long size = int.MaxValue;
long[] huge = new long[size]; // throws OutOfMemoryException
long[] huge = new long[size + 1]; // throws OverflowException
Run Code Online (Sandbox Code Playgroud)
我知道单个对象的大小有2GB的限制,这解释了第一个异常,但是为什么一旦元素数量超过32位,我会得到一个不同的异常?
(如果这很重要,我正在使用64位计算机).
编辑:我也可以定义和使用一个long没有问题的索引器:
internal sealed class MyClass
{
public object this[long x]
{
get
{
Console.WriteLine("{0}", x);
return null;
}
}
}
...
long size = int.MaxValue;
MyClass asdf = new MyClass();
object o = asdf[size * 50]; // outputs 107374182350
Run Code Online (Sandbox Code Playgroud)