Reading Notes-C # string resident Technology

Source: Internet
Author: User
Tags key string

I have learned a long time ago that the string resident technology can optimize the memory usage, but I am not very impressed. Today I have spent some time studying this issue and deepened my understanding of it. Here, we will sum up to facilitate future study and search, and hope to help our friends. Thank you!

Msdn concept: the Common Language Runtime stores strings by maintaining a table, which is called a detention pool and contains a reference to each unique string that is declared or created programmatically in the program. Therefore, there is only one instance of a string with a specific value in the system.

The above concepts are hard to understand. Let's start with the basics:

I. As we all know, the string in C # Is a reference type, and the string object is stored on the stack rather than on the stack. Therefore, when a string variable is assigned to another string, the two references of the same string in the memory are obtained. But why do we modify the value of a string while the value of another string is not affected? When we assign a string value to another string, A New String object will be created, that is, pointing to two completely different address spaces in the heap respectively. The following is a simple example:

Static void main (string [] ARGs)
{
String S1 = "Charles ";
String S2 = S1; // note that a new object is created.
Console. writeline ("S1 =" + S1 );
Console. writeline ("S2 =" + S2 );
S1 = "Charles Chen change ";
Console. writeline ("S1 =" + S1 );
Console. writeline ("S2 =" + S2 );
}

 

The result is:

S1 = Charles
S2 = Charles
S1 = Charles Chen change
S2 = Charles

That is to say, changing the S1 value does not change the S2 value, which is actually in conflict with the reference type. In fact, when S1 = "Charles", a string object is allocated to the stack. When S2 = S1, the reference also points to this reference. However, when the S1 value changes, instead of replacing the original value, it is actually allocating a new memory space on the heap. The S2 value still points to the original object, so the tower value has not changed.

2. Let's look at the following code block:

String str1 = "charleschen ";
String str2 = "charleschen ";

When we call system. object. when equals (str1, str2), the return value is true. According to the above understanding, it should be returned as false, and str1 and str2 should point to different memory spaces. How can I return true? Here we introduce the "string resident technology ".

Actually, the CLR uses the string resident technology. For string str1 = "charleschen"; string str2 = "charleschen ";

When CLR is initialized, an internal hash table is created, with the key string and the value pointing to the reference of the string in the managed heap. At the beginning, the hash is empty. During JIT compiler compilation, every text string constant ("charleschen") will be searched in the hash list. First, "charleschen" will be searched ", the compiler constructs a new object reference pointing to "charleschen" in the managed heap because it cannot be found, then, add the "ABC" string and the reference for executing the object to the hash list.

When string str2 = "charleschen", the compiler does not perform any operations to allocate memory space because the "charleschen" string has been added to the hash. First, the compiler searches for "charleschen" in the internal hash, and finds the reference to the previously created String object, and str2 points to the reference found here. Therefore, str1 and str2 point to the reference of the same address in the memory. Therefore, system. ojbect. Equals (str1, str2) returns true.

 

Related information:

Http://www.cnblogs.com/coderlee/archive/2008/01/02/1023316.html a string resident technology demo to deepen its understanding.

 

If you have any questions, please correct them. Thank you!

Charles Chen

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.