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)); } }}
结果居然和使用了字符串拘留池机制一样,??????????
- C# 字符串拘留池机制(string intern pool),禁止使用?????
- C# 字符串拘留池机制(string intern pool),禁止使用?????
- 字符串拘留池(string interning pool)
- 囧,对C#驻留池(Intern Pool)机制的误解
- .Net高级技术——字符串拘留池(Intern)
- string拘留池
- String.intern()-字符串常量池
- String.intern()-字符串常量池
- string intern pool 及扩展
- java string pool(java 字符串池)
- Java String Pool (字符串池)
- a string intern pool,new string 存贮
- Java String intern() 和 String Pool
- String.intern()的使用
- Java字符串池(String Pool)深度解析
- 什么是string interning(字符串驻留)以及python中字符串的intern机制
- 常量池之字符串常量池String.intern()
- 深入理解java虚拟机(三):String.intern()-字符串常量池
- 每个程序员都应该知道的福利
- Android逗比自学webservice(二)tomcat的认识与搭建
- Oracle 取随机数
- 优化网站设计(四):对资源启用压缩
- const的重载讲解
- C# 字符串拘留池机制(string intern pool),禁止使用?????
- 第十一周项目1-函数版星号图(三) .
- [转]Echarts 后台交互
- BZOJ 3555 Ctsc2014 企鹅QQ Hash
- Unity3D制作闪关灯效果
- 无法定位程序输入点GetProcessImageFileNameW 于动态链接库PSAPI.DLL上 ORA-12546
- 富文本NSAttributedString与NSMutableAttributedString
- 血型统计
- 和支持向量机(SVM)相关的几个基本概念