zoukankan      html  css  js  c++  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

    (2) use command line
    jmap -dump:format=b,file=c: empheapstatus.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: empheapstatus.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的原创文章,请关注公众号"汪子熙":

  • 相关阅读:
    ASP.NET Core 发布 centos7 配置守护进程
    AutoMapper在asp.netcore中的使用
    git忽略文件并删除git仓库中的文件
    Animate.css 一款牛逼的css3动画库
    URL中特殊符号的处理
    efcore 配置链接sqlserver
    简单抓取小程序大全,并展示。
    UEditor上传图片到七牛C#(后端实现)
    软件项目管理三国启示录01 群雄争霸之项目经理的自我修养
    【调侃】IOC前世今生
  • 原文地址:https://www.cnblogs.com/sap-jerry/p/13607145.html
Copyright © 2011-2022 走看看