String interning in .Net Framework - What are the benefits and when to use interning

后端 未结 5 826
独厮守ぢ
独厮守ぢ 2020-11-27 05:52

I want to know the process and internals of string interning specific to .Net framework. Would also like to know the benefits of using interning and the sce

5条回答
  •  旧巷少年郎
    2020-11-27 06:48

    Internalization of strings affects memory consumption.

    For example if you read strings and keep them it in a list for caching; and the exact same string occurs 10 times, the string is actually stored only once in memory if string.Intern is used. If not, the string is stored 10 times.

    In the example below, the string.Intern variant consumes about 44 MB and the without-version (uncommented) consumes 1195 MB.

    static void Main(string[] args)
    {
        var list = new List();
    
        for (int i = 0; i < 5 * 1000 * 1000; i++)
        {
            var s = ReadFromDb();
            list.Add(string.Intern(s));
            //list.Add(s);
        }
    
        Console.WriteLine(Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024 + " MB");
    }
    
    private static string ReadFromDb()
    {
        return "abcdefghijklmnopqrstuvyxz0123456789abcdefghijklmnopqrstuvyxz0123456789abcdefghijklmnopqrstuvyxz0123456789" + 1;
    }
    

    Internalization also improves performance for equals-compare. The example below the intern version takes about 1 time units while the non-intern takes 7 time units.

    static void Main(string[] args)
    {
        var a = string.Intern(ReadFromDb());
        var b = string.Intern(ReadFromDb());
        //var a = ReadFromDb();
        //var b = ReadFromDb();
    
        int equals = 0;
        var stopwatch = Stopwatch.StartNew();
        for (int i = 0; i < 250 * 1000 * 1000; i++)
        {
            if (a == b) equals++;
        }
        stopwatch.Stop();
    
        Console.WriteLine(stopwatch.Elapsed + ", equals: " + equals);
    }
    

提交回复
热议问题