字节[]的最大长度? [英] Maximum length of byte[]?
问题描述
我试图创建字节
的数组(胡)的长度 UInt32.MaxValue
。此数组本质上是一个小的(ISH)的内存数据库:
I'm trying to create an array of byte
s whose length is UInt32.MaxValue
. This array is essentially a small(ish) in-memory database:
byte[] countryCodes = new byte[UInt32.MaxValue];
在我的机器上,但是,在运行时,我收到了出现System.OverflowException
用算术运算导致溢出。
On my machine, however, at run-time, I get a System.OverflowException
with "Arithmetic operation resulted in an overflow".
这是怎么回事?我是否需要使用不安全
块和的malloc
?我会怎么做,在C#?
What's the deal? Do I need to use an unsafe
block and malloc
? How would I do that in C#?
推荐答案
目前实施的System.Array
用途的Int32
其所有内部计数器等,所以元素的理论最大数量为 Int32.MaxValue
。
The current implementation of System.Array
uses Int32
for all its internal counters etc, so the theoretical maximum number of elements is Int32.MaxValue
.
还有由微软CLR实行了最大2GB大小,每个对象的限制。
There's also a 2GB max-size-per-object limit imposed by the Microsoft CLR.
一个很好的讨论和解决办法在这里...
A good discussion and workaround here...
- BigArray<T>, getting around the 2GB array size limit
和一些相关的,而不是-相当重复的,这里的问题和答案...
And a few related, not-quite-duplicate, questions and answers here...
- Is there a limit of elements that could be stored in a List ?
- Very large collection in .Net causes out-of-memory exception
- what is the max limit of data into list in c#?
这篇关于字节[]的最大长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!