.net 32​​位最大字节数组大小是否小于? 2GB? [英] Is the .net 32-bit max byte array size < 2gb?

查看:88
本文介绍了.net 32​​位最大字节数组大小是否小于? 2GB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一些与字节数组的最大大小有关的问题(此处这里),并且一直在处理一些数组并获得一些我不太了解的结果。我的代码如下:

I have been looking at some SO questions related to the max size of an array of bytes (here and here) and have been playing with some arrays and getting some results I don't quite understand. My code is as follows:

byte[] myByteArr;
byte[] myByteArr2 = new byte[671084476];

for (int i = 1; i < 2; i++)
{
    myByteArr = new byte[671084476];
}

这将编译并在执行时抛出 myByteArr 的初始化中为'System.OutOfMemoryException'。如果将for循环中的2更改为1,或者注释掉其中一个初始化( myByteArr2 myByteArr )它会正常运行。

This will compile and upon execution it will throw a 'System.OutOfMemoryException' on the initialization of myByteArr. If I change the 2 in the for loop to a 1 or I comment out one of the initialization's (either myByteArr2 or myByteArr) it will run fine.

此外, byte [] myByteArr = new byte [Int32.MaxValue-56]; 会抛出相同的异常。

Also, byte[] myByteArr = new byte[Int32.MaxValue - 56]; throws the same exception.

为什么为32位编译时会发生这种情况?它们不是在2gb限制内吗?

Why does this happen when compiled for 32-bit? Aren't they within the 2gb limit?

推荐答案

32位程序的限制不是针对每个对象的。这是一个过程的限制。总共不能使用超过2GB。

The limits of a 32-bit program are not per-object. It's a process limit. You cannot have more than 2GB total in use.

不仅如此,而且在实践中,由于地址空间碎片,通常很难获得接近2GB的空间。 .NET的托管(即可移动)内存有所帮助,但不能消除此问题。

Not only that, but in practice, it's often difficult to get anywhere near 2GB due to address space fragmentation. .NET's managed (ie. movable) memory helps somewhat, but doesn't eliminate this problem.

即使您使用的是64位进程,也可能会有类似的问题,因为在C#中,数组是由 int 进行索引的,而 int 定义为32位带符号整数,因此无法处理超过2GB边界的数组。个字节。如果您仔细阅读了第二个链接的答案,还会发现每个对象的限制为2GB。您的字节数组大概有一些开销,因此仅原始数据就无法达到完整的2GB。

Even if you are using a 64-bit process, you may have a similar problem because in C# arrays are indexed by an int, which is defined as a 32-bit signed integer, and thus can't address past the 2GB boundary in an array of bytes. If you read the answer to the second link carefully, you'll also see that there is a 2GB per object limit. Your array of bytes presumably has some overhead, so it can't get to the full 2GB just for the raw data.

有关详细信息,请参见注释中的@Habib链接。

See @Habib's link in the comments for details.

这篇关于.net 32​​位最大字节数组大小是否小于? 2GB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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