C# 超大数组的 2GB 限制与 gcAllowVeryLargeObjects

在 C# 中,如果数据量太大,就会出现 'System.OutOfMemoryException' 异常。先说明:下面的 gcAllowVeryLargeObjects 只放宽 64 位进程里“单个数组”的 2GB 上限,并不能提高 HashSet/List 或进程总内存上限,也不是所有 OutOfMemoryException 的通用解法。

解决办法来自于Stack Overflow和MSDN    https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/gcallowverylargeobjects-element

1.生成改为x64,即让程序运行在64位模式下(任务管理器里可以看到是否是32位的)。

2.配置文件添加<gcAllowVeryLargeObjects enabled="true" />

具体位置:<configuration> Element
<runtime> Element
<gcAllowVeryLargeObjects> Element

在我当时那个“单个超大数组”的场景里,确实没有再出现 OutOfMemoryException;如果是集合/总体内存不足,还需要分块、流式处理或检查是否运行在 32 位模式。

 

posted @ 2017-12-15 14:24  Easy C#  阅读(1522)  评论(0)    收藏  举报