Memory Allocation and resident pool of strings in C #

Source: Internet
Author: User

When I first started learning C #, I heard that CLR has a special memory management mechanism for the String class: Sometimes, two String class objects are explicitly declared, however, they point to the same instance. As follows:

String s1 = "Hello ";
String s2 = "Hello"; // the actual values of s2 and s1 are "Hello"
Bool same = (object) s1 = (object) s2; // check whether s1 and s2 reference the same object instance.
// Therefore, you cannot write bool same = s1 = s2;
// Because the = Operator is overloaded in the String class to compare the actual values contained in the String object
The same is assigned true. That is to say, s1 and s2 really reference the same String object. Of course, it should be noted that s1 and s2 are uniformly assigned to the same string "Hello", which is the reason for the above situation.

Now we come to the conclusion that when multiple string variables contain the actual values of the same string, CLR may not allocate memory for them repeatedly, instead, let them all point to the same string object instance. (This is because, in some cases, the actual value of the same string exists with multiple copies in the memory. Continue .)

We know that the String class has many special features, one of which is immutable ). This means that every time we operate on a String object (such as Trim and Replace), we do not actually modify the instance of this String object, instead, a New String object instance is returned as the result of the operation. Once an instance of the String object is generated, it will not be changed until it is dead!

Based on the features such as the String class, it is reasonable for CLR to direct the variables that represent the actual values of the same String to the same String instance. Because any modification to the String instance does not affect the instance status, it does not affect the actual values of all other strings that point to the instance reference. The CLR manages the memory allocation of the String class so that the memory usage can be optimized to avoid redundant data in the memory.

To implement this mechanism, CLR silently maintains a table called an Intern Pool. This table records all references to string instances declared using the literal in the code. This indicates that strings declared using the literal will enter the resident pool, while strings declared using other methods will not enter, so they will not automatically enjoy the benefits of CLR mechanisms to prevent string redundancy. This is the example I mentioned above: "In some cases, the actual value of the same string exists in the memory with multiple copies at the same time. See this example:

StringBuilder sb = new StringBuilder ();
Sb. Append ("He"). Append ("llo ");

String s1 = "Hello ";
String s2 = sb. ToString ();

Bool same = (object) s1 = (object) s2;
In this case, same is not true, because although s1 and s2 represent the same string, but because s2 is not declared literally, CLR is sb. when the return value of the ToString () method is allocated to memory, it does not go to the resident pool to check whether a string with the value of "Hello" already exists, therefore, s2 is naturally not directed to objects in the resident pool.

To enable programmers to force CLR checks to reside in the pool to avoid redundant String copies, the designer of the String class provides a class method named Intern. The following is an example of this method:


 
StringBuilder sb = new StringBuilder ();
Sb. Append ("He"). Append ("llo ");

String s1 = "Hello ";
String s2 = String. Intern (sb. ToString ());

Bool same = (object) s1 = (object) s2;
Okay, same is true again. The Intern method accepts a string as a parameter and checks whether the string represented by the parameter exists in the resident pool. If yes, a reference to the string in the resident pool is returned. Otherwise, a new string indicating the same value is added to the resident pool, and a reference to the string is returned. However, even if the Intern method finds strings with the same value in the resident pool, you cannot save a single string memory allocation operation, because the parameter string has been allocated with memory once. The advantage of using the Intern method is that if the Intern method finds a string with the same value in the resident pool, although there are two copies of the string in the memory (one is a parameter, one copy is in the resident pool), but with the passage of time, the copy referenced by the parameter will be reclaimed, so that there will be no redundancy in the memory of the string. When a method exists in your program, you can create and return a long string according to different context environments, and it often returns the same string during the program running, you may have to consider using the Intern Method to Improve the memory usage. However, it is worth noting that using the Intern method to make a string survive in the resident pool also has a side effect: even if no other reference has pointed to the string in the resident pool, this string may not be recycled. That is to say, even if the string in the resident pool is useless, it may not be destroyed until the CLR ends. This special behavior should also be taken into account when you use the Intern method.

 

Http://www.cnblogs.com/instance/archive/2011/05/24/2056091.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.