【问题标题】:Why do I get System.OverflowException when init array [duplicate]为什么初始化数组时会出现 System.OverflowException [重复]
【发布时间】:2018-06-15 19:08:51
【问题描述】:
byte[] buffer2 = new byte[4294743227]; // string with System.OverflowException 

数字 4294743227 是 uint。

为什么会出现异常?

【问题讨论】:

  • 将近 4 GB 的存储空间!
  • 我不会使用uint,尝试使用long
  • @maccettura,是的。
  • @DanielA.White 实际上,它接近 16 场演出。他试图分配超过 40 亿个 int,每个 int 占用 4 个字节。

标签: c# exception


【解决方案1】:

根据this question,数组的最大大小为System.Int32.MaxValue,即2,147,483,647。

另请参阅System.Array 上的文档:

默认情况下,数组的最大大小为 2 GB。在一个 64位环境,可以通过设置 gcAllowVeryLargeObjects 配置元素的启用属性 在运行时环境中为 true。但是,该数组仍将是 限制为总共 40 亿个元素,并且最大索引为 任何给定维度的 0X7FEFFFFF(0X7FFFFFC7 用于字节数组和 单字节结构的数组)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    相关资源
    最近更新 更多