Read the original
View Virtual Machine process: jps command
jps command lists all of the Java processes. If jps without any parameters, you can list the process ID Java programs as well as the short name of the Main function, as shown below.
$ jps
6540 Jps
64447 Main
In addition, the following parameters can also specify a custom output:
parameter | meaning |
---|---|
-q | Jps output only specified process ID |
-m | Output parameters passed to the Java process |
-l | The full path of the main function output |
-v | Display parameters passed to the Java Virtual Machine |
Virtual Machine statistics: jstat command
jstat used to observe details of the Java heap information, the basic syntax bit:
jstat -<option> [-t] [-h<lines>] <vmid> [<interval>] [<count>]]
Option which may be composed of the following values.
parameter | meaning |
---|---|
-class | Monitoring class loading, unloading amount of total space, and the time it takes to load the class |
-gc | Monitor Java heap conditions, including Eden District, two Survivor areas, the old era, such as permanent-generation capacity, used space, GC total time information |
-gccapacity | Monitor the content and -gc basically the same, but the output is mainly concerned about the use of Java heap each region to the maximum, minimum space |
-gcutil | Monitor the content and -gc basically the same, but the output has been focused on the use of the percentage of total space |
-gccause | Like -gcutil function, but will cause additional output generated by the last GC |
-gcnew | GC monitors the status of the new generation |
-gcnewcapacity | Monitor the content and -gcnew basically the same, using the output of major concern to the maximum, minimum space |
-gcold | GC old's condition monitoring |
-gcoldcapacity | Monitor the content and -gcold basically the same, using the output of major concern to the maximum, minimum space |
-gcpermcapacity | Generation output permanently to use the maximum, minimum space |
-compiler | Output JIT compiler compiled method, time-consuming information |
-printcompilation | Output has been JIT compiled methods |
-t parameter represents the output time stamps, -h parameter represents the output of a header after how many rows, vmid is a process virtual machine ID, interval and count represents the number of output and the output gap.
For example: We use a LVMID jstat command to monitor the JVM process 2365.
$ jstat -gcutil 2365
S0 S1 E O P YGC YGCT FGC FGCT GCT
0.00 0.00 12.05 0.00 14.17 0 0.000 0 0.000 0.000
In which the meaning of each option are as follows:
parameter | meaning |
---|---|
S0、S1 | He represents Survivor0, Survivor1, not been used. |
E | Eden district represents 12.05% of the use of space. |
O | It represents years old yet use. |
P | Permanent represents 14.17% of the generation of the use of space |
YUC, YGCT | Since the program runs indicate the occurrence of a total of 0 times Minor GC (YGC, Young GC), took a total of 0 seconds. |
FGC, FGCT | Since the program represents a total of 0 times to run it occurred Full GC (FGC, Full GC), took a total of 0 seconds. |
View virtual machine parameters: jinfo command
jinfo expansion parameter can be used to view a Java application running, even at run-time support, modify some parameters. Its basic syntax is:
jinfo <option> <pid>
Execution example, the query parameter values CMSInitiatingOccupancyFraction
$ jinfo -flag CMSInitiatingOccupancyFraction 2618
-XX:CMSInitiatingOccupancyFraction=-1
Export heap to file: jmap command
jmap is a versatile command, you can generate Java program Dump files, you can also view the statistics within the heap object instance to view the information ClassLoader and finalizer queue.
jmap [option] vmid
Apply example, an example of using jmap generate a running Eclipse snapshot of the dump file. The example in 2618 was queried by jps name LVMID.
$ jmap -dump:format=b,file=Desktop/dump.bin 2618
Dumping heap to /Users/yurongchan/Desktop/dump.bin ... Heap dump file created
Heap analysis tool: jhat command
jhat command is used to analyze Java application snapshot memory. Sun JDK provides jhat command jmap with the use, to analyze the heap dump snapshot jmap generated. After jhat built a miniature of HTTP / HTML server, generate analysis results dump files can be viewed in a browser. Here we use to analyze jhat generated above dump.bin file:
$ jhat dump.bin
Reading from dump.bin...
Dump file created Sun May 15 23:04:19 CST 2016 Snapshot read, resolving... Resolving 13822 objects... Chasing references, expect 2 dots.. Eliminating duplicate references.. Snapshot resolved. Started HTTP server on port 7000 Server is ready.
After you enter in the browser http: // localhost: 7000 you can see the results, as shown below:
But do not generally jhat order to analyze the dump file, mainly for two reasons: First, analysis of the dump file on the server generally will not deploy the application because analysis is a hardware resource consumption and process time-consuming. Another reason is the analysis jhat is still relatively simple, compared to the introduction of tools such as VisualVM behind a lot worse.
View thread stacks: jstack command
jstack command is used to derive a Java application thread's stack. jstack command format:
jstack [option] vmid
The following examples use jstack see a thread of battle:
nobody $ jstack -l 2618
2016-05-15 23:39:04 Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.79-b02 mixed mode): "Attach Listener" daemon prio=5 tid=0x00007f83228e6000 nid=0x280b waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "DestroyJavaVM" prio=5 tid=0x00007f832387e800 nid=0x1303 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None
Remote host information collection: jstatd command
jstad command is used to gather information on the remote host.
Multi-function command line: jcmd command
jcmd command can execute a command for a given Java virtual machine.
Performance statistics tools: hprof
- JVM foundation series begins: Why study the virtual machine?
- Lecture 1 JVM basic series: Java language Past and Present
- History Java Virtual Machine: JVM speak basic series 2
- Lecture Series 3 JVM basis: in the end what is a virtual machine?
- Lecture 4 JVM basic series: from source code to machine code, what happened?
- Lecture 5 JVM foundation Series: bytecode file structure
- Lecture Series 6 basis JVM: Java Virtual Machine Memory Architecture
- Lecture 7 JVM basic series: JVM class loading mechanism
- Lecture Series 8 basis JVM: JVM garbage collection mechanism
- Lecture Series 9 basis JVM: JVM garbage collector
- Lecture 10 JVM basic series: several types of garbage collection
- Lecture Series 11 basis JVM: JVM stack space of configuration parameters
- Lecture Series 12 basis JVM: JVM parameters of view JVM parameters
- Lecture Series 13 basis JVM: JVM parameters like the tracking information
- Lecture Series 14 JVM basis: GC log JVM parameters of configuration
- Lecture Series 15 basis JVM: JDK command performance monitoring
Read the original
View Virtual Machine process: jps command
jps command lists all of the Java processes. If jps without any parameters, you can list the process ID Java programs as well as the short name of the Main function, as shown below.
$ jps
6540 Jps
64447 Main
In addition, the following parameters can also specify a custom output:
parameter | meaning |
---|---|
-q | Jps output only specified process ID |
-m | Output parameters passed to the Java process |
-l | The full path of the main function output |
-v | Display parameters passed to the Java Virtual Machine |
Virtual Machine statistics: jstat command
jstat used to observe details of the Java heap information, the basic syntax bit:
jstat -<option> [-t] [-h<lines>] <vmid> [<interval>] [<count>]]
Option which may be composed of the following values.
parameter | meaning |
---|---|
-class | Monitoring class loading, unloading amount of total space, and the time it takes to load the class |
-gc | Monitor Java heap conditions, including Eden District, two Survivor areas, the old era, such as permanent-generation capacity, used space, GC total time information |
-gccapacity | 监视内容与-gc基本相同,但输出主要关注Java堆各个区域使用到的最大、最小空间 |
-gcutil | 监视内容与-gc基本相同,但输出主要关注已使用空间占总空间的百分比 |
-gccause | 与-gcutil功能一样,但是会额外输出导致上一次GC产生的原因 |
-gcnew | 监视新生代GC状况 |
-gcnewcapacity | 监视内容与-gcnew基本相同,输出主要关注使用到的最大、最小空间 |
-gcold | 监视老年代GC状况 |
-gcoldcapacity | 监视内容与-gcold基本相同,输出主要关注使用到的最大、最小空间 |
-gcpermcapacity | 输出永久代使用到的最大、最小空间 |
-compiler | 输出JIT编译器编译过的方法、耗时信息 |
-printcompilation | 输出已经被JIT编译的方法 |
-t 参数表示输出时间戳、-h 参数表示在多少行后输出一个表头、vmid 则是虚拟机的进程ID、interval 和 count 表示输出间隔以及输出次数。
例如:我们用jstat命令来监视一个LVMID为2365的JVM进程。
$ jstat -gcutil 2365
S0 S1 E O P YGC YGCT FGC FGCT GCT
0.00 0.00 12.05 0.00 14.17 0 0.000 0 0.000 0.000
其中每个选项的意义如下:
参数 | 含义 |
---|---|
S0、S1 | 表示Survivor0、Survivor1,还未使用。 |
E | 表示Eden区使用了12.05%的空间。 |
O | 表示老年代还未使用。 |
P | 表示永久代使用了14.17%的空间 |
YUC、YGCT | 表示从程序运行以来一共发生了0次Minor GC(YGC,Young GC),总共耗时0秒。 |
FGC、FGCT | 表示从程序运行以来一共发生了0次Full GC(FGC,Full GC),总共耗时0秒。 |
查看虚拟机参数:jinfo 命令
jinfo 可以用来查看正在运行的 Java 应用程序的扩展参数,甚至支持在运行时,修改部分参数。它的基本语法是:
jinfo <option> <pid>
执行例子,查询 CMSInitiatingOccupancyFraction 参数值
$ jinfo -flag CMSInitiatingOccupancyFraction 2618
-XX:CMSInitiatingOccupancyFraction=-1
导出堆到文件:jmap 命令
jmap 是一个多功能命令,可以生成 Java 程序的 Dump 文件,也可以查看堆内对象实例的统计信息、查看 ClassLoader 的信息以及 finalizer 队列。
jmap [option] vmid
执行样例,使用 jmap 生成一个正在运行的 Eclipse 的 dump 快照文件的例子。例子中的2618是通过jps名称查询到的LVMID。
$ jmap -dump:format=b,file=Desktop/dump.bin 2618
Dumping heap to /Users/yurongchan/Desktop/dump.bin ... Heap dump file created
堆分析工具:jhat 命令
jhat 命令用于分析 Java 应用的对快照内存。Sun JDK 提供了 jhat 命令与 jmap 搭配使用,来分析 jmap 生成的堆转储快照。jhat 内置了一个微型的 HTTP/HTML 服务器,生成 dump 文件的分析结果后,可以在浏览器中查看。下面我们用 jhat 来分析上面生成的 dump.bin 文件:
$ jhat dump.bin
Reading from dump.bin...
Dump file created Sun May 15 23:04:19 CST 2016 Snapshot read, resolving... Resolving 13822 objects... Chasing references, expect 2 dots.. Eliminating duplicate references.. Snapshot resolved. Started HTTP server on port 7000 Server is ready.
之后在浏览器输入 http://localhost:7000 就可以看到分析结果,如下图所示:
不过一般情况下不用 jhat 命令来分析 dump 文件,主要有以下两个原因:一是一般不会再部署应用的服务器上分析 dump 文件,因为分析工作是一个耗时而且消耗硬件资源的过程。另一个原因是 jhat 的分析功能还比较简陋,比起后面介绍的 VisualVM 等工具还差得很多。
查看线程堆栈:jstack 命令
jstack 命令用于导出 Java 应用程序的线程堆栈。jstack命令格式:
jstack [option] vmid
下面使用jstack查看一个线程对战的例子:
nobody $ jstack -l 2618
2016-05-15 23:39:04 Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.79-b02 mixed mode): "Attach Listener" daemon prio=5 tid=0x00007f83228e6000 nid=0x280b waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "DestroyJavaVM" prio=5 tid=0x00007f832387e800 nid=0x1303 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None
远程主机信息收集:jstatd 命令
jstad 命令用于收集远程主机信息。
多功能命令行:jcmd 命令
jcmd 命令可以针对给定的 Java 虚拟机执行一条命令。
性能统计工具:hprof
- JVM基础系列开篇:为什么要学虚拟机?
- JVM基础系列第1讲:Java 语言的前世今生
- JVM基础系列第2讲:Java 虚拟机的历史
- JVM基础系列第3讲:到底什么是虚拟机?
- JVM基础系列第4讲:从源代码到机器码,发生了什么?
- JVM基础系列第5讲:字节码文件结构
- JVM基础系列第6讲:Java虚拟机内存结构
- JVM基础系列第7讲:JVM类加载机制
- JVM基础系列第8讲:JVM 垃圾回收机制
- JVM基础系列第9讲:JVM垃圾回收器
- JVM基础系列第10讲:垃圾回收的几种类型
- JVM基础系列第11讲:JVM参数之堆栈空间配置
- JVM基础系列第12讲:JVM参数之查看JVM参数
- JVM基础系列第13讲:JVM参数之追踪类信息
- JVM基础系列第14讲:JVM参数之GC日志配置
- JVM基础系列第15讲:JDK性能监控命令