In C# 2008, what is the Maximum Size that an Array can hold?
在c# 2008中,数组可以容纳的最大大小是多少?
7 个解决方案
#1
47
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.
如果你的意思是系统。数组,即。任何通常定义的数组(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.
这个限制是强制执行的,因为系统。数组使用Int32作为索引器,因此只能使用Int32的有效值。除此之外,只能使用正值(即>= 0)。这意味着数组大小上的最大上限是Int32值上的最大上限,Int32中提供了这个上限。MaxValue,相当于2 ^ 31日,约20亿人。
On a completely different note, if you're worrying about this, it's likely you're using alot of 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.
另一个完全不同的注意,如果你担心这个,很可能你正在使用大量的数据,不管是正确的还是错误的。在本例中,我将使用List
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
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还可以控制索引器的类型,并允许使用负值。对于绝对最大大小的稀疏数组,您可以使用字典
#2
14
Per MSDN it is
每MSDN是
By default, the maximum size of an Array is 2 gigabytes (GB).
默认情况下,数组的最大大小是2g (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。
#3
9
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。对于。net在4.5之前,它也是一个数组的内存限制。在。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;
}
#4
4
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- maxim- 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上,所以这取决于数组中的内容。
#5
0
Since Length is an int I'd say Int.MaxValue
因为长度是int . maxvalue
#6
0
I think if you don't consider the VM, it is Integer.MaxValue
我认为如果你不考虑VM,它是Integer.MaxValue
#7
-1
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(或者可能是虚拟内存)空间相关联,并将最大限制为您的OS版本(例如32位或64位)
#1
47
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.
如果你的意思是系统。数组,即。任何通常定义的数组(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.
这个限制是强制执行的,因为系统。数组使用Int32作为索引器,因此只能使用Int32的有效值。除此之外,只能使用正值(即>= 0)。这意味着数组大小上的最大上限是Int32值上的最大上限,Int32中提供了这个上限。MaxValue,相当于2 ^ 31日,约20亿人。
On a completely different note, if you're worrying about this, it's likely you're using alot of 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.
另一个完全不同的注意,如果你担心这个,很可能你正在使用大量的数据,不管是正确的还是错误的。在本例中,我将使用List
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
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还可以控制索引器的类型,并允许使用负值。对于绝对最大大小的稀疏数组,您可以使用字典
#2
14
Per MSDN it is
每MSDN是
By default, the maximum size of an Array is 2 gigabytes (GB).
默认情况下,数组的最大大小是2g (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。
#3
9
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。对于。net在4.5之前,它也是一个数组的内存限制。在。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;
}
#4
4
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- maxim- 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上,所以这取决于数组中的内容。
#5
0
Since Length is an int I'd say Int.MaxValue
因为长度是int . maxvalue
#6
0
I think if you don't consider the VM, it is Integer.MaxValue
我认为如果你不考虑VM,它是Integer.MaxValue
#7
-1
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(或者可能是虚拟内存)空间相关联,并将最大限制为您的OS版本(例如32位或64位)