.NET Arrary OutOfMemoryException

2023-05-16

在.NET中如果数组大小超过2GB,会遇到错误:OutOfMemoryException Array dimensions exceeded supported range.

此时可以通过修改App.config中设置gcAllowVeryLargeObjects 解决。

<configuration>
  <runtime>
    <gcAllowVeryLargeObjects enabled="true" />
  </runtime>
</configuration>
关于gcAllowVeryLargeObjects的官方解释。

Reed Copsey的解释:

An array could theoretically have at most 2,147,483,647 elements, since it uses an int for indexing. The actual limit is slightly lower than this, depending on the type contained within the array.

However, there is a 2GB maximum single object restriction in the .NET CLR, even in 64bit. This was done by design.

You can easily make an IList<T> implementation that, internally, keeps multiple arrays, and allows you to grow beyond the 2GB single object limit, but there is not one in the framework itself.

Typically, however, this is not a real problem. Most of the time, you'll have arrays pointing to large classes - so the array is just holding references. This would mean your array can effectively point to many, many GBs of memory - but the array itself cannot be >2GB.


Note that, as of .NET 4.5, there is a new option available where 64bit applications can opt-in: gcAllowVeryLargeObjects. With this new option set, it is possible to get UInt32.MaxValue(4,294,967,295) elements in a multi-dimensional array, though a single dimensional array is still limited to 2,146,435,071 elements (2,147,483,591 for single byte arrays or arrays of a struct containing nothing ut a byte).

The new rules with this option are:

  • The maximum number of elements in an array is UInt32.MaxValue.
  • The maximum index in any single dimension is 2,147,483,591 (0x7FFFFFC7) for byte arrays and arrays of single-byte structures, and 2,146,435,071 (0X7FEFFFFF) for other types.
  • The maximum size for strings and other non-array objects is unchanged.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

.NET Arrary OutOfMemoryException 的相关文章

  • 使用 JSON.NET 反序列化一些 JSON

    我对 JSON 非常陌生 我需要解析 API 提供的一些内容 谷歌快速搜索出现了JSON NET http james newtonking com pages json net aspx 所以我现在尝试使用它将此 JSON 解析为列表对象
  • 在 Orchard 中设置唯一的主体类和 ID

    有没有办法在 Orchard 中为每页设置唯一的正文类和 ID 我希望能够在 编辑页面 部分控制这些 例如 主页的正文 ID 为 home 关于页面的正文 ID 为 about 等 并且 如果 about 页面下有子页面 这些页面将具有 a
  • 实体框架左外连接和分组抛出:ORA-00907:缺少右括号

    我在基于实体框架的数据访问中使用实体框架来定位多个数据库 我们是一个使用 Entity Framework 已有 2 年的团队 生成的代码与 sql server 2008 完美配合 现在 我们在将数据库迁移到 Oracle 11 Expr
  • 如何使用鼠标拖动和移动winform

    我知道如何通过添加以下代码来 拖动和移动 winform Protected Overrides Sub WndProc ByRef m As Message If m Msg 163 And ClientRectangle Contain
  • 无法通过 HTTPS 调用 Web 服务

    我正在开发一个 Net 应用程序 它与 Web 服务通信以获取一些数据 Net 应用程序和 Web 服务之间的连接是通过 HTTPS 完成的 当我从 Net 应用程序调用 Web 服务时 我得到以下堆栈跟踪 System Net WebEx
  • C# datagridview 列转入数组

    我正在用 C 构建一个程序 并在其中包含一个 datagridview 组件 datagridview 有固定数量的列 2 我想将其保存到两个单独的数组中 但行数确实发生了变化 我怎么能这样做呢 假设一个名为 dataGridView1 的
  • C#中Enum中定义的value__是什么

    What value 可能在这里 value MSN ICQ YahooChat GoogleTalk 我运行的代码很简单 namespace EnumReflection enum Messengers MSN ICQ YahooChat
  • 如何访问窗口?

    我正在尝试使用其句柄访问特定窗口 即System IntPtr value Getting the process of Visual Studio program var process Process GetProcessesByNam
  • System.IO.Compression 和 ZipFile - 提取并覆盖

    我使用标准 VB NET 库来提取和压缩文件 它也可以工作 但是当我必须提取并且文件已经存在时 问题就出现了 我使用的代码 Imports Imports System IO Compression 崩溃时我调用的方法 ZipFile Ex
  • 使用 System.Security.Cryptography 将 Win32 Crypto API 调用转换为 C#

    我接到一项任务 要删除我们产品的一个 dll 并将其替换为纯 C 的 dll 旧的 DLL 是 NET 2 0 托管 C C CLI 它包装了对 Win32 本机加密 API 的调用 新的 DLL 应公开具有相同名称和方法的新对象 但应使用
  • 考虑周末和节假日,添加迄今为止的工作日

    给定一个日期和假期列表 如何将给定的工作日数添加到该日期 对于不考虑假期的较小问题有很多解决方案 例如参见向日期添加天数但不包括周末 https stackoverflow com questions 279296 adding days
  • 检查Web服务是否存在

    有人可以告诉我确定给定 URL 是否存在 Web 服务 ASP NET 的最佳方法吗 我假设一种方法类似于使用 System Net Webclient 发出请求 但我如何确定它是否是有效的 Web 服务以及我应该发出哪种类型的请求 编辑
  • 如何使用 json.net 在生成的 json 中“内联”属性

    我的一个类中有一个属性 我试图用 json net 序列化它 我想 内联 这意味着 我不想将该属性嵌套到具有属性名称的元素中 但其内容直接位于其父级中 这是一个例子 假设我有以下类结构 public interface ISteeringW
  • 如何提取括号(圆括号)之间的文本?

    我有一根绳子User name sales 我想提取括号之间的文本 我该怎么做 我怀疑子字符串 但我无法弄清楚如何阅读 直到右括号 文本的长度会有所不同 如果您希望远离正则表达式 我能想到的最简单的方法是 string input User
  • 有人有 Postsharp 制作经验吗? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 两个 C# 扩展泛型方法之间的不明确调用,其中一个 where T:class 和另一个 where T:struct

    考虑两种扩展方法 public static T MyExtension
  • .NET 有 Base64Stream 吗?

    如果我想生成 Base64 编码的输出 我该如何在 NET 中做到这一点 我知道从 NET 2 0 开始 就有了IC加密转换 http msdn microsoft com en us library system security cry
  • 无法在 Visual Studio 2022 中启动调试适配器

    如果我创建一个启用了 Docker 支持的 ASP Core MVC 目标框架 5 0 并启动它 我会得到 发生一个或多个错误 无法启动调试适配器 附加信息可能会 在输出窗口中可用 操作被取消 这是调试输出 启用 DebugAdapterH
  • VB.NET 是否优化字符串文字的串联?

    如同this https stackoverflow com questions 288794 does c optimize the concatenation of string literals问题 但对于 VB NET 来说 因为我
  • 从 git 签出后 nuget dll 丢失

    I have a C solution containing different projects On those projects I have some normal nuget packages like Newtonsoft Js

随机推荐