Back to Java

See Also jstack

JDK Tools and Utilities

1. 简介(Based on Java SE 6/7)

JDK工具集中的命令行大多数是lib\tools.jar类库的一层薄包装而已,他们主要的功能代码是在tools类库中实现的。使用时需要一起部署tools.jar。

1.1. Standard JDK Tools and Utilities

1.2. Experimental JDK Tools and Utilities

在本节介绍的工具属于实验性功能,不提供支持且可能在未来的JDK版本里消失 //有趣的是,从5、6到7只有少量的“增加”却没有“消失”:)

1.3. Troubleshooting Documentation

2. 学习笔记

2.1. Basic Tools

2.1.1. Java Archive (JAR) Files

http://docs.oracle.com/javase/6/docs/technotes/guides/jar/index.html

# 查看
jar tf libs/volley-6.0.jar
# 创建
jar cf myFile.jar *.class
jar cf myFile.jar -C /tmp/src *.class
# 解压缩
jar xf libs/volley-6.0.jar

2.2. Monitoring Tools

http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/JavaJCMD/index.html

除了jvisualvm/jconsole之外,其余工具均为实验性功能

2.2.1. jvisualvm

2.2.2. jconsole

2.2.3. jcmd(since JDK 7) - JVM诊断工具,jps的增强版

## jcmd pid 对应参数
jcmd 10672 VM.flags

2.2.4. jps - 监控JVM进程状态

#显示当前所有java进程pid
jps -v

##找到需要查看的进程ID                                          
$ jps -lvm      
...             
7124  -Xms128M -Xmx512M -Dfile.encoding=UTF-8 -XstartOnFirstThread
...

2.2.5. jstat - 监控Java内存使用情况

利用JVM内建的指令对Java应用程序的资源和性能进行实时的命令行的监控,包括了对Heap size和垃圾回收状况的监控。

参考:

参数说明:

$ jstat -options
-class  class loader的统计信息
-compiler       编译器的统计信息
-gc     heap内存收集情况(输出参数见man jstat)GCT是总耗时
-gccapacity
-gccause
-gcnew
-gcnewcapacity
-gcold
-gcoldcapacity
-gcpermcapacity
-gcutil 
-printcompilation

2.2.5.1. Samples 1: jstat -gcutil

#查看jvm(1.8)的gc情况: 16624进程, 每60s刷新一次,共5次
$ jstat -gcutil 16624 60000 5
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00  62.50  25.13  20.69  98.45  97.39    763    4.558     1    0.040    4.598
  0.00  62.50  10.95  20.70  98.45  97.39    765    4.574     1    0.040    4.614
 62.50   0.00  96.40  20.71  98.45  97.39    766    4.582     1    0.040    4.622
 65.62   0.00  98.60  20.72  98.45  97.39    768    4.599     1    0.040    4.638
 71.88   0.00  94.93  20.73  98.45  97.39    770    4.616     1    0.040    4.655
#查看jvm(1.7)的gc情况: 22231进程, 每60s刷新一次,共5次
$ /usr/java/default/bin/jstat -gcutil 22231 60000 5
  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT   
 68.75   0.00  27.87  32.32  20.39   2894   32.381     0    0.000   32.381
 81.25   0.00  22.48  32.34  20.39   2896   32.411     0    0.000   32.411
  0.00  87.50  96.41  32.34  20.39   2897   32.429     0    0.000   32.429
  0.00  75.00  80.51  32.35  20.39   2899   32.459     0    0.000   32.459
  0.00  81.25  73.40  32.36  20.39   2901   32.489     0    0.000   32.489

2.2.5.2. Samples 2: jstat -class

## 
$ jstat -class 1555 10s 4
Loaded  Bytes  Unloaded  Bytes     Time   
  6181 13194.9        0     0.0      23.27
  6181 13194.9        0     0.0      23.27
  6229 13292.3        0     0.0      23.32
  6242 13312.7        0     0.0      23.34

2.2.6. jstatd - JVM jstat Daemon

jstat的Daemon,启动一个RMI服务端应用来监控本机所有的JVMs

使用参考:http://comeonbabye.iteye.com/blog/1463133

2.3. Troubleshooting Tools(实验性功能)

2.3.1. jinfo

需要root权限,显示进程启动的详细信息

jinfo -help
jinfo <pid>  

2.3.2. jmap - Memory Map

打印java的共享对象内存map和堆内存的细节

map [ option ] pid
-- 打印共享对象mapping
-dump:[live,]format=b,file= -- dump堆中的对象到文件
-finalizerinfo  打印等待回收对象的信息
-heap  打印堆总结
-histo[:live]  打印堆的对象统计,包括对象数、内存大小等等
-permstat  打印java堆perm区的classloader统计
-F  强制,在jmap -dump或jmap -histo中使用,如果pid没有相应的回复
-J  提供jvm选项,如:-J-Xms256m 

Samples:

##先找到需要查看的进程ID
$ jps -lvm
...
1555  -Xms128M -Xmx512M -Dfile.encoding=UTF-8 -XstartOnFirstThread
##打印堆对象统计:
$ jmap -histo 1555

 num     #instances         #bytes  class name
----------------------------------------------
   1:        208440       15192208  [C
   2:         18429       10284704  [I
   3:         66022        9791856  <constMethodKlass>
   4:         66022        8997008  <methodKlass>
   5:         79847        7314096  [Ljava.lang.Object;
   6:          6181        7095696  <constantPoolKlass>
   7:         11719        6738776  [B
   8:        205325        6570400  java.lang.String
   9:        113853        6375768  com.sun.org.apache.xerces.internal.dom.DeferredElementImpl
  10:        103684        5292424  <symbolKlass>

$ jmap -dump:live,format=b,file=heap.hprof 1555

2.3.3. jhat(堆分析工具)

是用来分析dump出来(kill -3或jmap)的java堆的命令,可以将堆中的对象以html的形式显示出来,包括对象的数量,大小等等,并支持对象查询语言

## find jvm pid
# jps -lvm
884 sun.tools.jps.Jps -lvm -Dapplication.home=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home -Xms8m
748  -Xms128M -Xmx512M -Dfile.encoding=UTF-8 -XstartOnFirstThread
## dump java heap
# jmap -dump:live,file=xmind.map 748
## launch jhat
# jhat -J-Xmx512m xmind.map 
Reading from xmind.map...
Dump file created Thu Feb 20 10:57:09 CST 2014
Snapshot read, resolving...
Resolving 1315241 objects...
... some warnign
.....................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.

jhat中的OQL(对象查询语言): http://blog.csdn.net/gtuu0123/article/details/6039592

2.3.4. jsadebugd

2.3.5. *jstack - Stack Trace

打印java线程的堆栈跟踪,可以得知哪些线程被阻塞或正等待,以便于查找原因; 如果java程序崩溃生成core文件,jstack工具可以用来获得core文件的java stack和native stack的信息,用来定位故障点以分析崩溃原因。

$ jstack 748
2014-02-20 11:46:19
Full thread dump Java HotSpot(TM) 64-Bit Server VM (20.65-b04-462 mixed mode):

"Attach Listener" daemon prio=9 tid=118cdb000 nid=0x1158d0000 waiting on condition [00000000]
   java.lang.Thread.State: RUNNABLE

"WaitToShowNotifications" daemon prio=1 tid=119807800 nid=0x118395000 waiting on condition [118394000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at net.xmind.workbench.internal.notification.SiteEventNotificationService$1.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:695)

...

"Worker-JM" prio=5 tid=1099a2800 nid=0x110720000 in Object.wait() [11071f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <7df14b430> (a java.util.ArrayList)
        at org.eclipse.core.internal.jobs.InternalWorker.run(InternalWorker.java:58)
        - locked <7df14b430> (a java.util.ArrayList)

"Framework Event Dispatcher" daemon prio=5 tid=10e037000 nid=0x110565000 in Object.wait() [110564000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <7df145d50> (a org.eclipse.osgi.framework.eventmgr.EventManager$EventThread)
        at java.lang.Object.wait(Object.java:485)
        at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.getNextEvent(EventManager.java:400)
        - locked <7df145d50> (a org.eclipse.osgi.framework.eventmgr.EventManager$EventThread)
        at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:336)

...

"Concurrent Mark-Sweep GC Thread" prio=9 tid=10587e000 nid=0x10d54a000 runnable 
"VM Periodic Task Thread" prio=10 tid=10e002800 nid=0x10eb07000 waiting on condition 

"Exception Catcher Thread" prio=10 tid=105801800 nid=0x104101000 runnable 
JNI global references: 2360

2.4. Scripting Tools(实验性功能)

java的脚本语言shell,以下命令检查当前支持的脚本

$ jrunscript -q
Language ECMAScript 1.6 implemention "Mozilla Rhino" 1.6 release 2
Language AppleScript 2.3 implemention "AppleScriptEngine" 1.1

该工具也可以解释执行其他脚本语言,需要配置对应语言的解释引擎,例如groovy,ruby,beanshell等:

$ jrunscript -e "print('hello world\n')"
hello world
$ jrunscript -e "cat('http://wiki.li3huo.com')"
<!DOCTYPE html>
<html lang="en">
...

3. Common Use Tools

4. Reference


CategoryTool

MainWiki: JDK_Tools_and_Utilities (last edited 2017-06-01 21:41:49 by twotwo)