64 位 Windows 上 .NET 中数组的最大长度是多少 [英] What is the maximum length of an array in .NET on 64-bit Windows

查看:23
本文介绍了64 位 Windows 上 .NET 中数组的最大长度是多少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. :-)

推荐答案

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

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.

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

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

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

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.

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

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

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).

带有此选项的新规则是:

The new rules with this option are:

  • 数组中元素的最大数量为 UInt32.MaxValue.
  • 字节数组和单字节结构数组的任何单个维度的最大索引为 2,147,483,591 (0x7FFFFFC7),其他类型的最大索引为 2,146,435,071 (0X7FEFFFFF).
  • 字符串和其他非数组对象的最大大小不变.

这篇关于64 位 Windows 上 .NET 中数组的最大长度是多少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆