String中substring方法内存泄漏问题

来源:互联网 发布:微信人工智能客服 编辑:程序博客网 时间:2024/06/09 20:21
众所周知,JDK中以前String类中的substring方法存在内存泄漏问题,之所以说是以前,是因为JDK1.7及以后的版本已经修复了,我看都说JDK1.6的版本也存在这个问题,但是我本机上安装的1.6看了看源码不存在内存泄漏问题啊,又看了1.7的源码,和我本机的1.6的一样,是不是我的1.6版版其实是1.7的?!唉,不管了,反正1.7版本肯定没有这个问题(1.5及更老版本肯定有)了,大家就放心的用吧。

之所以存在内存泄漏的问题,是因为原先的版本中,substring是这样实现的:

[java] view plaincopy
  1.   public String substring(int beginIndex, int endIndex) {  
  2. if (beginIndex < 0) {  
  3.     throw new StringIndexOutOfBoundsException(beginIndex);  
  4. }  
  5. if (endIndex > count) {  
  6.     throw new StringIndexOutOfBoundsException(endIndex);  
  7. }  
  8. if (beginIndex > endIndex) {  
  9.     throw new StringIndexOutOfBoundsException(endIndex - beginIndex);  
  10. }  
  11. return ((beginIndex == 0) && (endIndex == count)) ? this :  
  12.     new String(offset + beginIndex, endIndex - beginIndex, value);  
  13.    }  

 而其中用到的String构造方法是这样的:

[java] view plaincopy
  1.    // Package private constructor which shares value array for speed.  
  2.    String(int offset, int count, char value[]) {  
  3. this.value = value;  
  4. this.offset = offset;  
  5. this.count = count;  
  6.    }  

this.value=value这种实现就出现问题了,因为String类中有几个私有的成员变量:

[java] view plaincopy
  1. /** The value is used for character storage. */  
  2.    private final char value[];  
  3.   
  4.    /** The offset is the first index of the storage that is used. */  
  5.    private final int offset;  
  6.   
  7.    /** The count is the number of characters in the String. */  
  8.    private final int count;  
  9.   
  10.    /** Cache the hash code for the string */  
  11.    private int hash; // Default to 0  

明白了吧,这种实现还在引用着原先字符串变量的value[],通过offset和count返回一个长得像的“截取”后的字符串给人一种错觉,导致JVM认为这个最初字符串还在被引用着不对其gc,不过之所以这么做SUN公司(oracle 10年收购了)的JDK编写人员也是有原因的,就是效率问题,如注释所说:

 Package private constructor which shares value array for speed.  

这样导致的后果就是如果有一个很大很长的字符串我只需要其中的一小部分字符串用substring实现的话,如果让你看似得到的“新”的短小字符串一直没被JVM 回收的话,那么相当这个最初的大字符串也没被回收,尤其是你把这个短小“新”的字符串直接以引用的形式付给一个静态的全局变量,在加上如果访问数量很大,那应该“代价”还是蛮可观的,不过可以简单的这样new(s.substring())就避免了这个问题。

        新的JDK中substring之所以不存在这个问题了,是因为这个构造方法改成这样了:

[java] view plaincopy
  1. public String(char value[], int offset, int count) {  
  2.       if (offset < 0) {  
  3.           throw new StringIndexOutOfBoundsException(offset);  
  4.       }  
  5.       if (count < 0) {  
  6.           throw new StringIndexOutOfBoundsException(count);  
  7.       }  
  8.       // Note: offset or count might be near -1>>>1.  
  9.       if (offset > value.length - count) {  
  10.           throw new StringIndexOutOfBoundsException(offset + count);  
  11.       }  
  12.       this.offset = 0;  
  13.       this.count = count;  
  14.       this.value = Arrays.copyOfRange(value, offset, offset+count);  
  15.   }  

其中 value变量不再引用了而是重新新建了一个,所以没有这个问题了,是不是大家看完之后对这个方法有了更全新的认识?大笑

转载请注明—作者:Java我人生(陈磊兴)   原文出处:http://blog.csdn.net/chenleixing/article/details/43646255

0 0
原创粉丝点击