java内存泄露快速检查方法

Identify memory leak in Java app

One important advantage of java is programer don't care memory management and GC handle it well. Maybe this is one reason why java is more popular.

As Java programer you real dont care it? After you meet Out of memory you will realize it it’s not true.

Java GC and memory is big topic you can get some information in here

Today just let me show how to identify memory leak quickly.

Let quickly review demo java code, it’s one kind of memory leak in our code, using static collection and always add some object.

import java.util.ArrayList;
import java.util.List;

public class MemoryTest {
public static void main(String[] args) {
new Thread(new MemoryLeak(), "MemoryLeak").start();
}
}

class MemoryLeak implements Runnable {
public static List<Integer> leakList = new ArrayList<Integer>();
public void run() {
int num =0;
while(true) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
num++;
Integer i = new Integer(num);
leakList.add(i);

}
}
}

run it with java -verbose:gc -XX:+PrintGCDetails -Xmx60m -XX:MaxPermSize=160m MemoryTest

after about some minuts you will get

Exception in thread "MemoryLeak" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2760)
at java.util.Arrays.copyOf(Arrays.java:2734)
at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
at java.util.ArrayList.add(ArrayList.java:351)
at MemoryLeak.run(MemoryTest.java:25)
at java.lang.Thread.run(Thread.java:619)
Heap
def new generation total 18432K, used 3703K [0x045e0000, 0x059e0000, 0x059e0000)
eden space 16384K, 22% used [0x045e0000, 0x0497dde0, 0x055e0000)
from space 2048K, 0% used [0x055e0000, 0x055e0000, 0x057e0000)
to space 2048K, 0% used [0x057e0000, 0x057e0000, 0x059e0000)
tenured generation total 40960K, used 40959K [0x059e0000, 0x081e0000, 0x081e0000)
the space 40960K, 99% used [0x059e0000, 0x081dfff8, 0x081e0000, 0x081e0000)
compacting perm gen total 12288K, used 2083K [0x081e0000, 0x08de0000, 0x10de0000)
the space 12288K, 16% used [0x081e0000, 0x083e8c50, 0x083e8e00, 0x08de0000)
No shared spaces configured.

OK let us quickly identify it using JProfile

Download JProfile in here 

Run JProfile and attach MemoryTest get largest size of  Objects in Memory View in here is Integer


then select Integer and go to Heap Walker.

get GC Graph for this object


 Then you get detail code raise this issue quickly now.

 That is enjoy it. 

更多的内容

JBossBook 303
Doxygen 的使用简介
全新java初学者实践教程
JAVA2安全书的例子
JAVA编程利器Eclipse
Java私塾跟我学系列
java基础知识总结(经典)
Eclipse Web开发从入门到精通 实例
Java编程思想
C++与Java混合编程
将win7电脑变身WiFi热点,让手机、笔记本共享上网
JSP数据库访问技术研究
JSP应用开发技术(书)
JAVA 2平台安全技术-结构,API设计和实现
手机软件开发平台逐个来数 
SitePoint.jQuery.Novice.to.Ni nja.2nd.Edition.2012.eBook-EL OHiM
[JavaScript.Web.Applications] .Alex.MacCaw
JAVA语言入门
高薪诚聘软件J2ME开发工程师,J2EE开发工程师
oracle存储过程学习经典
JSP由浅入深
android安装步骤
Java编程思想
Java问题定位技术
Java语言SQL接口 JDBC编程技术
精通Tomcat:Java Web应用开发、框架分析与组件配置、系统集成与案例实战
一个基于XML的Log框架
Java 7 New Features Cookbook
一个用JavaScript写的俄罗斯方块


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值