关于.NET字符串驻留的问题

时间:2023-03-08 15:40:01
关于.NET字符串驻留的问题

默认情况下,CLR会把字符串常量保存在字符串池中。
.NET 2.0引入了CompilationRelaxations.NoStringInterning枚举成员。这个枚举CompilationRelaxationsAttribute属性配合使用可以把程序集标记为“不要求字符串字面量驻留”(marks an assembly as not requiring string-literal interning)。
从字面意思理解,应用[assembly: CompilationRelaxationsAttribute(CompilationRelaxations.NoStringInterning)]这个属性好像没有强制性的意思。也就是说,CLR的JIT可以选择忽略此属性。

我在.NET 4.0和4.6上都测试了。但是,结果是一样的。不管有没有加[assembly: CompilationRelaxationsAttribute(CompilationRelaxations.NoStringInterning)]这个属性,字符串都会驻留在内存中。

MSDN中的String.Internal方法的说明中有一句话“The reason is that the CLR's reference to the interned String object can persist after your application, or even your application domain, terminates.”。根据这句话理解,即使应用程序结束,甚至即使应用程序域终止了,CLR的驻留字符串引用依然存在。

所以有如下两个可能:
1,CLR的JIT忽略了[assembly: CompilationRelaxationsAttribute(CompilationRelaxations.NoStringInterning)]这个属性。
2,应用程序终止了,但是CLR的驻留字符串引用依然存在。

但是,我更换了好几个字符串的值,结果仍旧不变。
所以第二种可能性显然不成立(最起码在我测试的时候不成立)。
所以,应该是CLR的JIT忽略了[assembly: CompilationRelaxationsAttribute(CompilationRelaxations.NoStringInterning)]这个属性。
应用CompilationRelaxations.NoStringInterning对CLR来说不是强制性的。

以下是测试代码:

 [assembly: CompilationRelaxationsAttribute(CompilationRelaxations.NoStringInterning)]
namespace StringInterningTest
{
class Program
{
static void Main(string[] args)
{
string s1 = "unicode";
string s2 = new string(new char[] { 'u', 'n', 'i', 'c', 'o', 'd', 'e' });
string s3 = "unicode"; bool b1 = (object)s1 == (object)s2;
bool b2 = (object)s1 == (object)s3;
}
}
}

测试结果:
n1的值始终为"unicode"。
n2的值始终为null。

如有不正确的地方,希望各位指正!