• string


     公共语言运行时通过维护名为拘留池的表来节省字符串存储,该表包含对程序中以编程方式声明或创建的每个唯一文本字符串的单个引用。 因此,系统中仅存在一个具有特定值的文本字符串的实例。

    string.Intern 是终生制的,一旦加入只要程序不重启,就会一直存在。

    // Sample for String.Intern(String)
    using System;
    using System.Text;
    
    class Sample
    {
        public static void Main()
        {
            string s1 = "MyTest";
            string s2 = new StringBuilder().Append("My").Append("Test").ToString();
            string s3 = String.Intern(s2);
            Console.WriteLine($"Is s2 the same reference as s1?: {(Object)s2 == (Object)s1}");
            Console.WriteLine($"Is s3 the same reference as s1?: {(Object)s3 == (Object)s1}");
        }
    }
    /*
    This example produces the following results:
    Is s2 the same reference as s1?: False
    Is s3 the same reference as s1?: True
    */

    Microsoft.Toolkit.HighPerformance 包中StringPool类解决重复字符串实例过多,导致浪费内存的情况。优点是可以释放

  • 相关阅读:
    泛型的内部原理:类型擦除以及类型擦除带来的问题
    Redis的那些最常见面试问题
    线程池全面解析
    对线程调度中Thread.sleep(0)的深入理解
    集群环境下Redis分布式锁
    3.8
    3.7
    3.6任务
    3.5任务
    3.4
  • 原文地址:https://www.cnblogs.com/yetsen/p/15581697.html
Copyright © 2020-2023  润新知