C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Result: A Dictionary with zero capacity only used 52 bytes. A Dictionary with 1000 capacity required 22084 bytes.
So: With 1000 capacity, Dictionary used 22 bytes per unit of capacity. With 10000, it required about 18 bytes each.
ByteC# program that tests Dictionary memory
using System;
using System.Collections.Generic;
class Program
{
static Dictionary<string, int> _d;
static void Main()
{
long m1 = GC.GetTotalMemory(true);
{
_d = new Dictionary<string, int>();
}
long m2 = GC.GetTotalMemory(true);
{
_d = new Dictionary<string, int>(1000);
}
long m3 = GC.GetTotalMemory(true);
{
_d = new Dictionary<string, int>(10000);
}
long m4 = GC.GetTotalMemory(true);
// Display results.
Console.WriteLine("Capacity: {0}, Memory: {1}", 0, m2 - m1);
Console.WriteLine("Capacity: {0}, Memory: {1}", 1000, m3 - m2);
Console.WriteLine("Capacity: {0}, Memory: {1}", 10000, m4 - m3);
}
}
Output
Capacity: 0, Memory: 52
Capacity: 1000, Memory: 22084
Capacity: 10000, Memory: 180004
Therefore: The actual capacity is not 1000 or 10000 in these examples—it is a value close to those specified.
Prime NumberWarning: If you choose to use a capacity optimization, an overly large capacity could cause memory problems.