java.lang.String的substring、split方法引起的内存问题

本文探讨了Java中String类的substring和split方法可能导致的内存泄漏问题,通过代码示例展示了如何避免这些问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文大部分内容,摘自下面两篇文章:

           http://blog.xebia.com/2007/10/04/leaking-memory-in-java/

            http://www.iteye.com/topic/626801

先用一个极端例子说明String的substring方法引起的OutOfMemoryError问题:

[java]  view plain copy
  1. public class TestGC {     
  2.   private String large = new String(new char[100000]);     
  3.     
  4.   public String getSubString() {     
  5.     return this.large.substring(0,2);     
  6.   }     
  7.     
  8.   public static void main(String[] args) {     
  9.     ArrayList<String> subStrings = new ArrayList<String>();     
  10.     for (int i = 0; i <1000000; i++) {     
  11.       TestGC testGC = new TestGC();     
  12.       subStrings.add(testGC.getSubString());     
  13.     }     
  14.   }     
  15. }  
运行该程序,结果出现:

    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

为什么会出现这个情况?查看一下JDK String类substring方法的源码,可以找到原因,源码如下:

[java]  view plain copy
  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 plain copy
  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.    }  
从该构造函数的访问权限和注释,可以看出,SUN为了优化性能而专门写了这个构造方法。
该方法为了避免内存拷贝,提高性能,并没有重新创建char数组,而是直接复用了原String对象的char[],通过改变偏移量和长度来标识不同的字符串内容。也就是说,substring出的来String小对象,仍然会指向原String大对象的char[],所以就导致了OutOfMemoryError问题

找到问题之后,将上面代码中,getSubString的方法修改一下,如下:

[java]  view plain copy
  1. public String getSubString() {  
  2.     return new String(this.large.substring(0,2));   
  3. }  
将substring的结果,重新new一个String出来。再运行该程序,则没有出现OutOfMemoryError的问题。为什么?因为此时调用的是String类的public的构造方法,该方法源码如下:

[java]  view plain copy
  1.    public String(String original) {  
  2. int size = original.count;  
  3. char[] originalValue = original.value;  
  4. char[] v;  
  5.     if (originalValue.length > size) {  
  6.         // The array representing the String is bigger than the new  
  7.         // String itself.  Perhaps this constructor is being called  
  8.         // in order to trim the baggage, so make a copy of the array.  
  9.            int off = original.offset;  
  10.            v = Arrays.copyOfRange(originalValue, off, off+size);  
  11.     } else {  
  12.         // The array representing the String is the same  
  13.         // size as the String, so no point in making a copy.  
  14.     v = originalValue;  
  15.     }  
  16. this.offset = 0;  
  17. this.count = size;  
  18. this.value = v;  
  19.    }  
从代码可以看出,在String对象中value的length大于count的情况下,会重新创建一个char[],并进行内存拷贝。

除了substring方法之后,String的split方法,也存在同样的问题,split的源码如下:

[java]  view plain copy
  1. public String[] split(String regex, int limit) {  
  2. urn Pattern.compile(regex).split(this, limit);  
  3. }  

可以看出,String的split方法通过Pattern的split方法来实现,Pattern的split方法源码如下:

[java]  view plain copy
  1. public String[] split(CharSequence input, int limit) {  
  2.         int index = 0;  
  3.         boolean matchLimited = limit > 0;  
  4.         ArrayList<String> matchList = new ArrayList<String>();  
  5.         Matcher m = matcher(input);  
  6.   
  7.         // Add segments before each match found  
  8.         while(m.find()) {  
  9.             if (!matchLimited || matchList.size() < limit - 1) {  
  10.                 String match = input.subSequence(index, m.start()).toString();  
  11.                 matchList.add(match);  
  12.                 index = m.end();  
  13.             } else if (matchList.size() == limit - 1) { // last one  
  14.                 String match = input.subSequence(index,  
  15.                                                  input.length()).toString();  
  16.                 matchList.add(match);  
  17.                 index = m.end();  
  18.             }  
  19.         }  
  20.   
  21.         // If no match was found, return this  
  22.         if (index == 0)  
  23.             return new String[] {input.toString()};  
  24.   
  25.         // Add remaining segment  
  26.         if (!matchLimited || matchList.size() < limit)  
  27.             matchList.add(input.subSequence(index, input.length()).toString());  
  28.   
  29.         // Construct result  
  30.         int resultSize = matchList.size();  
  31.         if (limit == 0)  
  32.             while (resultSize > 0 && matchList.get(resultSize-1).equals(""))  
  33.                 resultSize--;  
  34.         String[] result = new String[resultSize];  
  35.         return matchList.subList(0, resultSize).toArray(result);  
  36.     }  
方法中的第9行: Stirng match = input.subSequence(intdex, m.start()).toString();
调用了String类的subSequence方法,该方法源码如下:

[java]  view plain copy
  1. public CharSequence subSequence(int beginIndex, int endIndex) {  
  2.     return this.substring(beginIndex, endIndex);  
  3. }  

通过代码可以看出,最终调用的是String类的substring方法,因此存在同样的问题。split出来的小对象,直接使用原String对象的char[]


看了一下StringBuilder和StringBuffer的substring方法,则不存在这样的问题。其源码如下:

[java]  view plain copy
  1. public String substring(int start, int end) {  
  2. (start < 0)  
  3.  throw new StringIndexOutOfBoundsException(start);  
  4. (end > count)  
  5.  throw new StringIndexOutOfBoundsException(end);  
  6. (start > end)  
  7.  throw new StringIndexOutOfBoundsException(end - start);  
  8.     return new String(value, start, end - start);  
  9. }  
最后一行,调用了String类的public构造方法,方法源码如下:

[java]  view plain copy
  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. }  
该方法不是直接使用原String对象的char[],而是重新进行了内存拷贝。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值