Java JDK目录下的jmap和jhat工具的使用方式
发布日期:2021-06-30 14:20:17 浏览次数:2 分类:技术文章

本文共 1566 字,大约阅读时间需要 5 分钟。

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

(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:

heap bin file is generated now:

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

jhat c:\temp\heapstatus.bin

Then access localhost:7000 in browser:

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.

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

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

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

Click “Find object by address”:

Type address of object instance you want to inspect:

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

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

转载地址:https://jerry.blog.csdn.net/article/details/105932041 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:使用Chrome开发者工具研究JavaScript的垃圾回收机制
下一篇:Java注解@Cacheable的工作原理

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月25日 11时41分26秒