Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Update:</strong> Given the OP's sample image, it seems that the maximum number of items would be over 16 million, and <a href="https://stackoverflow.com/questions/20643222/net-dictionaryint-int-out-of-memory-exception-at-around-6-000-000-entries/#comment30902049_20643387">apparently</a> even that is too much to allocate when instantiating the dictionary. I see three options here:</p> <ul> <li>Resize the image down to a manageable size and work from that.</li> <li>Try to convert to a color scheme with fewer color possibilities.</li> <li>Go for an array of fixed size as others have suggested.</li> </ul> <p><strong>Previous answer:</strong> 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.</p> <p>Example: this code runs out of memory at nearly 24 million entries (in my machine, running in 32-bit mode):</p> <pre><code>Dictionary&lt;int, int&gt; count = new Dictionary&lt;int, int&gt;(); for (int i = 0; ; i++) count.Add(i, i); </code></pre> <p>because with the last expansion it is currently using space for the entries already there, and tries to allocate <em>new</em> space for another so many million more, and that is too much.</p> <p>Now, if we initially allocate space for, say, 40 million entries, it runs without problem:</p> <pre><code>Dictionary&lt;int, int&gt; count = new Dictionary&lt;int, int&gt;(40000000); </code></pre> <p>So try to indicate how many entries there will be when creating the dictionary.</p> <p>From <a href="http://msdn.microsoft.com/en-us/library/tk84bxf4%28v=vs.110%29.aspx" rel="nofollow noreferrer">MSDN</a>:</p> <blockquote> <p>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. <strong>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</strong>.</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload