zoukankan      html  css  js  c++  java
  • 关于.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来说不是强制性的。

    以下是测试代码:

     1 [assembly: CompilationRelaxationsAttribute(CompilationRelaxations.NoStringInterning)]
     2 namespace StringInterningTest
     3 {
     4     class Program
     5     {
     6         static void Main(string[] args)
     7         {
     8             string s1 = "unicode";
     9             string s2 = new string(new char[] { 'u', 'n', 'i', 'c', 'o', 'd', 'e' });
    10             string s3 = "unicode";
    11 
    12             bool b1 = (object)s1 == (object)s2;
    13             bool b2 = (object)s1 == (object)s3;
    14         }
    15     }
    16 }

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

     

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

  • 相关阅读:
    07-图4 哈利·波特的考试 (25分)
    Windows环境下清除SVN文件
    查看SQL SERVER 2008R2 表大小
    Oauth支持的5类 grant_type 及说明
    SignalR的性能监测
    Loadrunner11安装
    Azure ServiceBus 通信失败问题
    sql server text类型 存储问题
    System.BadImageFormatException
    InputStream只能读取一次的解决办法 C# byte[] 和Stream转换
  • 原文地址:https://www.cnblogs.com/yitouniu/p/5057193.html
Copyright © 2011-2022 走看看