Three "in-depth understanding of java virtual machine" study notes - Chapter Four

Chapter IV VM performance monitoring and troubleshooting tool

1, JDK command line tools

jps command:

  • Role: lists the virtual machines running processes.
  • Format: jps [option] [hostid]
  • Options: -q output only LVMID (Local Virtual Machine Identifier) ​​omitted the name of the main class.

      The parameter passed to the main function of the main class of -m output when the virtual machine process starts

      -l output of the main class full name, if the process of implementation of the jar package, Java output path

        -v output jvm parameters of the virtual machine process starts, showing a list of parameters specified, the default parameters can not be viewed

jstat command:

  • Role: Virtual Machine heap information statistical monitoring tool, it can display local or remote virtual machine in the process of class loading, memory, garbage collection, JIT compiler and other operating data.
  • Format: jstat [option vmid [interval [s | ms] [count]]], option on behalf of the user wants to query the contents of the virtual machine, is divided into class loading, garbage collection, runtime compiler cases, interval representative for virtual machine interval, count represents the number of queries, query only once if the explanation is omitted.
  • Options: -class monitor class loading, unloading quantity, space, and the total time it takes to load the class.

        -gc monitor Java heap conditions, including Eden District, two survivor district, the old era, such as permanent-generation capacity, used space, GC total time and other information.

jinfo command:

  • Role: Java configuration tools, real-time view and adjust the parameters of the virtual machine, including not show the default values ​​for the parameters specified, and added the ability to modify the parameters during operation.
  • Format: jinfo [option] pid
  • Options: -flag output parameter named value vm

jmap command:

  • Role: Java memory imaging tools for raw heap dump snapshot (commonly referred to as heapdump or dump file), you can finalize the query execution queue, Java heap and details of permanent generation, such as space usage that is currently used kinds of collector and so on.
  • Format: jmap [option] vmid
  • Options: -dump generation Java heap dump snapshot. -dump: fomat = b, filename = <filename>

      -finalizerinfo subjects showing an F-Queue waiting Finalizer thread executing finalizer method, only Linux, Solaris platforms effectively.

      -heap display object on the heap statistics, only in Linux, Solaris platforms effectively.

      -histo display object on the heap statistics, including the class instance data, aggregate capacity.

      -F when the virtual machine process to dump option is not available in response to this option to force generate dump snapshot, only Linux, Solaris platforms effectively.

jhat command:

  • Role: virtual machine snapshot dump analysis tool, used in conjunction with jmap to analyze heap storage snapshot jmap generated, based on analysis of the default package units
  • Format: jhat filename, open the browser to access http: // localhost: 7000

jstack command:

  • Role: Java stack trace tool for generating virtual machine snapshot of the current thread, the thread method is a snapshot of the current virtual machine each executing thread stack collection, you can view each thread call stack by jstack, you can navigate to the thread pause the common cause.
  • Format: jstack [option] vmid
  • Options: -F when the normal output is not responding, force the output thread stack.

      In addition to an outer stack -l display additional information about locks.

      -m If the local method call, you can display the C / C ++ stack

HSDIS plug-ins:

  • Role: HotSpot virtual machine disassembly to provide plug-in, let's -XX HotSpot: + PrintAssembly instructions to call him to the dynamically generated native code to analyze the problem.

2, JDK visualizer

JConsole:

  • Role: Java Monitoring and Management Console
  • Jstat memory tag is equivalent to the command used to monitor income Collector Manager virtual machine memory.
    Copy the code
    package com.ecut.tool;
    
    import java.util.ArrayList;
    import java.util.List; public class MonitoringTest { static class OOMObject{ public byte[] placeholder = new byte[64*1024]; } public static void fillHeap(int num) throws InterruptedException { List<OOMObject> list = new ArrayList<>(); for(int i =0; i < num ; i++){ Thread.sleep(50); list.add(new OOMObject()); } System.gc(); } public static void main(String[] args) throws InterruptedException { fillHeap(1000); } }
    Copy the code

    Results are as follows:

  • Thread tag equivalent to jstack command can use this tab to analyze the face of the thread standstill.
    Copy the code
    package com.ecut.tool;
    
    import java.io.BufferedReader;
    import java.io.IOException; import java.io.InputStreamReader; public class MonitoringThreadTest { /** * 死循环 */ public static void createBusyThread(){ Thread thread = new Thread(new Runnable() { @Override public void run() { while (true); } },"testBusyThread"); thread.start(); } /** * 等待锁 * @param lock */ public static void createLockThread(final Object lock){ Thread thread = new Thread(new Runnable() { @Override public void run() { synchronized (lock){ try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } },"testLockThread"); thread.start(); } public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader((System.in))); bufferedReader.readLine(); createBusyThread(); bufferedReader.readLine(); createLockThread(new Object()); } }
    Copy the code

     运行结果如下:

  • 停顿的主要原因有等待外部的资源(数据库连接网络资源),死循环锁等待。

VisualVM插件:

  • 显示虚拟机进程及进程配置环境信息。
  • 监视应用程序的CPU、GC、堆、方法区以及线程信息。
  • dump以及分析堆转储快照。
  • 方法级的程序运行性能分析,找出被调用最多的,运行时间最长的方法。

源码地址:

https://github.com/SaberZheng/jvm-test

转载请于明显处标明出处:

https://www.cnblogs.com/AmyZheng/p/10525147.html

第四章 虚拟机性能监控与故障处理工具

1、JDK命令行工具

jps命令:

  • 作用:列出正在运行的虚拟机进程。
  • 格式:jps [option] [hostid]
  • 选项:-q 只输出LVMID(Local Virtual Machine Identifier)省略主类的名称。

      -m 输出虚拟机进程启动时传递给主类的main函数的参数

      -l 输出主类全名,如果进程执行的时jar包,输出Java路径

        -v 输出虚拟机进程启动的jvm参数,显示指定的参数列表,默认参数无法查看

jstat命令:

  • 作用:虚拟机统计堆信息监视工具,它可以显示本地或者远程虚拟机进程中的类装载、内存、垃圾收集、JIT编译等运行数据。
  • 格式:jstat [option vmid [ interval [ s | ms] [ count ] ] ],option代表用户希望查询虚拟机的内容,主要分为类装载,垃圾收集,运行期编译情况,interval代表查询虚拟机间隔,count代表查询次数,若省略说明只查询一次。
  • 选项:-class 监视类装载,卸载数量,总空间以及类装载所耗费的时间。

        -gc监视Java堆状况,包括Eden区,两个survivor区,老年代,永久代等容量,已用空间,GC时间合计等信息。

jinfo命令:

  • 作用:Java配置信息工具,实时的查看和调整虚拟机各项参数,包括未被显示指定的参数的默认值,并且加入了运行期间修改参数的能力。
  • 格式:jinfo [option] pid
  • 选项:-flag 输出已命名vm参数的值

jmap命令:

  • 作用:Java内存映像工具,用于生成堆转储快照(一般称为heapdump或者dump文件),它还可以查询finalize执行队列,Java堆和永久代的详细信息,如空间使用率当前用的是那种收集器等。
  • 格式:jmap [option] vmid
  • 选项:-dump 生成Java堆转储快照。-dump: fomat=b, filename=<filename>

      -finalizerinfo显示出F-Queue中等待Finalizer线程执行finalizer方法的对象,只在Linux、Solaris平台有效。

      -heap显示堆中对象统计信息,只在Linux、Solaris平台有效。

      -histo显示堆中对象统计信息,包括类实例数据,合计容量。

      -F当虚拟机进程对dump选项没有响应时可使用这个选项强制生成dump快照,只在Linux、Solaris平台有效。

jhat命令:

  • 作用:虚拟机对转储快照分析工具,与jmap配合使用,来分析jmap生成的堆储快照,分析结果默认是以包为单位
  • 格式:jhat filename ,打开浏览器访问http://localhost:7000

jstack命令:

  • 作用:Java堆栈跟踪工具,用于生成虚拟机当前线程快照,线程快照就是当前虚拟机每一条线程正在执行的方法堆栈的集合,可以通过jstack来查看各个线程调用堆栈,就可以定位到线程出现停顿的常见原因。
  • 格式:jstack [option] vmid
  • 选项:-F当正常输出不被响应时,强制输出线程堆栈。

      -l除了堆栈外显示有关锁的附加信息。

      -m如果调用本地方法的话,可以显示C/C++的堆栈

HSDIS插件:

  • 作用:HotSpot虚拟机提供的反汇编插件,是让HotSpot的-XX:+PrintAssembly指令调用他来把动态生成的本地代码来分析问题。

2、JDK可视化工具

JConsole:

  • 作用:Java监视与管理控制台
  • 内存标签相当于jstat命令用于监视收收集器管理的虚拟机内存。
    Copy the code
    package com.ecut.tool;
    
    import java.util.ArrayList;
    import java.util.List; public class MonitoringTest { static class OOMObject{ public byte[] placeholder = new byte[64*1024]; } public static void fillHeap(int num) throws InterruptedException { List<OOMObject> list = new ArrayList<>(); for(int i =0; i < num ; i++){ Thread.sleep(50); list.add(new OOMObject()); } System.gc(); } public static void main(String[] args) throws InterruptedException { fillHeap(1000); } }
    Copy the code

    运行结果如下:

  • 线程标签相当于jstack命令遇到线程停顿时可以使用这个页签进行分析。
    Copy the code
    package com.ecut.tool;
    
    import java.io.BufferedReader;
    import java.io.IOException; import java.io.InputStreamReader; public class MonitoringThreadTest { /** * 死循环 */ public static void createBusyThread(){ Thread thread = new Thread(new Runnable() { @Override public void run() { while (true); } },"testBusyThread"); thread.start(); } /** * 等待锁 * @param lock */ public static void createLockThread(final Object lock){ Thread thread = new Thread(new Runnable() { @Override public void run() { synchronized (lock){ try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } },"testLockThread"); thread.start(); } public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader((System.in))); bufferedReader.readLine(); createBusyThread(); bufferedReader.readLine(); createLockThread(new Object()); } }
    Copy the code

     运行结果如下:

  • The main reason for pause have the resources (database connections network resources) waiting outside, waiting for death cycle lock.

VisualVM plugin:

  • Display the virtual machine configuration processes and process environmental information.
  • CPU monitoring applications, GC, heap, the method area and thread information.
  • dump heap dump analysis and snapshots.
  • Running performance level method of analysis to identify the most is called, the longest running method.

Source Address:

https://github.com/SaberZheng/jvm-test

Reproduced please indicate the source in a prominent place :

https://www.cnblogs.com/AmyZheng/p/10525147.html

Guess you like

Origin www.cnblogs.com/manmanchanglu/p/11621839.html