title: How to limit the memory used by ConcurrentDictionary
body: <p>I use a <code>ConcurrentDictionary&lt;String, String&gt;</code> to store a big amount of data (4 500 000 entries), and I dont want to use extra memory, so I fixed the capacity at the beginning. But the dictionary grow automatically before reaching the specified capacity.</p>
  
  <p>I wrote a little portion of code to show the matter with only 500 items, I do reflection on the private buckets array because I didn't find a public property giving the real capacity:</p>
  
  <pre><code>using System;
  using System.Collections.Concurrent;
  using System.Reflection;
  
  namespace MemoryUsage
  {
      class Program
      {
          static void Main(string[] args)
          {
              CapacityTest();
          }
          private static void CapacityTest()
          {
              int capacity = 500;
              ConcurrentDictionary&lt;String, String&gt; dict = new ConcurrentDictionary&lt;string, string&gt;(Environment.ProcessorCount, capacity);
              Console.WriteLine("{0} buckets", GetBucketCount(dict));
              for (int index = 0; index &lt; capacity; index++)
                  dict.AddOrUpdate(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), (key, value) =&gt; value);
              Console.WriteLine("{0} buckets", GetBucketCount(dict));
              Console.ReadLine();
          }
          private static int GetBucketCount(ConcurrentDictionary&lt;string, string&gt; dict)
          {
              object tables = dict.GetType().GetField("m_tables", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(dict); // "_tables" with .NET Core, "m_tables" with .NET Framework
              object buckets = tables.GetType().GetField("m_buckets", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(tables); // "_buckets" with .NET Core, "m_buckets" with .NET Framework
              return ((Array)buckets).Length;
          }
      }
  }
  </code></pre>
  
  <p>Displays:</p>
  
  <pre><code>500 buckets at the beginning
  1003 buckets at the end
  </code></pre>
  
  <p>I expected <code>500 buckets at the end</code>. Do you know a way to avoid allocating extra memory since I know the number of items at the beginning?</p>