随笔-124  评论-49  文章-56  trackbacks-0
最近一直被内存溢出捆扰,现在对这几天的工作做个小小的总结!另外给首次与到此类问题的小鸟们作个提示,希望大家早日变成老鸟!

1 首先确认是不是内存溢出,如果直接报OutOfMemoryException,那肯定是内存溢出,有时可能没有发现这个错误,但WEB服务到最后还是挂了,也有可能是由Memory Out,如何确认,最好的办法就是查看gc日志。

2 如果确认是内存溢出,不要急着跟踪,因为要找到确切位置还是有一定困难的,个人建议对最近修改的代码作一次全面检查,对于有可能出现内存溢出的地方作些修改。
可能出现的原因:
(1) 递归、循环,这里要注意隐性的地笔,如拦载器,我自已遇到一次拦载器的死循环
(2) static 的对象,查看static里面是不是有大量的对象塞进去
(3)Set/List/Map对象,查看Set/List/Map里面是不是有对象用完了没有释放
(4)session/application,查看session里的对象、过期时间等,看是不是无法即时释放
(5)ClassLoader,Java ClassLoader结构的使用为内存泄漏提供了许多可乘之机。这个我不知道深层原因,有知道的可以告诉我,将不胜感谢!
(6)String,字符串累加也容易出现溢出。一般顺序StringBuilder,StringBuffer,String。
(7)全局变量,尽量不使用,使用了即时释放
修改后再查看日志,也许问题已经解决了。

3 如果还是有溢出就只能上工具了,我使用的是JProfiler,功能很强大,首先我在window上远程监控linux,结果失败,原因是因为JProfiler本身就有很大的消耗,而且在测试中还要不断gc,影响生产,所以选用在测试环境中配合jmeter测试。

4 测试方法:循环测试,查看gc后是否有对象数量不断增加,即有对象未释放。

5 其此是要了解java内存分配原理和gc的工作原理,这样才可以定位到问题的具体位置。

6 对jvm进行优化。




具体操作未详细说明,可以参考相关资料

java内存泄漏原因 http://www.ibm.com/developerworks/cn/java/l-JavaMemoryLeak/

gc原理http://chenchendefeng.javaeye.com/blog/455883

jvm优化http://www.cjsdn.net/post/view?bid=1&id=197954

JVM调优-解决native heap持续增长 http://sw1982.javaeye.com/blog/724626 荐



posted on 2010-07-22 11:11 junly 阅读(2170) 评论(1)  编辑  收藏 所属分类: java

评论:
# re: 内存溢出跟踪笔录 2010-07-22 17:34 | jack_yin
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML>
<style type="text/css">
.button{
height:19px;background-color: #f1f1f1;margin:none;padding:0 2px 0 3px!important;padding: 0 1px 0 2px;color: #000000;border-top: 1px solid #CCCCCC;border-left: 1px solid #CCCCCC;border-right: 1px solid #CCCCCC;border-bottom:1px solid #CCCCCC;background-image: url(img/button_bg.gif);margin:0 0 0 0;}
</style>
<BODY>
<input type="button" value="计时开始" class="button" id="buttonTime" onclick="startTime();" />
<input type="text" id="displayTime" readonly="readonly" align="middle" />
<input type="button" value="统计" class="button" onclick="getResult();" />
<input type="text" id="displayMoney" />
<input type="button" onclick="toZero();" value="归零" class="button" />
<br />
<input type="image" src="hide_b.gif" style=" background:#999999" />

<SCRIPT language=javascript>
<!--
//document.write("");

var currentTime;
var stopTimeout;
function show_date_time(){

stopTimeout = window.setTimeout("show_date_time()",1/1000);
timeold=((new Date()).getTime()-currentTime.getTime());
sectimeold=timeold/1000 ;
secondsold=Math.floor(sectimeold);
msPerDay=24*60*60*1000 ;
e_daysold=timeold/msPerDay;
daysold=Math.floor(e_daysold);
e_hrsold=(e_daysold-daysold)*24;
hrsold=Math.floor(e_hrsold);
e_minsold=(e_hrsold-hrsold)*60;
minsold=Math.floor((e_hrsold-hrsold)*60);
seconds=Math.floor((e_minsold-minsold)*60);
document.getElementById("displayTime").value=""+daysold+"天"+hrsold+"小时"+minsold+"分"+seconds+"秒";

}

function startTime(){
var v = document.getElementById("buttonTime").value;
currentTime=new Date();
if(v=="计时开始"){
document.getElementById("buttonTime").value="停止计时";
show_date_time();
}
else{
document.getElementById("buttonTime").value="计时开始";
window.clearTimeout(stopTimeout);
}
}

function getResult(){
document.getElementById("buttonTime").value="计时开始";
document.getElementById("displayMoney").value="one dollar";
window.clearTimeout(stopTimeout);
}

function toZero(){
document.getElementById("displayTime").value="0天0小时0分0秒";
document.getElementById("displayMoney").value="";
}
//-->
</SCRIPT>
</BODY>

  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: