title: .Net Dictionary<int,int> out of memory exception at around 6,000,000 entries
body: <p>I am using a <code>Dictionary&lt;Int,Int&gt;</code> 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.</p>
  
  <p>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?</p>
  
  <p>For reference, here is the code that loops through the pixels in a bitmap and saves the frequency in the <code>Dictionary&lt;int,int&gt;</code>:</p>
  
  <pre><code>Bitmap b; // = something...
  Dictionary&lt;int, int&gt; count = new Dictionary&lt;int, int&gt;();
  System.Drawing.Color color;
  
  for (int i = 0; i &lt; b.Width; i++)
  {
      for (int j = 0; j &lt; 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;
      }
  }
  </code></pre>
  
  <p>Edit: In case you were wondering what image has that many different colors in it: <a href="http://allrgb.com/images/mandelbrot.png">http://allrgb.com/images/mandelbrot.png</a></p>
  
  <p><strong>Edit: I also should mention that this is running inside an asp.net web application using .Net 4.0. So there may be additional memory restrictions.</strong></p>
  
  <p><em><strong>Edit: I just ran the same code inside a console application and had no problems. The problem only happens in ASP.Net.</em></strong></p>