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

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

汪子熙 CSDN认证博客专家 前端框架 Node.js SAP
Jerry Wang,2007 年从电子科技大学计算机专业硕士毕业后加入 SAP 成都研究院工作至今。Jerry 是 SAP 社区导师,SAP 中国技术大使。14 多年的 SAP 产品开发生涯,Jerry 曾经先后参与 SAP Business ByDesign,SAP CRM,SAP Cloud for Customer,SAP S/4HANA,SAP Commerce Cloud(电商云)等标准产品的研发工作。
相关推荐
©️2020 CSDN 皮肤主题: 博客之星2020 设计师:CY__ 返回首页
实付 49.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值