作者killeryagami (kira)
看板java
标题Re: [问题] Java heap space (已爬文)
时间Tue Nov 24 18:43:20 2009
※ 引述《killeryagami (kira)》之铭言:
: JVM能吃的Memory有限,所以我有几个问题想请教各位神人
: 平常在程式撰写的时候,都用什麽方式节省记忆体?
: 再者是我的一部份程式码,用途是读取页面原始码
: public String getHtmlCode(String url,String encode)throws IOException{
: StringBuffer sb = new StringBuffer();
: if(checkUrl(url)){ //这个地方是判断是不是我要的网页
: try {
: toConnection(url);
: br = new BufferedReader(new InputStreamReader(urlConn.
: getInputStream(), encode));
: int count = 0;
: String line = "";
: while ((line = br.readLine()) != null && count<= 700 ) {
: //为了不让资料过大,所以只读取700行的原始码
: sb.append(line.intern());
: count++;
: }
: br.close();
: toDisconnect();
: } catch (Exception e) {
: System.out.println("Paser: " + e);
: }
: this.htmlcode = sb.toString();
: }
: return this.htmlcode;
: }
: 我的Project出现Java heap space的问题,目前怀疑是这部份的程式码
: 在程式离开这个method之後,htmlcode是否会立即被回收(释放空间)?
: 又例如
: String htmlcode = getPageHtml(url_1);
: htmlcode = getPageHtml(url_2);
: 那麽JVM中原本储存的700行url_1的资料,是不是不会被马上释放?
: 最近对於节省记忆体有点头大,希望各位多多指教 m(_ _)m
/**
按照T大以及其他人的建议,找出问题位置
我想抓的资料形式是有分页,因此以回圈方式不断抓分页资料,直到分页不存在为止
错误讯息还是只有一行 Java heap space ......
*/
public ArrayList<String> crawlerLeafData()
{
ArrayList<String> alLeafData = new ArrayList<String>();
String html="";
for (int i = 1; ; i++) {
String nextPageUrl = "
http://mypage/List.jsp?ShowPage="+i; //资料
有分页
try{
html = pas.getHtmlCode(nextPageUrl, "Big5");
if (!checkLeafDataExist(html)) //若分页i已经没有想抓的资料或
不存在
break;
}catch(Exception e){System.out.println("LeafError: "+e);}
ArrayList<String> PageAry = pas.getHtmlArray(html); //抓取某个Tag,
是Url的参数
for (int j = 0; j < PageAry.size(); j++) {
String tempUrl =
"
http://mypage/hello/List.jsp?"+PageAry.get(j);
allLeafData.add(tempUrl);
}
}
return alLeafData;
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.109.18.52
1F:推 PsMonkey:你的问题,出在更基本的问题。想清楚流程跟需要性 11/24 20:23
※ 编辑: killeryagami 来自: 140.109.18.52 (11/25 10:10)
2F:→ killeryagami:是...读取原码太多次吗?不太懂... 11/25 10:14
3F:推 tcw026:最好用finally把inputsream close掉. 11/25 21:31
4F:推 ynchang:请用JRE 6的环境跑看看..会显示OutOfMemory的相关资讯 11/25 22:50
5F:→ xlk:呼叫这个函式的程式做了些什麽?真的有把allLeafData处理掉? 11/26 15:39
6F:→ jej:用ByteArrayInputStream??用byte[]接真的超过heap就挂不用谈.. 11/27 22:01