MyEclipse Profile 结合visualvm 监控内存泄露

VisualVM是集成了多个JDK命令工具的一个可视化工具,它主要用来监控JVM的运行情况,可以用它来查看和浏览Heap Dump、Thread Dump、内存对象实例情况、GC执行情况、CPU消耗以及类的装载情况。在JDK Update7之后,VisualVM作为JDK的一部分发布,但同时VisualVM也发布独立的版本。VisualVM必须运行在JDK1.6以上的VM环境下,但可以用它来监控JDK1.4以上的JVM。
1:创建java工程VisualvmTest,写一个消耗内存的测试代码:
Java代码 复制代码 收藏代码
  1. publicclassMemoryLeak{
  2. publicstaticvoidmain(String[]args)throwsInterruptedException{
  3. intMAX_CONSUMERS=10000;
  4. intSLEEP_BETWEEN_ALLOCS=5;
  5. ConsumerContainerobjectHolder=newConsumerContainer();
  6. while(objectHolder.size()<MAX_CONSUMERS){
  7. System.out.println("Allocatingobject"
  8. +Integer.toString(objectHolder.size()));
  9. objectHolder.add(newMemoryConsumer());
  10. try{
  11. Thread.currentThread().sleep(SLEEP_BETWEEN_ALLOCS);
  12. }catch(InterruptedExceptionie){
  13. //Donothing.
  14. }
  15. }//while.
  16. Thread.sleep(1000*60*60);
  17. }//main.
  18. }//EndofMemoryLeak.
  19. classConsumerContainerextendsVector{
  20. }
  21. classMemoryConsumer{
  22. publicstaticfinalintMEMORY_BLOCK=1024;
  23. publicbyte[]memoryHoldingArray;
  24. MemoryConsumer(){
  25. memoryHoldingArray=newbyte[MEMORY_BLOCK];
  26. }
  27. }//EndMemoryConsumer.
public class MemoryLeak {

	public static void main(String[] args) throws InterruptedException {

		int MAX_CONSUMERS = 10000;
		int SLEEP_BETWEEN_ALLOCS = 5;

		ConsumerContainer objectHolder = new ConsumerContainer();

		while (objectHolder.size() < MAX_CONSUMERS) {
			System.out.println("Allocating object "
					+ Integer.toString(objectHolder.size()));
			objectHolder.add(new MemoryConsumer());
			try {
				Thread.currentThread().sleep(SLEEP_BETWEEN_ALLOCS);
			} catch (InterruptedException ie) {
				// Do nothing.
			}
		} // while.

		Thread.sleep(1000*60*60);
	} // main.

} // End of MemoryLeak.

class ConsumerContainer extends Vector {
}

class MemoryConsumer {
	public static final int MEMORY_BLOCK = 1024;
	public byte[] memoryHoldingArray;

	MemoryConsumer() {
		memoryHoldingArray = new byte[MEMORY_BLOCK];
	}
} // End MemoryConsumer.


2:在MemoryLeak.java右键菜单选择【Profile As】->【Java Application】,在弹出如下窗口,选择【memory】.

3:程序开始启动运行,同时弹出visualvm窗口,如下图,【profile】tab可以看到对象大小比例。【监控】可以看到对的运行走向。

4:可以发现byte数组暂用比较大。在byte[]右键菜单选择【生成快照并显示栈分配跟踪】。能够快速定位在什么地方消耗比较大的内存,能够快速解决问题:



参考资料: VisualVM远程监控
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值