当字典计数大于 89478457 时出错 [英] Error when Dictionary count is bigger as 89478457

查看:31
本文介绍了当字典计数大于 89478457 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在接受这个命令

Dictionary<UInt64, int> myIntDict = new Dictionary<UInt64, int>(89478458);

这个错误:

System.OutOfMemoryException was unhandled  HResult=-2147024882
Message=Array dimensions exceeded supported range.
Source=mscorlib
StackTrace:
   at System.Collections.Generic.Dictionary`2.Initialize(Int32 capacity)
   at System.Collections.Generic.Dictionary`2..ctor(Int32 capacity, IEqualityComparer`1 comparer)

在 89478457 上没有错误.这是Dictionary.cs:

On 89478457 there is no error. Here is the source of Initialize in Dictionary.cs:

    private void Initialize(int capacity)
    {
        int size = HashHelpers.GetPrime(capacity);
        ...
        entries = new Entry[size];
        ...
    }

当我重现这个时,错误发生在数组创建上.在这种情况下,条目是一个大小为 24 的结构体.当我们得到 max int32 (0x80000000-1) 并除以 24 = 89478485 并且这个数字介于质数 89478457 和 89478503 之间时.

When I reproduce this, the error happens on the array creation. Entry is a struct in this case with size 24. When we get max int32 (0x80000000-1) and divide on 24 = 89478485 and this number is between prime numbers 89478457 and 89478503.

这是否意味着结构数组不能比 maxInt32/sizeOfThisStruct 大?

Does this mean, that array of struct cannot be bigger as maxInt32/sizeOfThisStruct?

是的.我实际上超过了 2 GB.这发生在字典创建 struct Entry 的内部数组时,(键,值)对存储在哪里.在我的例子中,sizeof(Entry) 是 24 字节,因为值类型是内联分配的.

Yes. I actually go over 2 GB. This happens, when the dictionary creates the internal array of struct Entry, where are the (key,value) pairs stored. In my case the sizeof(Entry) is 24 bytes and as value type is inline allocated.

解决方案是使用 gcAllowVeryLargeObjects 标志(谢谢Evk).实际上在 .net 核心中,标志是 环境变量 COMPlus_gcAllowVeryLargeObjects(谢谢svick).

And the solution is to use the gcAllowVeryLargeObjects flag (thank you Evk). Actually in .net core the flag is the environment variable COMPlus_gcAllowVeryLargeObjects (thank you svick).

是的,狗仔队是对的.我得想想,怎么不浪费内存.谢谢你们.

And yes, Paparazzi is right. I have to think about, how not to waste memory. Thank you all.

推荐答案

.NET 运行时存在已知限制 - 堆上允许的最大对象大小为 2 GB,即使在 64 位版本的运行时也是如此.但是,从 .NET 4.5 开始,有 配置选项 允许您放宽此限制(仅在 64 位版本的运行时)并创建更大的数组.启用它的配置示例是:

There is known limitation of .NET runtime - maximum object size allowed on the heap is 2 GB, even on 64-bit version of runtime. But, starting from .NET 4.5 there is configuration option which allows you to relax this limit (only on 64-bit version of runtime still) and create larger arrays. Example of configuration to enable that is:

<configuration>
  <runtime>
    <gcAllowVeryLargeObjects enabled="true" />
  </runtime>
</configuration>

这篇关于当字典计数大于 89478457 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆