A few misconceptions about string

Source: Internet
Author: User
Tags constant terminates hosting

Yesterday debugging a program found that memory was never released, and finally found that it was the wrong use of string, which prompted me to take a closer look at the string type today, I don't know, a study found that I used to have a lot of knowledge of string that was wrong, feeling Feel this kind of mistaken understanding also more typical, so write down this article and we discuss together.

1. String type variable append, or modified new string object is resident (interned).

As the following code

string S1 = "ABCD";
String s2 = S1 + "E";

I used to take it for granted that S2 was resident, but it was not, in string. The Isinterned method detects that the S2 is non resident. Later, the study found that only constant strings would reside by default, and other string variables, even if they were constructed with new string, would not reside by default unless using String. Intern forcibly resident. I'll talk about the impact of hosting on memory, and I think it's in memory that Microsoft doesn't allow all the strings to reside.

2. When a String variable is no longer referenced, the CLR automatically frees its memory through the GC.

string S1 = "ABCD";
S1 = null;

The code above, I take it for granted that S1 = null has ceased to refer to the "ABCD" string, and if no other reference points to the string, the GC releases the "ABCD" block of memory. The actual result is negative. Because S1 is given a constant, the string "ABCD" is resident, and the resident string cannot be automatically released until the process is finished. To make it worse, a large number of string variables in the program that I debugged yesterday were in string. Intern forced hosting, which caused me to release all of the managed objects and still not be able to release that part of about 30 m of memory.

Unfortunately, in the Microsoft MSDN Chinese version of String.intern's help message, which misses the performance test (performance consideration), I estimate that most Chinese programmers, including me, are too lazy to read English if they have Chinese help. It is a pity that Microsoft Chinese help do not know why the most important part of the leak. Below is a section on performance consideration in English help.

Performance considerations

If you are trying to reduce the total amount of memory your application-allocates, keep in mind that interning a string ha s two unwanted side effects. The memory allocated for interned String objects are not likely to be released until the common language runtime (CLR) Terminates. The reason is this CLR ' reference to the interned String object can persist after your application, or even your Ication domain, terminates. Second, to intern a string, and you must the. The memory used by the String object must still is allocated, even though the memory to eventually the garbage.

The. NET Framework version 2.0 introduces the compilationrelaxations.::. Nostringinterning enumeration member. The nostringinterning member marks a assembly as not requiring string-literal interning. can apply nostringinterning to a assembly using the Compilationrelaxationsattribute attribute. Also the Native Image Generator (Ngen.exe) to compile a assembly in advance by run time, strings are no in Terned across modules.

Read the English help to know that after the intern string is unable to release.

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.