Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

List of Interned String Reduce Memory Usage

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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

dotMemory

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading