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”来判断结尾