C# 数组可以容纳的最大大小是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1391672/
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
What is the Maximum Size that an Array can hold?
提问by DNR
In C# 2008, what is the Maximum Size that an Array can hold?
在 C# 2008 中,数组可以容纳的最大大小是多少?
采纳答案by Matthew Scharley
System.Int32.MaxValue
Assuming you mean System.Array
, ie. any normally defined array (int[]
, etc). This is the maximum number of values the array can hold. The size of each value is only limited by the amount of memory or virtual memory available to hold them.
假设你的意思是System.Array
,即。任何通常定义的数组(int[]
等)。这是数组可以容纳的最大值数。每个值的大小仅受可用于容纳它们的内存或虚拟内存量的限制。
This limit is enforced because System.Array
uses an Int32
as it's indexer, hence only valid values for an Int32
can be used. On top of this, only positive values (ie, >= 0
) may be used. This means the absolute maximum upper bound on the size of an array is the absolute maximum upper bound on values for an Int32
, which is available in Int32.MaxValue
and is equivalent to 2^31, or roughly 2 billion.
强制执行此限制是因为System.Array
使用 anInt32
作为它的索引器,因此只能使用 an的有效值Int32
。除此之外,只能使用正值(即>= 0
)。这意味着数组大小的绝对最大上限是 an 值的绝对最大上限Int32
,可Int32.MaxValue
用于 2^31,或大约 20 亿。
On a completely different note, if you're worrying about this, it's likely you're using alotof data, either correctly or incorrectly. In this case, I'd look into using a List<T>
instead of an array, so that you are only using as much memory as needed. Infact, I'd recommend using a List<T>
or another of the generic collection types all the time. This means that only as much memory as you are actually using will be allocated, but you can use it like you would a normal array.
另一方面,如果您对此感到担忧,很可能您正在使用大量数据,无论是正确的还是错误的。在这种情况下,我会考虑使用 aList<T>
而不是数组,以便您只使用所需的内存。事实上,我建议一直使用一种List<T>
或另一种泛型集合类型。这意味着只会分配与您实际使用一样多的内存,但您可以像使用普通数组一样使用它。
The other collection of note is Dictionary<int, T>
which you can use like a normal array too, but will only be populated sparsely. For instance, in the following code, only one element will be created, instead of the 1000 that an array would create:
另一个笔记集合是Dictionary<int, T>
您也可以像普通数组一样使用的集合,但只会稀疏地填充。例如,在以下代码中,只会创建一个元素,而不是数组将创建的 1000:
Dictionary<int, string> foo = new Dictionary<int, string>();
foo[1000] = "Hello world!";
Console.WriteLine(foo[1000]);
Using Dictionary
also lets you control the type of the indexer, and allows you to use negative values. For the absolute maximal sized sparse array you could use a Dictionary<ulong, T>
, which will provide more potential elements than you could possible think about.
使用Dictionary
还可以让您控制索引器的类型,并允许您使用负值。对于绝对最大大小的稀疏数组,您可以使用 a Dictionary<ulong, T>
,它将提供比您想象的更多的潜在元素。
回答by waqasahmed
I think it is linked with your RAM (or probably virtual memory) space and for the absolute maximum constrained to your OS version (e.g. 32 bit or 64 bit)
我认为它与您的 RAM(或可能是虚拟内存)空间以及受您的操作系统版本(例如 32 位或 64 位)限制的绝对最大值有关
回答by James Black
Here is an answer to your question that goes into detail: http://www.velocityreviews.com/forums/t372598-maximum-size-of-byte-array.html
这是您的问题的详细答案:http: //www.velocityreviews.com/forums/t372598-maximum-size-of-byte-array.html
You may want to mention which version of .NET you are using and your memory size.
您可能想要提及您使用的是哪个版本的 .NET 以及您的内存大小。
You will be stuck to a 2G, for your application, limit though, so it depends on what is in your array.
对于您的应用程序,您将被困在 2G 上,但有限制,因此这取决于您的阵列中的内容。
回答by Preet Sangha
Since Length is an int I'd say Int.MaxValue
由于 Length 是一个 int 我会说 Int.MaxValue
回答by Peter Lee
I think if you don't consider the VM, it is Integer.MaxValue
我想如果你不考虑虚拟机,那就是 Integer.MaxValue
回答by Sai
Per MSDN it is
根据 MSDN,它是
By default, the maximum size of an Array is 2 gigabytes(GB).
默认情况下,数组的最大大小为2 GB。
In a 64-bit environment, you can avoid the size restriction by setting the enabled attribute of the gcAllowVeryLargeObjects configuration element to true in the run-time environment.
在 64 位环境中,您可以通过在运行时环境中将 gcAllowVeryLargeObjects 配置元素的 enabled 属性设置为 true 来避免大小限制。
However, the array will still be limited to a total of 4 billion elements.
但是,该数组仍将限制为总共40 亿个元素。
Refer Here http://msdn.microsoft.com/en-us/library/System.Array(v=vs.110).aspx
请参阅此处http://msdn.microsoft.com/en-us/library/System.Array(v=vs.110).aspx
Note:Here I am focusing on the actual length of array by assuming that we will have enough hardware RAM.
注意:这里我假设我们有足够的硬件 RAM 来关注数组的实际长度。
回答by Anton K
This answer is about .NET 4.5
这个答案是关于 .NET 4.5
According to MSDN, the index for array of bytes cannot be greater than 2147483591. For .NET prior to 4.5 it also was a memory limit for an array. In .NET 4.5 this maximum is the same, but for other types it can be up to 2146435071.
根据 MSDN,字节数组的索引不能大于 2147483591。对于 4.5 之前的 .NET,它也是数组的内存限制。在 .NET 4.5 中,这个最大值是相同的,但对于其他类型,它可以达到 2146435071。
This is the code for illustration:
这是用于说明的代码:
static void Main(string[] args)
{
// -----------------------------------------------
// Pre .NET 4.5 or gcAllowVeryLargeObjects unset
const int twoGig = 2147483591; // magic number from .NET
var type = typeof(int); // type to use
var size = Marshal.SizeOf(type); // type size
var num = twoGig / size; // max element count
var arr20 = Array.CreateInstance(type, num);
var arr21 = new byte[num];
// -----------------------------------------------
// .NET 4.5 with x64 and gcAllowVeryLargeObjects set
var arr451 = new byte[2147483591];
var arr452 = Array.CreateInstance(typeof(int), 2146435071);
var arr453 = new byte[2146435071]; // another magic number
return;
}