如何计算出的能力参数传递给字典的构造,以避免OutOfMemoryException异常最大值?最大值、计算出、字典、异常

2023-09-03 17:40:42 作者:抠鼻子抠出梦想つ

与此相关的:

System.OutOfMemoryException因为大词典

我收到 OutOfMemoryException异常时,我让.NET管理我的字典。在调整大小的方法抛出的异常。所以我试图避免调整大小,提供大量的容量。当然,我试图从 int.MaxValue 开始只是为了确保这将有助于解决这个问题。我正打算根据需要往下走之多。

I am getting OutOfMemoryException when I let .NET manage my dictionary. The exception thrown in resize method. So I was trying to avoid resizing by providing large number as a capacity. Naturally I tried to start from int.MaxValue just to make sure it will help to solve the problem. I was planning to go down as much as needed.

然而 OutOfMemoryException异常被抛出 int.MaxValue 。我决定做一个小的二进制搜索和能力被接受的构造函数中的第一个值转作 int.MaxValue / 32 。不仅如此小于我所预期的,但它也只是混乱。

However OutOfMemoryException is thrown for int.MaxValue. I decided to make a little binary search and the very first value for capacity that was accepted by constructor turned to be int.MaxValue / 32. Not only that is smaller than I have expected but it is also just confusing.

所以,有没有人有任何想法,为什么?

So has anyone have any ideas why?

呵呵,项目设置都设置为使用x64体系结构。

Oh, the project settings are setup to use x64 architecture.

推荐答案

词典&LT的硬限制; TKEY的,TValue> 是由于私人领域输入,它的类型是输入[] 。在这种情况下输入是一个结构:

The hard limit for Dictionary<TKey, TValue> is due to the private field entries, which has the type Entry[]. In this case Entry is a struct:

private struct Entry {
  public int hashCode;
  public int next;
  public TKey key;
  public TValue value;
}

输入的最小尺寸为4×4,这可能发生,如果 TKEY的 TValue 均为4字节类型(指的是阵列对准尺寸,即通过的命令行的sizeof 字节code指令)。这导致限制int.MaxValue /(2 * 16)由于条目array大小限制在.NET 。如果您使用 TKEY的 TValue 键入这是大于4个字节,最大字典大小将相应减少。

The minimum size of Entry is 4*4, which could happen if TKey and TValue are both 4-byte types (referring to the array alignment size, i.e. the value returned by the CLI sizeof bytecode instruction). This results in a limit of int.MaxValue / (2*16) entries due to the array size limitation in .NET. If you use a TKey or TValue type which is larger than 4 bytes, the maximum dictionary size would decrease accordingly.

有关大型辞书像你所提出的建议,一个

 
精彩推荐
图片推荐