I use a ConcurrentDictionary<String, String>
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.
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:
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<String, String> dict = new ConcurrentDictionary<string, string>(Environment.ProcessorCount, capacity);
Console.WriteLine("{0} buckets", GetBucketCount(dict));
for (int index = 0; index < capacity; index++)
dict.AddOrUpdate(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), (key, value) => value);
Console.WriteLine("{0} buckets", GetBucketCount(dict));
Console.ReadLine();
}
private static int GetBucketCount(ConcurrentDictionary<string, string> 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;
}
}
}
Displays:
500 buckets at the beginning
1003 buckets at the end
I expected 500 buckets at the end
. Do you know a way to avoid allocating extra memory since I know the number of items at the beginning?