java substring 性能_《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仅仅存(5-1)*100个字符 应该是完全没问题的。

`-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。

看下测试效果:

ff2bd3881d74d25cc0e4fa186686c43c.png

一个子串包含了原字符串的所有char,1024*1024=148576

990fb2eb8f47ed4bcee958cc427ae689.png

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

而ImprovedHugeStr的gutSub的时候, 实际在new String(str.substring(1, 5));的时候,返回的Arrays.copyOfRange拷贝的值(③)。已经失去了原字符串str 包括其value的引用,即使原字符串的value 很大,但是已经不被GC Roots对象链接到,会被回收。区区4长度*100的字符串, 不会引起OOM.

效果如图:

37f584037930d26d7e6bc39cdd0cbfc7.png

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;

}

测试效果:

24bd9e9093b2d566d30f22687cb9269c.png

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值