在 java 的 bin 目录下,jdk 提供了很多使用的工具,下面学习一些监控和故障处理的工具。
| 名称 | 作用 |
|---|---|
| jps | JVM process status tool,显示指定系统内所有的 HotSpot 虚拟机进程 |
| jstat | JVM statistics monitoring tool,用于收集 HotSpot 虚拟机各方面的运行数据 |
| jinfo | 显示虚拟机配置信息 |
| jmap | 生产虚拟机的内存快照 dump 文件 |
| jhat | 分析 dump 文件 |
| jstack | 显示虚拟机的线程快照 |
jps 虚拟机进程状况工具
jps 的命令格式:
jps [options] [hostid]
1 2 3 4 5 6 7 | >jps -l25330sun.tools.jps.Jps25296>jps -lv25356sun.tools.jps.Jps -Dapplication.home=/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home -Xms8m25296-Dosgi.requiredJavaVersion=1.6-XstartOnFirstThread -Dorg.<span class="wp_keywordlink"><a href="http://res.importnew.com/eclipse"title="Eclipse ImportNew主页"target="_blank">Eclipse</a></span>.swt.internal.carbon.smallFonts -XX:MaxPermSize=256m -Xms40m -Xmx512m -Xdock:icon=../Resources/Eclipse.icns -XstartOnFirstThread -Dorg.eclipse.swt.internal.carbon.smallFonts |
jps 可以查看通过 rmi 协议查询开启了 rmi 服务的原创虚拟机进程状态,hostid 是 rmi 注册表中注册的主机。
jps 有如下主要的选项:
| 选项 | 作用 |
|---|---|
| -q | 只输出 LVMID,省略主类的名称 |
| -m | 输出虚拟机启动时候传递给 main 方法的参数 |
| -l | 输出类的全名 |
| -v | 输出虚拟机进程启动时 JVM 参数 |
jstat 虚拟机统计信息监视工具
jstat 可以显示本地或者远程虚拟机进程中的类装载、内存、垃圾收集、 JIT 编译等运行数据。
jstat 的命令格式:
jstat [option vmid [interval] [count]]
例如:
1 2 3 4 5 6 7 | >jstat -gcutil 2529610005 S0 S1 E O P YGC YGCT FGC FGCT GCT 0.0099.5490.4393.7095.23551.15651.9903.146 0.0099.5490.4393.7095.23551.15651.9903.146 0.0099.5490.4393.7095.23551.15651.9903.146 0.0099.5490.4393.7095.23551.15651.9903.146 0.0099.5490.4393.7095.23551.15651.9903.146 |
查询 25296 进程的虚拟机状况,并且每隔 1000 毫秒一次,显示 5 次。
看下主要选项的含义:
| 选项 | 作用 |
|---|---|
| -class | 监视类装载、卸载数量、总看见以及类装载消耗的时间 |
| -gc | 监视 java 堆状况,包括 eden 区、两个 survivor 区、年老代、永久代等的容量、已用空间、gc 时间合计等 |
| -gccapacity | 内容与 -gc 基本相同,输出主要关注 java 堆各个区使用到的最大、最小空间 |
| -gcutil | 内容与 -gc 基本相同,关注已使用区域占总空间的百分比 |
| -gccause | 内容与 -gcutil 一样,并且多输出导致上一次 gc 产生的原因 |
| -gcnew | 监视新生代状况 |
| -gcnewcapacity | 与 -gcnew 相同,主要关注使用到的最大、最小空间 |
| -compiler | 输出 JIT 编译器编译过的方法、耗时等信息 |
下面解读下 -gcutil 所产生的内容:
S0、S1 分别代表了 Survivor0 和 Survivor1,E 代表 Eden 区,O 代表老年区, P 代表永久代。YGC 代表 Young GC 的次数,YGCT 代表时间,后面一样解释。
jinfo 查看 java 配置信息工具
这个命令比较简单,直接看自带的描述:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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> isone 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 setthe 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 thishelp message |
jmap 生产 java 内存 dump
jmap 除了可以生成 dump 文件外,还可以查询 finalize 执行队列,java 堆和永久代的详细信息,如空间使用率和当前用的是哪种收集器等。
具体的 jamp 如何操作部多介绍,看下面提供的说明,比较简单:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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> isone of: <none> to print same info asSolaris pmap -heap to print java heap summary -histo[:live] to print histogram of java object heap; ifthe "live" suboption isspecified, 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 inhprof binary format dump-options: live dump only live objects; ifnot specified, all objects inthe 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 isnot supported inthismode. -h | -help to print thishelp message -J<flag> to pass <flag> directly to the runtime system |
jhat 虚拟机堆快照分析工具
我们可以使用 jhat 来分析 jmap 生成的 dump 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 | >jhat tmp.dumpReading from tmp.dump...Dump file created Sat May 0917:10:52CST 2015Snapshot read, resolving...Resolving 0objects...WARNING: hprof file does not includejava.lang.Class!WARNING: hprof file does not includejava.lang.String!WARNING: hprof file does not includejava.lang.ClassLoader!Chasing references, expect 0dotsEliminating duplicate referencesSnapshot resolved.Started HTTP server on port 7000Server isready. |
默认会开 7000 端口进行 web 访问。一般不使用这个命令来分析,会使用专业的工具来分析 dump 文件,如 eclipse memory analyzer 等。
jstack 分析 java 堆栈
jstack 用来生成当前时刻线程快照。
使用方式如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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 ishung) -m to print both java and nativeframes (mixed mode) -l long listing. Prints additional information about locks -h or -help to print thishelp message |
文章转载自程序员达叔,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




