JVM performance monitoring and troubleshooting tools

    When locating problems for a system, knowledge and experience are the key foundation, and data is the basis. Tools are the means by which knowledge is used to process data. The data mentioned here include: running logs, exception stacks, GC logs, thread snapshot files (threaddump/javacore files), heap dump snapshots (heapdump/hprof files), etc.

   To this end, sun company provides a series of tools to support these functions, these tools are mainly divided into two categories: command line tools and visualization tools.

1. JDK command line tools

    1.1、jps

    J vm process status tool, displays all hotspot virtual machine processes in the specified system.

    to list running virtual machine processes and display the virtual machine execution main class name and the local virtual machine unique ID for those processes.

    jps command format jps [options] [hostid]

    jps can open the remote virtual machine process state of the RMI service through the RMI protocol, and the hostid is the host name registered in the RMI registry.

    jps common options

attribute effect
-p Only output the LVMID, omitting the name of the main class
-m Output the parameters passed to the main class main() function when the virtual machine process starts
-l Output the full name of the main class, if the process executes a jar package, output the jar path
-v Output jvm parameters when the virtual machine process starts

    1.2 、 jstat

    J vm statistics monitoring tool, used to collect the running data of all aspects of the hotspot virtual machine.

    jstat is a command-line tool for monitoring various running status information of virtual machines. It can display running data such as class loading, memory, garbage collection, JIT compilation, etc. in the local or remote virtual machine process. On a server that does not have a GUI graphical interface, but only provides a plain text console environment, it will be a runtime positioning virtual machine. The tool of choice for machine performance issues.

    The command format of jstat:jstat [option vmid [interval [s|ms] [count]] ]

    For the VMID and LVMID in the command format, if it is a local virtual machine process, the VMID and LVMID are the same. If it is a remote virtual machine, the VMID format should be: [protocol:] [//] lvmid[@hostname[ :port]/servername].

The parameters interval and count represent the interval and times of the query respectively. If these two parameters are omitted, it means that the query is only performed once.

option role
-class Monitor loading classes, unloading classes, total space, and time spent loading classes
-gc Monitor the status of the java heap, including the capacity of the eden area, the two survivor areas, the old generation, the permanent generation, etc., the used space, and the total GC time information
-gccapacity The monitoring content is basically the same as -gc, but the output mainly focuses on the maximum and minimum space used by each area of ​​the java heap
-gcutil The monitoring content is basically the same as -gc, but the output mainly focuses on the percentage of the total space used by the control
-gccause Same as -gcutil, but will additionally output the reason for the last gc
-gcnew Monitor the new generation GC situation
-gcnewcapacity The monitoring content is basically the same as -gcnew, and the output mainly focuses on the maximum and minimum space used
-gcold Monitor the old generation GC situation
-gcoldcapacity The monitoring content is basically the same as -gcold, and the output mainly focuses on the maximum and minimum space used
-gcpermcapacity Output the maximum and minimum space used by the permanent generation
-compiler Output JIT compiled method, time-consuming and other information
-printcompilation Outputs methods that have been JIT-compiled

    1.3、jinfo

    C onfiguration info for java, displays virtual machine configuration information.

    The role of jinfo is to view and adjust various parameters of the virtual machine in real time. You can use -flag [+/-] name or -flag name value to modify some runtime-writable virtual machine parameter values.

    jinfo format jinfo [option] pid

    1.4、jmap

    Memory map for java, which generates a memory dump snapshot (heapdump file) of the virtual machine.

    The jmap command is used to generate heap dump snapshots. The role of jmap is not only to obtain dump files, it can also query the details of the finalize execution queue, java heap and permanent generation. Such as space usage, which collector is currently used, etc.

和jinfo命令一样,jmap在windows下也受到比较大的限制。除了生成dump文件的-dump选项和用于查看每个类的实例、控件占用统计的-histo选项在所有操作系用都提供之外,其余选项只能在linux/solaris下使用。

    jmap格式 jmap [option] vmid

选项 作用
-dump 生成java堆转储快照。格式为: -dump:[live,]format=b,file=,其中live子参数说明是否只dump出存活的对象
-finalizerinfo 显示在F-Queue中等待Finalizer线程执行finalize方法的对象。只在Linux/Solaris平台下有效
-heap 显示java堆详细信息,如使用哪种收集器、参数配置、分代情况等,在Linux/Solaris平台下有效
-jisto 显示堆中对象统计信息,包含类、实例对象、合集容量
-permstat 以ClassLoader为统计口径显示永久代内存状态。只在Linux/Solaris平台下有效
-F 当虚拟机进程对-dump选项没有相应时。可使用这个选项强制生成dump快照。只在Linux/Solaris平台下有效

    1.5、jhat

    Jvm heap dump browser,用于分析heapmap文件,它会建立一个http/html服务器让用户可以在浏览器上查看分析结果。

    Sun JDK提供jhat与jmap搭配使用,来分析dump生成的堆快照。jhat内置了一个微型的HTTP/HTML服务器,生成dump文件的分析结果后,可以在浏览器中查看。

    用法举例: jhat test1.bin

    test1.bin为生成的dump文件。

    屏幕显示“Server is ready.”的提示后,用户在浏览器中键入http://localhost:7000就可以看到分析的结果了。

    分析结果默认是以包围单位进行分组显示,分析内存泄漏问题主要会使用到其中的“Heap Histogram”与OQL标签的功能。前者可以找到内存中总容量最大的对象。后者是标准的对象查询语言,使用类似SQL的语法对内存中的对象进行查询统计。

    1.6、jstack

    Stack trace for java ,显示虚拟机的线程快照。

    jstack命令用于生成虚拟机当前时刻的线程快照(一般称为threaddump或者javacore文件)。线程快照就是当前虚拟机内每一条线程正在执行的方法堆栈集合,生成线程快照的主要目的是定位线程出现长时间停顿的原因,如线程死锁、死循环、请求外部资源导致长时间等待等。

    jstack 格式 jstack [option] vmid

    option选项的合法值和具体含义

选项 作用
-F 当正常输出的请求不被响应时,强制输出线程堆栈
-l 除堆栈外,显示关于锁的附加信息
-m 如果调用到本地方法的话,可以显示c/c++的堆栈

Tread类新增了一个getAllStackTraces()方法用于获取虚拟机中所有的线程的StackTraceElement对象。

2.JDK的可视化工具

    2.1、JConsole

    JConsole工具在JDK/bin目录下,启动JConsole后,将自动搜索本机运行的jvm进程,不需要jps命令来查询指定。双击其中一个jvm进程即可开始监控,也可使用“远程进程”来连接远程服务器。

    2.2、VisualVm

    VisualVM是一个集成多个JDK命令行工具的可视化工具。VisualVM基于NetBeans平台开发,它具备了插件扩展功能的特性,通过插件的扩展,可用于显示虚拟机进程及进程的配置和环境信息(jps,jinfo),监视应用程序的CPU、GC、堆、方法区及线程的信息(jstat、jstack)等。VisualVM在JDK/bin目录下。

VisualVM的性能分析功能甚至比起JProfiler、YourKit等专业且收费的Profiling工具都不会逊色多少,而且VisualVM还有一个很大的优点:不需要被监视的程序基于特殊Agent运行,因此它对应用程序的实际性能的影响很小,使得它可以直接应用在生产环境中。这个优点是JProfiler、YourKit等工具无法与之媲美的。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326128170&siteId=291194637