Java JDK目录下的jmap和jhat工具的使用方式

简介:

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

本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

相关文章
|
5月前
|
监控 Java Android开发
使用JDK自带的jmap和jhat监控处于运行状态的Java进程
使用JDK自带的jmap和jhat监控处于运行状态的Java进程
54 0
|
算法 Java Linux
Java的jmap命令使用详解
jmap(Java Virtual Machine Memory Map)是JDK提供的一个可以生成Java虚拟机的堆转储快照dump文件的命令行工具。除此以外,jmap命令还可以查看finalize执行队列、Java堆和方法区的详细信息,比如空间使用率、当前使用的什么垃圾回收器、分代情况等等。
565 0
Java的jmap命令使用详解
|
监控 Java Android开发
使用JDK工具jmap和jhat监控Java进程
使用JDK工具jmap和jhat监控Java进程
146 0
使用JDK工具jmap和jhat监控Java进程
|
监控 Java
Java命令行监控工具(jmap,jstack,jstat,jinfo,jps)
项目部署上线之后,线上项目是不能轻易修改的,定位问题的难度也会变大。因此监控是非常重要的一个环节,有了监控,我们才能更好的定位系统中的问题,从而排查。监控的工具有很多种, 但是java自带的命令行监控工具,是必须要掌握的。
3023 0
|
人工智能 Java 大数据
jmap, jhat, jvisualvm:java堆内存对象分析利器
image jmap -help 查看命令用法。 image jmap -heap 查看堆使用情况。 image jmap -dump导出堆对象文件进行内存分析。
4330 0
|
29天前
|
Java 调度 Windows
JAVA面试八股文之多线程基础知识
JAVA面试八股文之多线程基础知识
|
1天前
|
存储 缓存 Java
面试官:什么是Java内存模型?
面试官:什么是Java内存模型?
面试官:什么是Java内存模型?