[英]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.