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