[英]What is the largest array I can create under .NET 4.5?
I'm experimenting with memory limits on my x64 machine (Windows7) under .NET 4.5. 我正在尝试在.NET 4.5下的x64计算机(Windows7)上使用内存限制。 Based on this documentation (as referred to in this answer ) it seems like I should be able to create an array with up to UInt32.MaxValue
elements. 根据此文档 (如本答案中所述 ),似乎我应该能够创建一个最多UInt32.MaxValue
元素的数组。 So I enabled gcAllowVeryLargeObjects
and tried to create the following array: 因此,我启用了gcAllowVeryLargeObjects
并尝试创建以下数组:
int[] arr = new int[UInt32.MaxValue]; //2^32
However, this generates an OverflowException: ("Arithmetic operation resulted in an overflow.") 但是,这会生成OverflowException :(“算术运算导致溢出。”)
So I tried this instead, 所以我尝试了这个
int[] arr = new int[UInt32.MaxValue/2]; //2^31 (i.e., 2 * 1024 * 1024 * 1024)
and it throws a 'System.OutOfMemoryException'. 并抛出“ System.OutOfMemoryException”。
What am I missing here, and what in fact is the largest integer array that I can create? 我在这里缺少什么,实际上我可以创建的最大整数数组是什么?
UPDATE: So, as it turns out, the largest array I can create is: 更新:事实证明,我可以创建的最大数组是:
int[] arr = new int[(long)(1.999 * 1024 * 1024 * 1024)]; //just shy of 2^31
So why can't I reach the limit that the documentation leads me to expect? 那么,为什么我不能达到文档所期望的极限?
From your link: 从您的链接:
The maximum index in any single dimension is 2,147,483,591 (0x7FFFFFC7) for byte arrays and arrays of single-byte structures, and 2,146,435,071 (0X7FEFFFFF) for other types. 对于字节数组和单字节结构数组,任何单个维度的最大索引为2,147,483,591(0x7FFFFFC7),对于其他类型,最大索引为2,146,435,071(0X7FEFFFFF)。
You're exceeding that limit, still. 您仍然超出了该限制。
This should give a darn big array, though: 但是,这应该会给我们带来很大的麻烦:
int[,] arr = new int[UInt32.MaxValue/3,3];
// comes out even ^^^
The total size of that array, which contains the maximum number of elements, should be (16 GB - 4 bytes + metadata) Use an even larger value type to really put the hurt on your PC. 该数组的总大小(包含最大元素数)应为(16 GB-4字节+元数据)。使用更大的值类型将伤害真正地放在您的PC上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.