[英]gcAllowVeryLargeObjects is set but still causing System.ArgumentOutOfRangeException
我有点想法了。 使用以下代码,我尝试安装一个大于2GB的字节数组:
var b = Array.CreateInstance(typeof(byte), uint.MaxValue);
每次都会导致System.ArgumentOutOfRangeException
异常,并提示arrays larger then 2GB are not supported
。
我的App.config当前如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
<runtime>
<gcAllowVeryLargeObjects enabled="true" />
</runtime>
</configuration>
此外,该项目的目标平台是x64
我将不胜感激。 如果缺少任何信息,我将尽快更新问题。
我也尝试过uint.MaxValue
仅出于完整性检查的目的,您尝试分配9.223 EB(艾字节)的顺序内存块,即9.223×10 ^ 9 GB(千兆字节)。 很简单,但是您甚至无法在x64机器上执行此操作,因为无论如何都会使用一些内存,这将是最大的。
而是尝试使用动态增长的列表:
var b = new List<byte>();
对于字节数组和单字节结构数组,任何单个维度的最大索引为2,147,483,591(0x7FFFFFC7),对于其他类型,最大索引为2,146,435,071(0X7FEFFFFF)。 -来源: https : //docs.microsoft.com/zh-CN/dotnet/framework/configure-apps/file-schema/runtime/gcallowverylargeobjects-element
gcallowverylargeobjects-element的作用是,您可以定义超过2Gb的多维数组,对于其他数据类型,可以分配2146435071 * data_type_size内存。 例如,int32包含4个字节,因此它将为8.586 GB(千兆字节)。
一维数组不能包含多个int.MaxValue
元素,即使它们使用<gcAllowVeryLargeObjects
可以大于new int[int.MaxValue / 2]
(例如, new int[int.MaxValue / 2]
为new int[int.MaxValue / 2]
)。 要解决此问题,您必须创建二维数组,或使用其他类型,例如
public struct BytePair
{
public byte First, Second;
}
然后创建一个BytePair[]
,其大小是等效byte[]
的一半
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.