64 位 Windows 上 .NET 中数组的最大长度是多少

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2338778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:59:06  来源:igfitidea点击:

What is the maximum length of an array in .NET on 64-bit Windows

.netarraysmax

提问by Keith Hill

I heard from someone that the maximum array size in .NET is 4 GB? Just wondering if that is true. You wouldn't dream of doing this on 32-bit .NET but on a 64-bit system with 12 GB of RAM, maybe, just maybe you might want to do this. :-)

我听说 .NET 中的最大数组大小是 4 GB?只是想知道这是不是真的。您不会梦想在 32 位 .NET 上执行此操作,而是在具有 12 GB RAM 的 64 位系统上执行此操作,也许只是您可能想要执行此操作。:-)

回答by Reed Copsey

An array could theoretically have at most 2,147,483,647 elements, since it uses an int for indexing. The actual limit is slightly lower than this, depending on the type contained within the array.

一个数组理论上最多可以有 2,147,483,647 个元素,因为它使用 int 进行索引。实际限制略低于此值,具体取决于数组中包含的类型。

However, there is a 2GB maximum single object restriction in the .NET CLR, even in 64bit. This was done by design.

但是,在 .NET CLR 中有 2GB 的最大单个对象限制,即使是 64 位也是如此。这是设计使然

You can easily make an IList<T>implementation that, internally, keeps multiple arrays, and allows you to grow beyond the 2GB single object limit, but there is not one in the framework itself.

您可以轻松地IList<T>实现在内部保留多个数组,并允许您增长超过 2GB 单个对象的限制,但框架本身没有。

Typically, however, this is not a real problem. Most of the time, you'll have arrays pointing to large classes - so the array is just holding references. This would mean your array can effectively point to many, many GBs of memory - but the array itself cannot be >2GB.

然而,通常这不是一个真正的问题。大多数时候,你会有指向大类的数组——所以数组只是保存引用。这意味着您的阵列可以有效地指向很多很多 GB 的内存 - 但阵列本身不能大于 2GB。



Note that, as of .NET 4.5, there is a new option available where 64bit applications can opt-in: gcAllowVeryLargeObjects. With this new option set, it is possible to get UInt32.MaxValue(4,294,967,295) elements in a multi-dimensional array, though a single dimensional array is still limited to 2,146,435,071 elements (2,147,483,591 for single byte arrays or arrays of a struct containing nothing ut a byte).

请注意,从 .NET 4.5 开始,64 位应用程序可以选择加入一个新选项:gcAllowVeryLargeObjects。使用此新选项集,可以UInt32.MaxValue在多维数组中获取(4,294,967,295) 个元素,但一维数组仍限制为 2,146,435,071 个元素(单字节数组或不包含任何字节的结构数组为 2,147,483,591) .

The new rules with this option are:

此选项的新规则是:

  • The maximum number of elements in an array is UInt32.MaxValue.
  • 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.
  • The maximum size for strings and other non-array objects is unchanged.
  • 数组中的最大元素数为 UInt32.MaxValue。
  • 对于字节数组和单字节结构数组,任何单个维度中的最大索引为 2,147,483,591 (0x7FFFFFC7),其他类型的最大索引为 2,146,435,071 (0X7FEFFFFF)。
  • 字符串和其他非数组对象的最大大小不变。

回答by Marc Gravell

In versions of .NET prior to 4.5, the maximum object size is 2GB. From 4.5 onwards you can allocate larger objects if gcAllowVeryLargeObjectsis enabled. Note that the limit for stringis not affected, but "arrays" should cover "lists" too, since lists are backed by arrays.

在 4.5 之前的 .NET 版本中,最大对象大小为 2GB。从 4.5 开始,如果启用了gcAllowVeryLargeObjects,您可以分配更大的对象。请注意,限制string不受影响,但“数组”也应涵盖“列表”,因为列表由数组支持。

回答by Anon.

The maximum size of any one object in .NET is 2GB.

.NET 中任何一个对象的最大大小为 2GB。

This, of course, puts a hard cap on how large you can make a raw array.

当然,这对可以制作原始数组的大小设置了一个硬上限。

You can make an "array of arrays" (and even create your own indexer to access them as though it was one contiguous array) pretty much as large as you like if you define your own class for it.

如果您为它定义自己的类,您可以创建一个“数组数组”(甚至创建自己的索引器来访问它们,就好像它是一个连续的数组一样)。