文章目录
一、jps
(JVM Process Status Tool)
很多人都是用过linux系统里的ps
命令,这个命令主要是用来显示当前系统的进程情况,有哪些进程以及进程id(pid)。
ps -ef | grep 'java'
同样,jps
可以列出本机所有java进程的情况及进程id(pid)。
语法
[root@localhost bin]$ ./jps -help
illegal argument: --help
usage: jps [-help]
jps [-q] [-mlvV] [<hostid>]
Definitions:
<hostid>: <hostname>[:<port>]
- -q 仅输出VM标识符,不包括class name、jar name、arguments in main method
- -m 输出main method的参数
- -l 输出完全的包名,应用主类名,jar的完全路径名
- -v 输出jvm参数
- -V 输出通过flag文件传递到JVM中的参数(.hotspotrc文件或-XX:Flags=所指定的文件)
例子:
二、jinfo
(Java Configuration Info)
jinfo
是JDK自带的命令,可以用来查看正在运行的 java 应用程序的扩展参数,包括Java System属性和JVM命令行参数;也可以动态的修改正在运行的JVM一些参数。当系统崩溃时,jinfo
可以从core文件里面知道崩溃的Java应用程序的配置信息。
语法:
[root@localhost bin]$ ./jinfo -help
Usage:
jinfo [option] <pid>
(to connect to running process)
jinfo [option] <executable <core>
(to connect to a core file)
jinfo [option] [server_id@]<remote server IP or hostname>
(to connect to remote debug server)
where <option> is one of:
-flag <name> to print the value of the named VM flag
-flag [+|-]<name> to enable or disable the named VM flag
-flag <name>=<value> to set the named VM flag to the given value
-flags to print VM flags
-sysprops to print Java system properties
<no option> to print both of the above
-h | -help to print this help message
例子:
jinfo
具体用法示例参考:https://www.jianshu.com/p/8d8aef212b25
三、jstat
(JVM Statistics Monitoring Tool)
jstat
是用于监控虚拟机各种运行状态信息的命令行工具。他可以显示本地或远程虚拟机进程中的类装载、内存、垃圾收集、JIT编译等运行数据,在没有GUI图形的服务器上,它是运行期定位虚拟机性能问题的首选工具。Jstat是轻量级的、专门针对JVM的工具,非常适用。
语法:
[root@localhost bin]./jstat -help
Usage: jstat -help|-options
jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]
Definitions:
<option> An option reported by the -options option
<vmid> Virtual Machine Identifier. A vmid takes the following form:
<lvmid>[@<hostname>[:<port>]]
Where <lvmid> is the local vm identifier for the target
Java virtual machine, typically a process id; <hostname> is
the name of the host running the target Java virtual machine;
and <port> is the port number for the rmiregistry on the
target host. See the jvmstat documentation for a more complete
description of the Virtual Machine Identifier.
<lines> Number of samples between header lines.
<interval> Sampling interval. The following forms are allowed:
<n>["ms"|"s"]
Where <n> is an integer and the suffix specifies the units as
milliseconds("ms") or seconds("s"). The default units are "ms".
<count> Number of samples to take before terminating.
-J<flag> Pass <flag> directly to the runtime system.
- option 可以从下面参数中选择:
- -class 显示ClassLoad的相关信息;
- -compiler 显示JIT编译的相关信息;
- -gc 显示和gc相关的堆信息;
- -gccapacity 显示各个代的容量以及使用情况;
- -gccause 显示垃圾回收的相关信息(通-gcutil),同时显示最后一次或当前正在发生的垃圾回收的诱因;
- -gcnew 显示新生代信息;
- -gcnewcapacity 显示新生代大小和使用情况;
- -gcold 显示老年代和永久代的信息;
- -gcoldcapacity 显示老年代的大小;
- -gcpermcapacity 显示永久代的大小;
- -gcutil 显示垃圾收集信息;
- -printcompilation输出JIT编译的方法信息;
- -t 可以在打印的列加上Timestamp列,用于显示系统运行的时间
- -h 可以在周期性数据数据的时候,可以在指定输出多少行以后输出一次表头
- interval 执行每次的间隔时间,单位为毫秒
- count 用于指定输出多少次记录,缺省则会一直打印
例子:
jstat
具体用法示例参考:https://www.cnblogs.com/parryyang/p/5772484.html
四、jstack
(Java Stack Trace)
jstack用于生成java虚拟机当前时刻的线程快照。线程快照是当前java虚拟机内每一条线程正在执行的方法堆栈的集合,生成线程快照的主要目的是定位线程出现长时间停顿的原因,如线程间死锁、死循环、请求外部资源导致的长时间等待等。
语法:
./jstack -help
Usage:
jstack [-l] <pid>
(to connect to running process)
jstack -F [-m] [-l] <pid>
(to connect to a hung process)
jstack [-m] [-l] <executable> <core>
(to connect to a core file)
jstack [-m] [-l] [server_id@]<remote server IP or hostname>
(to connect to a remote debug server)
Options:
-F to force a thread dump. Use when jstack <pid> does not respond (process is hung)
-m to print both java and native frames (mixed mode)
-l long listing. Prints additional information about locks
-h or -help to print this help message
例子:
jstack
具体用法示例参考:https://www.jianshu.com/p/8d5782bc596e
五、jmap
(Java Memory Map)
jmap
可以获得运行中的jvm的堆的快照,从而可以离线分析堆,以检查内存泄漏,检查一些严重影响性能的大对象的创建,检查系统中什么对象最多,各种对象所占内存的大小等等。
语法:
[root@localhost bin]$ ./jmap -help
Usage:
jmap [option] <pid>
(to connect to running process)
jmap [option] <executable <core>
(to connect to a core file)
jmap [option] [server_id@]<remote server IP or hostname>
(to connect to remote debug server)
where <option> is one of:
<none> to print same info as Solaris pmap
-heap to print java heap summary
-histo[:live] to print histogram of java object heap; if the "live"
suboption is specified, only count live objects
-permstat to print permanent generation statistics
-finalizerinfo to print information on objects awaiting finalization
-dump:<dump-options> to dump java heap in hprof binary format
dump-options:
live dump only live objects; if not specified,
all objects in the heap are dumped.
format=b binary format
file=<file> dump heap to <file>
Example: jmap -dump:live,format=b,file=heap.bin <pid>
-F force. Use with -dump:<dump-options> <pid> or -histo
to force a heap dump or histogram when <pid> does not
respond. The "live" suboption is not supported
in this mode.
-h | -help to print this help message
-J<flag> to pass <flag> directly to the runtime system
例子:
jmap
具体用法示例参考:https://www.jb51.net/article/201435.htm
六、jhat
(Java Heap Analyse Tool)
jhat
,是一个用来分析java的堆情况的命令。之前的文章讲到过,使用jmap可以生成Java堆的Dump文件。生成dump文件之后就可以用jhat命令,将dump文件转成html的形式,然后通过http访问可以查看堆情况。
语法:
./jhat -help
Usage: jhat [-stack <bool>] [-refs <bool>] [-port <port>] [-baseline <file>] [-debug <int>] [-version] [-h|-help] <file>
-J<flag> Pass <flag> directly to the runtime system. For
example, -J-mx512m to use a maximum heap size of 512MB
-stack false: Turn off tracking object allocation call stack.
-refs false: Turn off tracking of references to objects
-port <port>: Set the port for the HTTP server. Defaults to 7000
-exclude <file>: Specify a file that lists data members that should
be excluded from the reachableFrom query.
-baseline <file>: Specify a baseline object dump. Objects in
both heap dumps with the same ID and same class will
be marked as not being "new".
-debug <int>: Set debug level.
0: No debug output
1: Debug hprof file parsing
2: Debug hprof file parsing, no server
-version Report version number
-h|-help Print this help and exit
<file> The file to read
For a dump file that contains multiple heap dumps,
you may specify which dump in the file
by appending "#<number>" to the file name, i.e. "foo.hprof#3".
All boolean options default to "true"