iteye_7771 2009-03-25 11:51
浏览 303
已采纳

String的substring方法. 难道是JAVA的bug?

前几天写个小东西有个大问题。
使用java获取网页的源代码截东西。后来发现内存光涨不降。

发现String.substring中内容如果过大就有问题。
后来使用
String content = //获取网页内容 最好大一点
String temp = content.subString(0, 10);
String result = new String(temp);

return result;

如果直接返回temp。。。内存就有好瞧的。。大家可以试试下面的代码。基本上一分钟就over了。。最好用jconsole看效果。 呵呵。 如果是我其它的地方写错了。 请大家指正了。

验证程序 HttpClientUtil就是一取网页内容的。可以用别的替代。:
public class Test {

/**
* @param args
*/
public static void main(String[] args) {

String s1 = "http://finance.sina.com.cn/realstock/company/sz000831/nc.shtml";

List<String> s = new ArrayList<String>();
while(true){
String source = HttpClientUtil.getGetResponseWithHttpClient(s1, "gb2312");
String temp = source.substring(0, 10);
s.add(temp);
//s.add(new String(temp));
}

}

}


问题补充:
while(true){//这里的true是我特意加的。为的是延长时间来看到问题的出现。呵呵。 评论的那几位如果有空,请照着试一下就知道我说的情况了。 问题不在这个while的死循环。。。

问题补充:
我这个问题关心的是 使用subString后带来的内存问题。。。即便是使用 StringBuffer,在截取字符串的时候,还是要用 subString吧。
问题补充:
to: playfish
如果使用 s.add(new String(temp)); 就没任何问题。。。内存情况良好。

to: yourgame
呵呵,不清楚为什么你那里为什么会OK。
问题补充:
to sdh5724 :
我的代码是为了证明内存的只往上涨。。当然,你也可以说我的内存上涨是因为死循环造成的。 但使用 s.add(temp); 和 s.add(new String(temp)); 所造成的内存上升幅度则是有大区别。 如果你有空可以跑一下试试。

“以后能不能不用死循环来提问?

知道一秒要执行多少次么.........”

你跑一下就知道一秒执行多少次了。。。呵呵。 你的网络能有多快? 那是去取网页内容的。 我又不是多线程在跑。。。

另: 我也上传的内存使用情况的图片。 幅度不是一般的大。
问题补充:
谢谢 RednaxelaFX 的回答。应该是正解了。

同时也谢谢 playfish 。

不过,好像不能把分拆开吧。 我只好都给 RednaxelaFX  了

  • 写回答

10条回答 默认 最新

  • rednaxelafx 2009-03-25 12:33
    关注

    楼主的无限循环本来就是用来演示内存占用量的问题,是by-design的
    用ArrayList来保存那些String显然不是个好做法,因为掩盖了楼主更关心的问题:substring()问题。

    正好最近读帖的时候才[url=http://www.artima.com/forums/flat.jsp?forum=106&thread=252441&start=60&msRange=15]有人提到过这个问题[/url],
    [quote="James Watson"]Again I don't know C#, but one big gotcha with Strings in Java is that when you substring a large string, the underlying array is shared between the old String and the new String. So your original String object is released and GC'd but the underlying char array is not. If you have lots of big Strings, it looks like your Strings are not being GC'd because the array is the bulk of the memory used by the String. If C# Strings work the same way, I'd give you even odds that that was your problem.[/quote]
    楼主遇到的正好就是这个问题:使用substring()的时候,底下的字符数组的共享的,所以很可能会有String对象看来没有被释放到导致泄漏。嘛。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(9条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建