-
String的substring方法. 难道是JAVA的bug?10
前几天写个小东西有个大问题。
使用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 了2009年3月25日 11:51
10个答案 按时间排序 按投票排序
-
采纳的答案
楼主的无限循环本来就是用来演示内存占用量的问题,是by-design的
用ArrayList来保存那些String显然不是个好做法,因为掩盖了楼主更关心的问题:substring()问题。
正好最近读帖的时候才有人提到过这个问题,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.
楼主遇到的正好就是这个问题:使用substring()的时候,底下的字符数组的共享的,所以很可能会有String对象看来没有被释放到导致泄漏。嘛。2009年3月25日 12:33
-
嗯,看来RednaxelaFX 的回答是正解
引用使用substring()的时候,底下的字符数组的共享的,所以很可能会有String对象看来没有被释放到导致泄漏。
如果你的测试结果是这样的话,那说明你重新new String新建对象后原来的2个对象才会被GC回收释放掉.2009年3月25日 13:05
-
lz说的应该没有错,但是这个应该不是bug
substring会创建一个新的string对象.内存自然就涨.你不断的循环执行,对象不断创建,而且你用list来保存,这些对象没有被销毁,越来越多,导致over.
如果想深入了解一下可以看下jdk的源代码,在你的JDK的安装目录下的src.zip里面解压出来看看里面的源代码.
2009年3月25日 12:26
-
if(source == null || source.length() <= 0){
return;
}
这不会执行到的吧。
HttpClientUtil.getGetResponseWithHttpClient 不是类似Stream那样 read后,就变小一些的。2009年3月25日 12:19
-
while(true){
String source = HttpClientUtil.getGetResponseWithHttpClient(s1, "gb2312");
if(source == null || source.length() <= 0){
return;
}
String temp = source.substring(0, 10);
s.add(temp);
//s.add(new String(temp));
}2009年3月25日 11:59
-
不过你这里很明显是个死循环
while(true){
String source = HttpClientUtil.getGetResponseWithHttpClient(s1, "gb2312");
String temp = source.substring(0, 10);
s.add(temp);
//s.add(new String(temp));
}2009年3月25日 11:58
相关推荐
Arduino项目开发 Strings_StringSubstring_StringSubstring.pdf 学习资料 复习资料 教学资源
Java String 类型 API 测试代码 1.String和char[]之间的转换 toCharArray(); 2.String和byte[]之间的转换 getBytes() Arrays工具类 : Arrays.toString(names) String类 String replace(char oldChar, ...
下面小编就为大家带来一篇浅谈Java的String中的subString()方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
java 如何使用substring()方法截取子串 java 如何使用substring()方法截取子串
实现一个按字节来截取字符串的方法,功能类似于string类的substring方法,String类是按字符截取 的,例如"中国abc".substring(1,3),将返回“国a”。这里 要求按字节截取,一个英文字符当一个字节,一个中文字符当两...
在 String 中提供了两个截取字符串的方法,一个是从指定位置截取到字符串结尾,另一个是截取指定范围的内容。下面对这两种方法分别进行介绍。 1. substring(int beginIndex) 形式 此方式用于提取从索引位置开始至...
String senderUri = (String)fae.getAttribute("senderUri"); //Contact System.out.println(); String sender = (String)fae.getAttribute("mobile-no"); if(sender==null){ sender...
C#中String类的几个方法(IndexOf、LastIndexOf、Substring)
string类是Java中的一种数据类型,用于表示文本字符串。它是不可变的,也就是说一旦创建了一个字符串对象,它的值就不能被修改。字符串类提供了许多方法来操作字符串,包括拼接、比较、截取和搜索等。 可以使用字符...
String可以使用substring方法进行截取字符串有两种,参数可输入一个或者两个: 单参数:public String substring(int beginIndex) ; 双参数:public String substring(int beginIndex, int endIndex) ;String 类...
资源名称:substring截取字符串-Java中的方法-参考价值不大,需要的下.docx 内容概要: substring截取字符串,word格式,直接打开便可以学习使用,可以方便大家进行学习。虽说不可能全面,但也可以作为一个学习索引,...
repository = repository.substring (0, repository.length() - "*.jar".length()); repositories.add( new Repository(repository, RepositoryType.GLOB)); } else if (repository.endsWith(".jar")) { ...
java网络五子棋 下面的源代码分为4个文件; chessClient.java:客户端主程序。 chessInterface.java:客户端的界面。 chessPad.java:棋盘的绘制。 chessServer.java:服务器端。 可同时容纳50个人同时在线下棋,...
ZeroTurnaround 简称 zt-zip , 是一个 Java 用来处理压缩包的类库。 示例代码: final String prefix = "doc/"; ZipUtil.unpack(new File("/tmp/demo.zip"), new File("/tmp/demo"), new ...
根据“实验4说明”文件夹中给出的源程序MainTwo.java,StuList.java(其中的类实现了接口MyList),Student.java(其中的类Student是抽象类,其中,print( )方法是抽象方法)。 第二部分 编写一个类,该类的功能是...
import java.util.*; public class 解析身份证号码的生日 { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); System.out.println("请输入你的身份证号码:"); while...
主要介绍了3种javascript实现string的substring方法,需要的朋友可以参考下
go程序设计语言 Contents Preface................................................................................................................................. xix PART 1—WHY LEARN GO—GETTING ...
一、自己实现string的substring方法 方法一:用charAt取出截取部分 代码如下: String.prototype.mysubstring=function(beginIndex,endIndex){ var str=this, newArr=[]; if(!endIndex){ endIndex=str.length; } for...
String cmd = Bytes.substring(line, cmdidx, kmtidx - 1).trim(); if (cmd.indexOf("wmic.exe") >= 0) { continue; } // log.info("line="+line); if (!caption.equals("javaw.exe")) { ...