在需要截取一个字符串的字串时,经常会使用String.substring(int beginIndex, int endIndex)方法来截取字串,但是该方法为了节约时间,提升性能,浪费了大量空间,其源代码如下:
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);
}
其中,new String(offset + beginIndex, endIndex - beginIndex, value)返回了一个新建的String对象,查看该构造函数源码如下:
String(int offset, int count, char value[]) {
this.value = value;
this.offset = offset;
this.count = count;
}
通过以上可知,其只是通过偏移量来获取一个字符数组的子数组,但是原数组中,没有被选择的并没有及时释放,因此,如果原字符串很长,而需要的子字符串又很短,则很容易造成内存溢出,如以下代码:
public class SubStringTest {
public static void main(String[] args) {
List<String> handlerList = new ArrayList<String>();
/*
* HugeStr不到100000次就内存溢出 但是ImprovedHuge不会
*/
for (int i = 0; i < 100000; i++) {
HugeStr h = new HugeStr();
handlerList.add(h.getSubString(1, 5));
System.out.println("times:"+i);
}
}
static class HugeStr {
private String str = new String(new char[100000]);
// 一个很长的字符串
public String getSubString(int begin, int end) {
// 获取字符串,有溢出
return str.substring(begin, end);
}
}
}
所以在实际开发中,必须谨慎使用该方法