字符串驻留

字符串的驻留(String Interning)

How to: Optimize the memory usage with strings

 

 

 

From MSDN:

The common language runtime conserves string storage by maintaining a table, called the intern pool, that contains a single reference to each unique literal string declared or created programmatically in your program. Consequently, an instance of a literal string with a particular value only exists once in the system.

For example, if you assign the same literal string to several variables, the runtime retrieves the same reference to the literal string from the intern pool and assigns it to each variable.

The Intern method uses the intern pool to search for a string equal to the value of str. If such a string exists, its reference in the intern pool is returned. If the string does not exist, a reference to str is added to the intern pool, then that reference is returned.

 

Performance Considerations
If you are trying to reduce the total amount of memory your application allocates, keep in mind that interning a string has two unwanted side effects. First, the memory allocated for interned String objects is not likely be released until the common language runtime (CLR) terminates. The reason is that the CLR's reference to the interned String object can persist after your application, or even your application domain, terminates. Second, to intern a string, you must first create the string. The memory used by the String object must still be allocated, even though the memory will eventually be garbage collected.

The .NET Framework version 2.0 introduces the CompilationRelaxations.NoStringInterning enumeration member. The NoStringInterning member marks an assembly as not requiring string-literal interning. You can apply NoStringInterning to an assembly using the CompilationRelaxationsAttribute attribute. When you use the Native Image Generator (Ngen.exe) to install that assembly to the native image cache on the local computer, string interning is not used.

 

 

 class Program
    {

        static void Main(string[] args)
        {
            string str1 = "ABCD1234";
            string str2 = "ABCD1234";
            string str3 = "ABCD";
            string str4 = "1234";
            string str5 = "ABCD" + "1234";
            string str6 = "ABCD" + str4;
            string str7 = str3 + str4;
            Console.WriteLine("string str1 = /"ABCD1234/";");
            Console.WriteLine("string str2 = /"ABCD1234/";");
            Console.WriteLine("string str3 = /"ABCD/";");
            Console.WriteLine("string str4 = /"1234/";");
            Console.WriteLine("string str5 = /"ABCD/" + /"1234/";");
            Console.WriteLine("string str6 = /"ABCD/" + str4;");
            Console.WriteLine("string str7 = str3 + str4;");
            Console.WriteLine("/nobject.ReferenceEquals(str1, str2) = {0}", object.ReferenceEquals(str1, str2));
            Console.WriteLine("object.ReferenceEquals(str1,  /"ABCD1234/") = {0}", object.ReferenceEquals(str1, "ABCD1234"));
            Console.WriteLine("/nobject.ReferenceEquals(str1, str5) = {0}", object.ReferenceEquals(str1, str5));
            Console.WriteLine("object.ReferenceEquals(str1, str6) = {0}", object.ReferenceEquals(str1, str6));
            Console.WriteLine("object.ReferenceEquals(str1, str7) = {0}", object.ReferenceEquals(str1, str7));
            Console.WriteLine("/nobject.ReferenceEquals(str1, string.Intern(str6)) = {0}", object.ReferenceEquals(str1, string.Intern(str6)));
            Console.WriteLine("object.ReferenceEquals(str1, string.Intern(str7)) = {0}", object.ReferenceEquals(str1, string.Intern(str7)));

            string a = "aaaaa/0bbbbb";
            Console.WriteLine(a);
            string b = a;
            Console.WriteLine(b);
        }
    }

 

 

.NET 中,字符串并不是用“/0”来判断结尾

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值