Discuss about. NET string detention pool

Source: Internet
Author: User

 

Discuss about. NET string detention pool

 

XiaoMing 《Interview experience (2)There are a lot of replies in this article. Here we talk about one interview question: How many times does string a = "a" + "B" actually allocate memory.

This involves the string interning pool in CLR.

Netizens Ivony and yundao tianxiao quoted materials that the detention pool is within the process. Therefore, it is possible that the Code defining the String constant below will not cause two memory allocations-because another process may have created the string objects "a" or "AB" in the detention pool.

String s1 = "";
String s2 = "";
String s3 = "a" + "B ";
Console. WriteLine (s1 );
Console. WriteLine (s2 );
Console. WriteLine (s3 );

I was a bit skeptical about this conclusion, so I went to Google and MSDN and found that the information about the string detention pool was messy. Finally, I decided to program myself as an experiment.

 

The String class has an IsInterned () method used to detect whether a String is in the detention pool, and the other Intern () method used to add a String to the detention pool.

To this end, I wrote the following test code:

 

 

Class Program
{
// Static string outerstr = "j ";

Static void Main (string [] args)
{

String str = new string ('J', 1); // dynamically constructed string, not stored in the detention pool

If (string. IsInterned (str) = null)
Console. WriteLine (str + "is not interned"); // not in the detention pool
Else
Console. WriteLine (str + "is interned"); // In the detention pool

Console. ReadKey ();
String s = string. Intern (str); // Add to detention pool forcibly

// Detect again
If (string. IsInterned (str) = null)
Console. WriteLine (str + "is not interned ");
Else
Console. WriteLine (str + "is interned ");
Console. ReadKey ();


}

 

The above code runs as follows:

 

J is not interned
J is interned

No matter how many times you run the program or whether you run multiple instances of the program at the same time, the results are consistent and the above results are the results. This indicates that after the process ends, the string constant object in the string detention pool related to the Assembly loaded by this process is cleared.

Now, the comment on the outerstr variable is canceled and the result is changed:

J is interned
J is interned

This indicates that the constant "j" in the Assembly is added to the string detention pool during loading, so the above results are available.

Another question is, can objects in the string detention pool be shared across different process boundaries?

Compile another test program:

 

 

Class Program
{

Static void Main (string [] args)
{

String str = new string ('J', 1 );
Console. WriteLine (string. IsInterned (str) = null );
Console. ReadKey ();
}
}

No matter whether the previous test program is running or not, the above Code always outputs true, indicating that the string "j" is not in the detention pool, this process cannot obtain the "j" string appended to the string detention pool by another process.

 

Can the following conclusions be drawn from these experiments?

When the process ends, the String constant object defined in the Assembly loaded by the process will be removed from the string detention pool by CLR.

Therefore, the string constants in the CLR string detention pool are "processes and assembly-related.

The String constant object defined by the application is added to the string detention pool when the application domain loads the application.

So,String objects in the string detention Pool cannot be shared across processes. Otherwise, how do we explain the results of code execution?

Since multiple application domains can be created in the same process, I have not compiled code to test whether the string objects in the string detention pool can be shared in multiple application domains of the same process. This issue remains to be further explored.

Can anyone tell me this question?

Supplement:

My test environment is Windows 7 + VS2010 RC.

 

 

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.