净字典< INT,INT>内存溢出异常约为600万项 [英] .Net Dictionary<int,int> out of memory exception at around 6,000,000 entries

查看:204
本文介绍了净字典< INT,INT>内存溢出异常约为600万项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是词典< INT,INT> 来存储颜色的频率的图像,其中关键是色彩(作为int),和的值是颜色已经在图像中被发现的次数。

I am using a Dictionary<Int,Int> to store the frequency of colors in an image, where the key is the the color (as an int), and the value is the number of times the color has been found in the image.

当我处理更大/更丰富多彩的图像,这本词典变得非常大。我得到一个内存溢出异常的就在600万项。这在32位模式下运行时,预期的产能?如果是这样,有什么我可以做些什么?什么可能是跟踪这些数据的一些替代方法的内存,不会跑出来了?

When I process larger / more colorful images, this dictionary grows very large. I get an out of memory exception at just around 6,000,000 entries. Is this the expected capacity when running in 32-bit mode? If so, is there anything I can do about it? And what might be some alternative methods of keeping track of this data that won't run out of memory?

有关引用,这里是code来遍历位图的像素并保存在词典&LT的频率; INT,INT&GT;

For reference, here is the code that loops through the pixels in a bitmap and saves the frequency in the Dictionary<int,int>:

Bitmap b; // = something...
Dictionary<int, int> count = new Dictionary<int, int>();
System.Drawing.Color color;

for (int i = 0; i < b.Width; i++)
{
    for (int j = 0; j < b.Height; j++)
    {
        color = b.GetPixel(i, j);
        int colorString = color.ToArgb();
        if (!count.Keys.Contains(color.ToArgb()))
        {
            count.Add(colorString, 0);                
        }
        count[colorString] = count[colorString] + 1;
    }
}

编辑:如果你想知道什么样的形象有那么多不同的颜色在里面: http://allrgb.com/images/ mandelbrot.png

编辑:我还要提到的是,这是使用.NET 4.0中一个asp.net web应用程序中运行。所以可能会有额外的内存限制。

编辑:我只是跑一个控制台应用程序中相同的code,也没有问题。该问题只发生在ASP.Net。

I just ran the same code inside a console application and had no problems. The problem only happens in ASP.Net.

推荐答案

更新:由于OP的样本图像,似乎项目的最大数量将超过1600万,而<一href="http://stackoverflow.com/questions/20643222/net-dictionaryint-int-out-of-memory-exception-at-around-6-000-000-entries/#comment30902049_20643387">apparently甚至是太多实例字典时分配。我看到三个选项的位置:

Update: Given the OP's sample image, it seems that the maximum number of items would be over 16 million, and apparently even that is too much to allocate when instantiating the dictionary. I see three options here:

  • 调整图像到从管理的大小和工作。
  • 尝试转换为一个配色方案用较少的颜色可能性。
  • 去固定大小的数组作为其他建议。

previous答案:的问题是,你不为你的字典分配足够的空间。在某些时候,当它正在扩大,你刚用完的内存扩展,但不一定是新的字典。

Previous answer: the problem is that you don't allocate enough space for your dictionary. At some point, when it is expanding, you just run out of memory for the expansion, but not necessarily for the new dictionary.

例:这个code内存用完,在近24万条(在我的机器,在32位模式下运行):

Example: this code runs out of memory at nearly 24 million entries (in my machine, running in 32-bit mode):

Dictionary<int, int> count = new Dictionary<int, int>();
for (int i = 0; ; i++)
     count.Add(i, i);

,因为它是目前使用空间的条目已经存在,并试图拨出最后一个扩展的的空间又如此多万以上,这实在是太多了。

because with the last expansion it is currently using space for the entries already there, and tries to allocate new space for another so many million more, and that is too much.

现在,如果我们最初分配的,也就是说,有40万个条目的空间,它运行没有问题:

Now, if we initially allocate space for, say, 40 million entries, it runs without problem:

Dictionary<int, int> count = new Dictionary<int, int>(40000000);

因此​​,试图说明有多少项目会出现在创建字典时。

So try to indicate how many entries there will be when creating the dictionary.

MSDN

一个字典的容量是可以添加到词典调整是必要的前元件的数目。作为元素添加到一个字典,所要求的重新分配内部阵列的能力自动增加。   如果所述集合的大小可被估计,指定初始容量无需执行大量大小调整操作而将元素添加到词典

The capacity of a Dictionary is the number of elements that can be added to the Dictionary before resizing is necessary. As elements are added to a Dictionary, the capacity is automatically increased as required by reallocating the internal array. If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Dictionary.

这篇关于净字典&LT; INT,INT&GT;内存溢出异常约为600万项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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