windows 64 位 .Net 应用程序中的内存限制?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6107322/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:54:48  来源:igfitidea点击:

Memory limitations in a 64-bit .Net application?

c#.netwindowsmemory-management64-bit

提问by Dan Bystr?m

On my laptop, running 64 bit Windows 7 and with 2 Gb of free memory (as reported by Task Manager), I'm able to do:

在我的笔记本电脑上,运行 64 位 Windows 7 和 2 Gb 可用内存(根据任务管理器的报告),我能够执行以下操作:

var x = new Dictionary<Guid, decimal>( 30 * 1024 *1024 );

Without having a computer with more RAM at my hands, I'm wondering if this will scale so that on a computer with 4 Gb free memory, I'll be able to allocate 60M items instead of "just" 30M and so on?

如果我手头没有一台具有更多 RAM 的计算机,我想知道这是否可以扩展,以便在具有 4 Gb 可用内存的计算机上,我将能够分配 60M 项目而不是“仅”30M 等等?

Or are there other limitations (of .Net and/or Windows) that I'll bump into before I'm able to consume all available RAM?

或者在我能够消耗所有可用 RAM 之前,我会遇到其他限制(.Net 和/或 Windows)吗?

Update:OK, so I'm not allowed to allocate a single object larger than 2 Gb. That's important to know! But then I'm of course curious to know if I'll be able to fully utilize all memory by allocating 2 Gb chunks like this:

更新:好的,所以我不允许分配大于 2 Gb 的单个对象。知道这一点很重要!但是我当然很想知道我是否能够通过像这样分配 2 Gb 块来充分利用所有内存:

  var x = new List<Dictionary<Guid, decimal>>();
  for ( var i = 0 ; i < 10 ; i++ )
    x.Add( new Dictionary<Guid, decimal>( 30 * 1024 *1024 ) );

Would this work if the computer have >20Gb free memory?

如果计算机的可用内存大于 20Gb,这会起作用吗?

采纳答案by John Leidegren

There's a 2 GiB limitation on all objects in .NET, you are never allowed to create a single object that exceeds 2 GiB. If you need a bigger object you need to make sure that the objects is built from parts smaller than 2 GiB, so you cannot have an array of continuous bits larger than 2 GiB or a single string longer larger than 512 MiB, I'm not entirely sure about the string but I've done some testing on the issue and was getting OutOfMemoryExceptions when I tried to allocate strings bigger than 512 MiB.

.NET 中的所有对象都有 2 GiB 的限制,您永远不能创建超过 2 GiB 的单个对象。如果你需要一个更大的对象,你需要确保这些对象是由小于 2 GiB 的部分构建的,所以你不能有大于 2 GiB 的连续位数组或大于 512 MiB 的单个字符串,我不是完全确定字符串,但我已经对这个问题进行了一些测试,并且当我尝试分配大于 512 MiB 的字符串时出现 OutOfMemoryExceptions。

These limits though are subject to heap fragmentation and even if the GC does try to compact the heap, large objects (which is somewhat of an arbitrary cross over around 80K) end up on the large object heap which is a heap that isn't compacted. Strictly speaking, and somewhat of a side note, if you can maintain short lived allocations below this threshold it would be better for your overall GC memory management and performance.

这些限制虽然受堆碎片的影响,即使 GC 确实尝试压缩堆,大对象(在 80K 左右的范围内有点任意交叉)最终会出现在大对象堆上,这是一个未被压缩的堆. 严格来说,也有些附带说明,如果您可以将短期分配保持在此阈值以下,那么对您的整体 GC 内存管理和性能会更好。

回答by Eldritch Conundrum

Update: The 2Gb single-object memory limit has been lifted on 64 bit with the release of .NET 4.5.

更新:随着 .NET 4.5 的发布,2Gb 单对象内存限制已在 64 位上解除。

You'll need to set gcAllowVeryLargeObjectsin your app.config.

您需要gcAllowVeryLargeObjects在 app.config 中进行设置。

The maximum number of elements in an array is still 2^32-1, though.

不过,数组中元素的最大数量仍然是 2^32-1。

See Single objects still limited to 2 GB in size in CLR 4.0?for more details.

请参阅CLR 4.0 中单个对象的大小仍然限制为 2 GB?更多细节。