In. NET, for the same string. NET will point them to the same address, they are the same instance: NET is not updated when a string variable is changed due to the immutability of the string. NET actually creates a new string, and the variable address points to the newly created string address.
string " Hello " ; string " Hello " ; bool b = object.referenceequals (s1, S2); Console.WriteLine (b); // Enter True They are the same instance
The CLR silently maintains a table called the Intern pool. This table records all references to string instances in the code that are declared with literal literals . This means that strings declared with literals go into the pool, and other ways of declaring strings do not come in, and there is no automatic benefit of the CLR's mechanism to prevent string redundancy.
StringBuilder SB =NewStringBuilder (); Sb. Append ("He"). Append ("Llo"); stringS1 ="Hello"; stringS2 =sb. ToString (); BOOLb =object.referenceequals (S1, S2); Console.WriteLine (b); //output False They are not the same object
The reason for this output fasle is that although S1,S2 is the same string, because S2 is not declared by literal, the CLR allocates memory for the ToString () return value and does not go to the pool to check if a string with a value of "Hello" already exists. So nature does not let S2 point to objects residing in the pool.
If you want to force the CLR to check the resident pool to avoid redundant copies of the string, the designer of the string class provides a class method named Intern. Here is an example of this method:
StringBuilder SB =NewStringBuilder (); Sb. Append ("He"). Append ("Llo"); stringS1 ="Hello"; stringS2 = String.intern (sb.) ToString ());//force Check for string resident pool here BOOLb =object.referenceequals (S1, S2); Console.WriteLine (b); //output True because the discovery string already exists when checking for pool hosting
The
Intern method takes a string as an argument that checks for the existence of the string represented by the parameter in the resident pool. Returns a reference to the string residing in the pool, if present, otherwise adds a new string representing the same value to the resident pool and returns a reference to the string.
However, it is important to note that even if the Intern method finds a string of the same value in the resident pool, it does not allow you to save the operation of the string memory allocation once, because the string as a parameter has already been allocated memory once. The advantage of using the Intern method is that if the Intern method finds a string of the same value in the resident pool, there are two copies of the string in memory (one is a parameter and one is in the pool), but as time passes, the copy referenced by the argument is garbage collected. This means that there is no redundancy in the string memory.
When there is a method in your program that can create and return a long string based on a different context, and it will often return the same string during the program's run, you might want to consider using the Intern method to improve memory utilization.
However, it is also worth noting that using the Intern method to keep a string alive in the dwell pool also has a side effect: even if no other references exist to the strings residing in the pool, the string will not necessarily be garbage collected. That is, even if the string residing in the pool is useless, it may not be destroyed until the CLR is terminated. This particular behavior should also be taken into account when you use the Intern method.