1、 jps(Java process status)
类似于windows下的任务管理器,把j去掉 ps就是Linux下进程管理
本地虚拟机唯一id : lvmid local virtual machine id
//接收程序运行时传入主类参数
jps -m
//显示运行时主类全名信息
jps -l
//接收虚拟机参数
jps -v
2、 jstat(监视虚拟机运行状态信息)
主要监视:类装载,内存,垃圾收集,jit编译的信息
依赖于jps,需要知道进程号
//jstat命令例子
jstat -gcutil 进程号 [间隔时间/ms] [打印次数/次]
3、 jinfo(实时查看和调整虚拟机各项参数)
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)
-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
4、 jmap(主要用于生成堆存储快照)
JVM Memory Map命令用于生成heap dump文件,如果不使用这个命令,还可以使用-XX:+HeapDumpOnOutOfMemoryError参数来让虚拟机出现OOM的时候自动生成dump文件
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
-clstats to print class loader 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
//jps查看pid
//jmap dump命令导出堆中信息
jmap -dump:format=b,file=c:\a.bin pid
参数
option:选项参数,不可同时使用多个选项参数
pid:java进程id,命令ps -ef | grep java获取
executable:产生核心dump的java可执行文件
core:需要打印配置信息的核心文件
remote-hostname-or-ip:远程调试的主机名或ip
server-id:可选的唯一id,如果相同的远程主机上运行了多台调试服务器,用此选项参数标识服务器
options参数
heap : 显示Java堆详细信息
histo : 显示堆中对象的统计信息
permstat :Java堆内存的永久保存区域的类加载器的统计信息
finalizerinfo : 显示在F-Queue队列等待Finalizer线程执行finalizer方法的对象
dump : 生成堆转储快照
F : 当-dump没有响应时,强制生成dump快照
5、 jhat(JVM heap Analysis Tool java虚拟机堆分析)
jmap用于导出堆快照存储信息,导出后用jhat进行分析
jhat是命令行工具,且资源占用比较大,一般用jmap导出堆快照信息后用可视化工具进行分析,jhat用的并不多
jhat内置了HTTP服务器
6、 jstack(生成虚拟机当前时刻的线程快照)
什么是线程快照?
答:当前虚拟机内每一条线程正在执行的方法堆栈的集合
线程快照的作用和目的?
答:定位线程出现长时间停顿的原因,如我们的code有多个定时任务,到了触发的条件或时间定时任务依然没有运行。在程序运行的过程中很难定位出现的问题,那么我们就可以通过jstack查看当前线程正在执行的方法堆栈集合信息。常见的有死锁、死循环、定时抓取新闻或数据,可以较准确定位程序在运行期间耗时过长的原因。
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:
//有时候-m,-l可能会卡住,当用-F时候强制信息打印出来
-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
还可以用jdk提供的API打印出当前线程正在执行方法的堆栈信息 Thread.getAllStackTraces();
package cn.itcats.jvm.tools;
import java.util.Map;
public class Test2 {
public static void main(String[] args) {
Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
for(Map.Entry<Thread, StackTraceElement[]> entry :allStackTraces.entrySet()) {
Thread key = entry.getKey();
StackTraceElement[] value = entry.getValue();
System.out.println("Thread name is"+ key.getName());
for(StackTraceElement e :value) {
System.err.println("\t"+e.toString());
}
}
}
}
打印结果
Thread name isReference Handler
java.lang.Object.wait(Native Method)
java.lang.Object.wait(Object.java:502)
java.lang.ref.Reference.tryHandlePending(Reference.java:191)
java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
Thread name ismain
Thread name isFinalizer
Thread name isSignal Dispatcher
java.lang.Thread.dumpThreads(Native Method)
java.lang.Thread.getAllStackTraces(Thread.java:1610)
cn.itcats.jvm.tools.Test2.main(Test2.java:7)
java.lang.Object.wait(Native Method)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)