C# 字符串拘留池机制(string intern pool),禁止使用?????

来源:互联网 发布:淘宝网童装保暖 编辑:程序博客网 时间:2024/04/29 16:54

转载地址:http://blog.csdn.net/susan19890313/article/details/7365820

一、字符串拘留池的引入

     在一个程序中,如果将同一个字符串赋值给不同的字符串引用,那么系统会为多次分配内存空间,然而这些内存中存储的是同一个字符串(见下图)。这不仅浪费了宝贵的内存,还极大程度的降低了系统的性能。为了改善程序的性能,.net提出了字符串拘留池机制来解决这一问题。

    string a="abcde";  string b="abcde";

 

二、字符串拘留池的原理

     一旦使用了字符串拘留池,当CLR启动的时候,会在内部创建一个容器,容器的键是字符串内容,值时字符串在托管堆上的引用。当一个新的字符串对象需要分配时,CLR首先检测容器中是否包含了该字符串对象,如果已经包含,就直接返回已经存在的字符串对象的引用;如果不存在,则新分配一个字符串对象,并把它添加到内部容器中同时返回该字符串对象的引用。但是当用new关键字显示的申明分配一个字符串对象时,字符串拘留池机制不会再起作用。

  示例:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{      class Program    {        public static void Main()        {            string a = "abcde";            string b = "abcde";            Console.WriteLine(Object.ReferenceEquals(a, b));            char[] c ={'a','b','c','d','e'};            string cc = new string(c);            Console.WriteLine(Object.ReferenceEquals(a, c));        }    }}

对于CLR保存的字符串拘留池的容器,可以通过System.String类型的两个静态方法进行访问:

        public  static  String  Intern (String s);

                  返回字符串s在字符串拘留池中对应的引用,如果该字符串不在字符串拘留池中,那么会新分配一个字符串对象并添加到字符串拘留池中同时返回该字符串的引用。

       public  static  String  IsInterned (String s);

               功能与上个方法相似。只是当字符串s不在字符串拘留池中时,不会分配新的对象,并返回null。

三、缺点

     当在一个程序中刚好较少地或根本没有使用到重复的字符串时,那么字符串拘留池机制就会带来负面效应。因为CLR额外的保留了一个存储字符串池的容器,并且在每次进行字符串赋值时都要额外的检查这个容器。考虑到这点,.NET提供了字符串拘留池的开关接口,如果程序集标记了一个System.Runtime.CompilerServices.CompilationRelaxationsAtrribute特性,并指定了一个System.Runtime.CompilerServices.CompilationRelaxations.NoStringInterning标志,那么CLR将不会再使用字符串拘留池机制。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.CompilerServices;[assembly: CompilationRelaxationsAttribute(CompilationRelaxations.NoStringInterning)]namespace ConsoleApplication1{      class Program    {        public static void Main()        {            string a = "abcde";            string b = "abcde";            Console.WriteLine(Object.ReferenceEquals(a, b));            char[] c ={'a','b','c','d','e'};            string cc = new string(c);            Console.WriteLine(Object.ReferenceEquals(a, c));        }    }}

结果居然和使用了字符串拘留池机制一样,??????????
0 0
原创粉丝点击