Home>Article>Java> How to use JDK’s built-in jmap and jhat to monitor the running status of Java processes

How to use JDK’s built-in jmap and jhat to monitor the running status of Java processes

不言
不言 forward
2018-10-22 16:00:47 3531browse

The content of this article is about how to use JDK’s own jmap and jhat to monitor the running status of Java processes. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

For running Java processes, JDK comes with many tools that allow Java developers to monitor various states of the running process, such as how many object instances are created inside the process, consumption How much memory is required, etc.

This article is written based on JDK1.8.

I wrote the simplest Java class below, which contains an infinite loop that increases the value of a counter every 5 seconds.

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(); } }

Execute this application in Eclipse.

The following describes how to use jmap and jhat to monitor this running process.

1. First get the ID of this Java running process: 15392. I directly used the Task Manager that comes with Windows to obtain the process ID.

How to use JDK’s built-in jmap and jhat to monitor the running status of Java processes

2. Use the following command line:

jmap -dump:format=b,file=c:tempheapstatus.bin 15392

jmap is a tool provided by JDK and is located in the bin folder of the JDK installation directory.

How to use JDK’s built-in jmap and jhat to monitor the running status of Java processes

Executing the command line will generate a heap dump file: headstatus.bin

How to use JDK’s built-in jmap and jhat to monitor the running status of Java processes

3. Now you can use another JDK tool, jhat, to read this dump file and parse it. Use the command line:

jhat c:tempheapstatus.bin

How to use JDK’s built-in jmap and jhat to monitor the running status of Java processes

##After the analysis is completed, the output printed by jhat prompts us that Snapshot resolved , which can be viewed from port 7000 of the local server.

Visit http://localhost:7000 and you can see the jmap parsing results.

localhost:7000 in browser:

How to use JDK’s built-in jmap and jhat to monitor the running status of Java processes

Click the hyperlink "jmap Tool" to enter the details: The picture below means my Tool The member variable of the class instance @0x7166babd8, that is, the value of the counter has accumulated to 49.

How to use JDK’s built-in jmap and jhat to monitor the running status of Java processes

4. If you don’t like the command line, you can also use an Eclipse plug-in, MAT – Memory Analyzer Tool, to complete the work with jmap. The jhat command has the same function.

How to use JDK’s built-in jmap and jhat to monitor the running status of Java processes

After this plug-in is installed, there will be an additional view in Eclipse:

How to use JDK’s built-in jmap and jhat to monitor the running status of Java processes

Directly drag the heap dump file generated by jmap into the MAT view, and the results will be automatically parsed and displayed.

Click the button "Find object by address":

How to use JDK’s built-in jmap and jhat to monitor the running status of Java processes

You can also see the objects you saw before in localhost:7000 Details of the instance:

You can get the same result as you get previously in

How to use JDK’s built-in jmap and jhat to monitor the running status of Java processes

How to use JDK’s built-in jmap and jhat to monitor the running status of Java processes

##

The above is the detailed content of How to use JDK’s built-in jmap and jhat to monitor the running status of Java processes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete