About. NET string-resident problem

Source: Internet
Author: User
Tags terminates

by default, the CLR saves string constants in a string pool.
. NET 2.0 introduces the Compilationrelaxations.nostringinterning enumeration members. This enumeration Compilationrelaxationsattribute property is used in conjunction with the ability to mark an assembly as "do not require string literal dwell" (marks an assembly as not requiring string-literal interning).
from the literal meaning understanding, the application [Assembly:compilationrelaxationsattribute (compilationrelaxations.nostringinterning)] This attribute seems to have no mandatory meaning. That is, the CLR's JIT can choose to ignore this property.

I tested it on. NET 4.0 and 4.6. However, the result is the same. The string will reside in memory, regardless of whether it is added with [Assembly:compilationrelaxationsattribute (compilationrelaxations.nostringinterning)] property.

In the description of the String.internal method in MSDN, there is a phrase "the reason is, the CLR reference to the interned String object can persist after Your application, or even your application domain, terminates. ". According to this sentence, even if the application ends, even if the application domain terminates, the CLR's resident string reference still exists.

so there are two possible:
1,CLR's JIT ignores [Assembly:compilationrelaxationsattribute (compilationrelaxations.nostringinterning)] this property.
2, the application terminates, but the CLR's resident string reference still exists.

However, I changed the values of several strings, and the result remains the same.
so the second possibility is obviously not tenable (at least not at the time of my testing).
Therefore, it should be that the CLR's JIT ignores [Assembly:compilationrelaxationsattribute (compilationrelaxations.nostringinterning)] this property.
applying compilationrelaxations.nostringinterning is not mandatory for the CLR.

here is the test code:

1 [Assembly:compilationrelaxationsattribute (compilationrelaxations.nostringinterning)]2 namespacestringinterningtest3 {4     class Program5     {6         Static voidMain (string[] args)7         {8             stringS1 ="Unicode";9             stringS2 =New string(New Char[] {'u','N','I','C','o','D','e' });Ten             stringS3 ="Unicode"; One  A             BOOLB1 = (Object) S1 = = (Object) S2; -             BOOLB2 = (Object) S1 = = (Object) S3; -         } the     } -}

Test results:
The value of N1 is always "Unicode".
The value of N2 is always null.

If there is an incorrect place, I hope you correct me!

About. NET string-resident problem

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.