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

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

问题描述

我正在执行此命令

  Dictionary< UInt64,int> myIntDict =新字典< UInt64,int>(89478458); 

此错误:

  System.OutOfMemoryException未处理HResult = -2147024882 
消息=数组尺寸超出了支持的范围。
Source = mscorlib
StackTrace:
在System.Collections.Generic.Dictionary`2.Initialize(Int32容量)
在System.Collections.Generic.Dictionary`2..ctor (Int32容量,IEqualityComparer`1比较器)

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

  private void初始化(int容量)
{
int size = HashHelpers.GetPrime(capacity);
...
个条目=新条目[大小];
...
}

当我重现此错误时,数组创建。在这种情况下,条目是一个结构,大小为24。当我们获得最大int32(0x80000000-1)并除以24 = 89478485时,该数字介于质数89478457和89478503之间。



这是否意味着该结构数组不能像maxInt32 / sizeOfThisStruct一样大?



编辑:





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



是的,狗仔队是正确的。我必须考虑,如何不浪费内存。
谢谢大家。

解决方案

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

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


I am getting on this command

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

this error:

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)

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];
        ...
    }

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.

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

EDIT:

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.

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.

解决方案

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天全站免登陆