c# string interning

Posted by CodingThunder on Stack Overflow See other posts from Stack Overflow or by CodingThunder
Published on 2010-03-24T09:43:16Z Indexed on 2010/03/24 9:53 UTC
Read the original article Hit count: 273

Filed under:

I am trying to understand string interning and why is doesn't seem to work in my example. The point of the example is to show Example 1 uses less (a lot less memory) as it should only have 10 strings in memory. However, in the code below both example use roughly the same amount of memory (virtual size and working set).

Please advice why example 1 isn't using a lot less memory? Thanks

Example 1:

        IList<string> list = new List<string>(10000);

        for (int i = 0; i < 10000; i++)
        {
            for (int k = 0; k < 10; k++)
            {
                list.Add(string.Intern(k.ToString()));
            }

        }

        Console.WriteLine("intern Done");
        Console.ReadLine();

Example 2:

        IList<string> list = new List<string>(10000);

        for (int i = 0; i < 10000; i++)
        {
            for (int k = 0; k < 10; k++)
            {
                list.Add(k.ToString());
            }

        }

        Console.WriteLine("intern Done");
        Console.ReadLine();

© Stack Overflow or respective owner

Related posts about c#