.net 在 CLR 4.0 中,单个对象的大小仍然限制为 2 GB?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1087982/
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
Single objects still limited to 2 GB in size in CLR 4.0?
提问by Brian Rasmussen
As I understand it there's a 2 GB limit on single instances in .NET. I haven't paid a lot of attention to that since I have mainly worked on 32 bit OS so far. On 32 but it is more or less an artificial limitation anyway. However, I was quite surprised to learn that this limitation also applies on 64 bit .NET.
据我了解,.NET 中的单个实例有 2 GB 的限制。我没有太注意这一点,因为到目前为止我主要在 32 位操作系统上工作。在 32 上,但无论如何它或多或少是人为的限制。但是,当我得知此限制也适用于 64 位 .NET 时,我感到非常惊讶。
Since collections such as List<T>use an array to store items, that means that a .NET application running on 32 bit will be able to hold twice as many reference type items in a list compared to the same application running on 64 bit. That is quite surprising imo.
由于诸如List<T>使用数组来存储项目的集合,这意味着与在 64 位上运行的相同应用程序相比,在 32 位上运行的 .NET 应用程序将能够在列表中保存两倍的引用类型项目。这是相当令人惊讶的imo。
Does anyone know if this limitation is addressed in CLR 4.0 (I don't have a 4.0 installation at hand at the moment).
有谁知道这个限制是否在 CLR 4.0 中得到解决(我目前手头没有 4.0 安装)。
回答by Reed Copsey
It's worse than that - you're process space, when you're working in .NET in 32bit is much smaller than the theoretical limit. In 32bit .NET apps, my experience is that you'll always tend to start getting out of memory errors somewhere around 1.2-1.4gb of memory usage (some people say they can get to 1.6... but I've never seen that). Of course, this isn't a problem on 64bit systems.
比这更糟糕 - 你是进程空间,当你在 32 位的 .NET 中工作时,它比理论限制小得多。在 32 位 .NET 应用程序中,我的经验是,您总是倾向于在大约 1.2-1.4gb 的内存使用量(有些人说他们可以达到 1.6... )。当然,这在 64 位系统上不是问题。
That being said, a single 2GB array of reference types, even on 64bit systems, is a huge amount of objects. Even with 8 byte references, you have the ability to allocate an array of 268,435,456 object references - each of which can be very large (up to 2GB, more if they're using nested objects). That's more memory than would ever really be required by most applications.
话虽如此,一个 2GB 的引用类型数组,即使在 64 位系统上,也是大量的对象。即使使用 8 字节引用,您也可以分配一个包含 268,435,456 个对象引用的数组 - 每个引用都可能非常大(最多 2GB,如果使用嵌套对象则更大)。这比大多数应用程序真正需要的内存要多。
One of the members of the CLR team blogged about this, with some options for ways to work around these limitations. On a 64bit system, doing something like his BigArray<T> would be a viable solution to allocate any number of objects into an array - much more than the 2gb single object limit. P/Invoke can allow you to allocate larger arrays as well.
CLR 团队的一名成员就此发表了博客文章,并提供了一些解决这些限制的方法。在 64 位系统上,执行类似他的 BigArray<T> 将是将任意数量的对象分配到数组中的可行解决方案 - 远远超过 2gb 的单个对象限制。P/Invoke 也可以让您分配更大的数组。
Edit: I should have mentioned this, as well - I do not believe this behavior has changed at all for .NET 4. The behavior has been unchanged since the beginning of .NET.
编辑:我也应该提到这一点 - 我认为 .NET 4 的这种行为根本没有改变。自 .NET 开始以来,这种行为一直没有改变。
Edit: .NET 4.5 will now have the option in x64 to explicitly allow objects to be larger than 2gb by setting gcAllowVeryLargeObjectsin the app.config.
编辑:.NET 4.5 现在可以在 x64 中通过在 app.config 中设置gcAllowVeryLargeObjects来明确允许对象大于2gb。
回答by Alina Popa
.NET Framework 4.5allows creating arrays larger than 2GB on 64 bit platforms. This feature is not on by default, it has to be enabled via config file using the gcAllowVeryLargeObjects element.
.NET Framework 4.5允许在 64 位平台上创建大于 2GB 的数组。此功能默认不启用,必须通过配置文件使用 gcAllowVeryLargeObjects 元素启用。
http://msdn.microsoft.com/en-us/library/hh285054(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/hh285054(v=vs.110).aspx
回答by Trevor Misfeldt
This is a big deal in the numerical field. Anyone using numerical class libraries in .NET has their matrices stored as arrays underneath. This is so native libraries can be called to do the number-crunching. The 2GB limit seriously hampers the size of matrices possible in 64-bit .NET. More here.
这在数值领域是一件大事。在 .NET 中使用数值类库的任何人都将他们的矩阵存储在下面的数组中。这样就可以调用本机库来进行数字运算。2GB 的限制严重阻碍了 64 位 .NET 中矩阵的大小。更多在这里。

