Java中,String的subString方法易导致内存溢出

在需要截取一个字符串的字串时,经常会使用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);
		}
	}
}

所以在实际开发中,必须谨慎使用该方法


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值