jmap java_使用JDK工具jmap和jhat监控Java进程

Suppose you have a running Java process and you would like to inspect its running status, for example how many object instance are created or memory consumption status, you can use some standard tool provided by JDK.

This blog is written based on JDK 1.8.

The sample code I am using to simulate a endless running process:package jmap;

class Tool{

private int count = 0;

public void Run() throws InterruptedException{

while(true){

System.out.println("Hello: " + this.count++);

Thread.sleep(5000);

}

}

}

public class JMapTest {

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

Tool tool = new Tool();

tool.Run();

}

}

(1) First get process id found in task manager: 15392

779621a32cbe96824cc25a858b1808fb.png

(2) use command line

jmap -dump:format=b,file=c:\temp\heapstatus.bin 15392

jmap is a standard tool provided by JDK in this folder in my laptop:

a44f6e36817297de7014b50c5e05f6b9.png

heap bin file is generated now:

1c9c7a616e5264120c9effb2f3c304d2.png

(3) Use another tool jhat to parse the bin file:

jhat c:\temp\heapstatus.bin

cee836cf6af547f91d0b3623457eb2ab.png

Then access localhost:7000 in browser:

e1c357337d87ea9b432120ec7e493868.png

Click hyperlink class jmap.Tool, now I can find out that the instance of my tool class @0x7166babd8 has member attribute count with value 49.

ec25f29b7ddaf6e1bb1dfbd961bf6acc.png

(4) There is a plugin in Eclipse MAT – Memory Analyzer Tool which can achieve the same.

942506b13f97f2eeb0b7414ca2b26ee5.png

Once plugin is installed, you can make them visible in “Show View”:

38f3f7ddc22007dd205985dcd2dd7f21.png

Drag your bin file into the view and the heap file will be parsed automatically.

Click “Find object by address”:

2c641eb781f415fa5288cc25b443d095.png

Type address of object instance you want to inspect:

76491d98d0e41d06ee3b362303f941fe.png

You can get the same result as you get previously in localhost:7000

92ce472aa8b98bc9fcf80f8a2409d8f8.png

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

61fb9822a20bf853fa7bc619689492cf.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值