With this following interned List, why wouldn’t my List Memory Consumption is still quite high at 8MB?
I can see all the strings in the list have the same address.
Is this just how C# List works ?
int maxCount = 1_000_000;
var list1 = new List<string>(maxCount);
string val = "input1.country == \"india\" AND input1.loyaltyFactor <= 2 AND input1.totalPurchasesToDate >= 5000 AND input2.totalOrders > 2 AND input3.noOfVisitsPerMonth > 2";
for (var ix = 0; ix < maxCount; ix++)
{
list1.Add(string.Intern(val));
}
var totalMemory = GC.GetTotalMemory(false);
Console.WriteLine(totalMemory/1024 + "KB");
Console.ReadKey();
Console.Write(list1[0]);
>Solution :
Your list has 1,000,000 entries. Even if all the entries are the same, the list itself still needs space. And 8MB is exactly 1,000,000 times 8, as each entry in a list of a reference type such as string uses 8 bytes on a 64 bit CPU.
