《Java程序性能优化》subString()方法的内存泄露

String的构造

首先了解下String的构造,String内部使用char [] value 来存储字符。
需要注意 offset和count在1.7已经没有了。

 /** The value is used for character storage. */
    private final char value[];
/** The offset is the first index of the storage that is used. */
private final int offset;//仅仅出现在1.7以下

/** The count is the number of characters in the String. */
private final int count;//仅仅出现在1.7以下

测试用例

环境: jdk1.6  之后为jdk1.7
运行参数  这里指定了最大堆为50m . 如果list仅仅存400个字符 应该是完全没问题的。
`-Xms50m -Xmx50m -XX:+PrintGCDetails`


public class MyTest {

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        List list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add(new HugeStr().getSub());
//            list.add(new ImprovedHugeStr().getSub());
        }
        Object o = list.get(99);
        Field value = String.class.getDeclaredField("value");
        value.setAccessible(true);
        Object o1 = value.get(o);
        if (o1 instanceof char[]) {
            System.out.println(((char[]) o1).length);
        }
    }

    public static class HugeStr{
        private String str = new String(new char[1024*1024]);
        public String getSub(){
            return str.substring(1, 5);//1.7已经修复, 内部采用Arrays.copyOfRange
        }
    }
    public static class ImprovedHugeStr{
        private String str = new String(new char[1024*1024]);
        public String getSub(){
            return new String(str.substring(1, 5));//new String的时候会使用Arrays.copyOfRange 拷贝需要的部分。
        }
    }
}

jdk1.6 String代码部分实现

需要注意subString时传入的value是原字符串的char [] value.
new String 时,判断了offset与count.通过 Arrays.copyOfRange生成字符数组。

    public String substring(int beginIndex, int endIndex) {
	if (beginIndex < 0) {
	    throw new StringIndexOutOfBoundsException(beginIndex);
	}
	if (endIndex > count) {
	    throw new StringIndexOutOfBoundsException(endIndex);
	}
	if (beginIndex > endIndex) {
	    throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
	}
	return ((beginIndex == 0) && (endIndex == count)) ? this :
	    new String(offset + beginIndex, endIndex - beginIndex, value);//此处value储存有原字符串的所有字符①
    }
    
    // Package private constructor which shares value array for speed.
    String(int offset, int count, char value[]) {//subString最终调用此方法②
	this.value = value;
	this.offset = offset;
	this.count = count;
    }
    
   public String(String original) {
	int size = original.count;
	char[] originalValue = original.value;
	char[] v;
  	if (originalValue.length > size) {
 	    // The array representing the String is bigger than the new
 	    // String itself.  Perhaps this constructor is being called
 	    // in order to trim the baggage, so make a copy of the array.
            int off = original.offset;
            v = Arrays.copyOfRange(originalValue, off, off+size);//③此处保证修复后的代码不再泄露 
 	} else {
 	    // The array representing the String is the same
 	    // size as the String, so no point in making a copy.
	    v = originalValue;
 	}
	this.offset = 0;
	this.count = size;
	this.value = v;
    }

由上面代码可以知道。当使用HugeStr的getSub的时候,str.subString(1,5)返回的子串包含了原字符串的char数组 value.

jdk1.6中使用测试subString

①处 subString的value (v)和②处value代表原字符串str 的字符数组(长度大于4)。由于v的长度远大4,所以很快就内存不够,当内存不够发生gc的时候,v一直被子串b引用,导致无法回收。所以会报OOM。
看下测试效果:
《Java程序性能优化》subString()方法的内存泄露
一个子串包含了原字符串的所有char,1024*1024=148576
《Java程序性能优化》subString()方法的内存泄露

jdk1.6中使用修复后的代码测试subString

而ImprovedHugeStr的gutSub的时候, 实际在new String(str.substring(1, 5));的时候,返回的Arrays.copyOfRange拷贝的值(③)。已经失去了原字符串str 包括其value的引用,即使原字符串的value 很大,但是已经不被GC Roots对象链接到,会被回收。区区4长度*100的字符串, 不会引起OOM.
效果如图:
《Java程序性能优化》subString()方法的内存泄露

JDK1.7中已经修复了该问题

在new String内部使用了 Array.copyOfRange 拷贝。 原value 不再引用。当内存不够发生gc的时候,原value被回收。

jdk1.7 String代码部分实现

关键在 subString后调用的构造方法有改变。也是通过Arrays.copyOfRange拷贝。

    public String substring(int beginIndex, int endIndex) {
        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(beginIndex);
        }
        if (endIndex > value.length) {
            throw new StringIndexOutOfBoundsException(endIndex);
        }
        int subLen = endIndex - beginIndex;
        if (subLen < 0) {
            throw new StringIndexOutOfBoundsException(subLen);
        }
        return ((beginIndex == 0) && (endIndex == value.length)) ? this
                : new String(value, beginIndex, subLen);
    }
    public String(char value[], int offset, int count) {//subString最终调用此构造方法
        if (offset < 0) {
            throw new StringIndexOutOfBoundsException(offset);
        }
        if (count < 0) {
            throw new StringIndexOutOfBoundsException(count);
        }
        // Note: offset or count might be near -1>>>1.
        if (offset > value.length - count) {
            throw new StringIndexOutOfBoundsException(offset + count);
        }
        this.value = Arrays.copyOfRange(value, offset, offset+count);//此处是关键
    }
   public String(String original) {
    this.value = original.value;//1.7的构造方法有改变。  成员不再有 count,offset
    this.hash = original.hash;
}

测试效果:
《Java程序性能优化》subString()方法的内存泄露

参考

Java中的substring真的会引起内存泄露么?