提纲
JDK的命令行工具
1.进程状况查看工具
- jps -l输出主类的全名
- Jps -q省略主类的名称
- Jps -m输出进程启动时传递给主类main()函数的参数
- Jps -v输出进程启动时jvm的参数
2.虚拟机统计信息监视工具
jstat [ option vmid [interval(查询间隔)[s|ms] [count(次数)]] ]
例:
C:\Users\lenovo>jstat -class 18704 250 5(监视类装载Loaded、卸载数量Unloaded、总空间以及类装载所耗费的时间Time) Loaded Bytes Unloaded Bytes Time 21617 46751.3 30 46.8 144.20 21617 46751.3 30 46.8 144.20 21617 46751.3 30 46.8 144.20 21617 46751.3 30 46.8 144.20 21617 46751.3 30 46.8 144.20 C:\Users\lenovo>jstat -gc 18704 250 20(监视java堆情况,包括Eden区、两个survivor区、老年代、永久代的容量、已用空间、gc时间合计等信息。) S0C S1C S0U S1U EC EU OC OU PC PU YGC YGCT FGC FGCT GCT 31744.0 512.0 0.0 0.0 229376.0 83.0 349696.0 230084.0 187904.0 133283.5 45 1.144 11 7.977 9.121 C:\Users\lenovo>jstat -gccapacity 18704 250 2(输出主要关注java堆各个区域使用到的最大最小空间) NGCMN NGCMX NGC S0C S1C EC OGCMN OGCMX OGC OC PGCMN PGCMX PGC PC YGC FGC 175104.0 349696.0 286720.0 27648.0 28672.0 229376.0 349696.0 698880.0 349696.0 349696.0 21504.0 262144.0 167936.0 167936.0 48 14 175104.0 349696.0 286720.0 27648.0 28672.0 229376.0 349696.0 698880.0 349696.0 349696.0 21504.0 262144.0 167936.0 167936.0 48 14 C:\Users\lenovo>jstat -gcutil 18704 250 2(输出主要关注已使用空间站总空间的百分比) S0 S1 E O P YGC YGCT FGC FGCT GCT 0.00 0.00 0.37 57.38 81.83 49 1.218 15 10.599 11.817 0.00 0.00 0.37 57.38 81.83 49 1.218 15 10.599 11.817 C:\Users\lenovo>jstat -gccause 18704 250 2(额外输出导致上一次gc产生的原因) S0 S1 E O P YGC YGCT FGC FGCT GCT LGCC GCC 0.00 0.00 0.51 57.38 81.83 49 1.218 15 10.599 11.817 System.gc() No GC 0.00 0.00 0.51 57.38 81.83 49 1.218 15 10.599 11.817 System.gc() No GC C:\Users\lenovo>jstat -gcnew 18704 250 2监视新生代 S0C S1C S0U S1U TT MTT DSS EC EU YGC YGCT 27136.0 1024.0 0.0 0.0 1 15 27136.0 229376.0 1168.8 49 1.218 27136.0 1024.0 0.0 0.0 1 15 27136.0 229376.0 1168.8 49 1.218 C:\Users\lenovo>jstat -gcnewcapacity 18704 250 2 NGCMN NGCMX NGC S0CMX S0C S1CMX S1C ECMX EC YGC FGC 175104.0 349696.0 281600.0 116224.0 25088.0 116224.0 26112.0 348672.0 229376.0 50 16 175104.0 349696.0 281600.0 116224.0 25088.0 116224.0 26112.0 348672.0 229376.0 50 16 C:\Users\lenovo>jstat -gcold 18704 250 2监视老年代 PC PU OC OU YGC FGC FGCT GCT 158208.0 133229.8 349696.0 200472.1 50 16 10.943 12.179 158208.0 133229.8 349696.0 200472.1 50 16 10.943 12.179 C:\Users\lenovo>jstat -gcoldcapacity 18704 250 2 OGCMN OGCMX OGC OC YGC FGC FGCT GCT 349696.0 698880.0 349696.0 349696.0 50 16 10.943 12.179 349696.0 698880.0 349696.0 349696.0 50 16 10.943 12.179 C:\Users\lenovo>jstat -gcpermcapacity 18704 250 2输出永久代的最大、最小使用空间 PGCMN PGCMX PGC PC YGC FGC FGCT GCT 21504.0 262144.0 158208.0 158208.0 50 16 10.943 12.179 21504.0 262144.0 158208.0 158208.0 50 16 10.943 12.179 C:\Users\lenovo>jstat -compiler 18704 250 2输出jit编译器编译过的方法、耗时信息 Compiled Failed Invalid Time FailedType FailedMethod 3541 1 0 65.49 1 com/google/inject/spi/InjectionPoint getInjectionPoints 3541 1 0 65.49 1 com/google/inject/spi/InjectionPoint getInjectionPoints C:\Users\lenovo>jstat -printcompilation 18704 250 2输出已经被jit编译过的方法 Compiled Size Type Method 3541 79 1 org/eclipse/swt/widgets/Display checkDevice 3541 79 1 org/eclipse/swt/widgets/Display checkDevice
3.Java配置信息工具: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) where <option> is one of: -flag <name> toprint the value of the named VM flag -flag [+|-]<name> toenable or disable the named VM flag -flag <name>=<value> to set the named VM flag to the givenvalue -flags to print VMflags -sysprops to print Javasystem properties <no option> toprint both of the above -h | -help to print this help message
4.Java内存映像工具:jmap
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> toprint same info as Solaris pmap -heap to print javaheap summary -histo[:live] to printhistogram of java object heap; if the "live" suboption isspecified, only count live objects -permstat to printpermanent generation statistics -finalizerinfo to printinformation on objects awaiting finalization -dump:<dump-options> to dump java heap in hprof binary format dump-options: live dump only live objects; if notspecified, allobjects 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. Usewith -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 thishelp message -J<flag> to pass <flag> directly tothe runtime system
5.虚拟机堆转出快照分析工具:Jhat
Usage: jhat [-stack <bool>] [-refs<bool>] [-port <port>] [-baseline <file>] [-debug<int>] [-version] [-h|-help] <file> -J<flag> Pass <flag> directly to theruntime system. For example, -J-mx512m touse a maximum heap size of 512MB -stack false: Turn off tracking object allocation callstack. -refs false: Turn off tracking of references toobjects -port <port>: Set the port for the HTTP server. Defaults to 7000 -exclude <file>: Specify a file that lists data members thatshould be excluded from thereachableFrom query. -baseline <file>: Specify abaseline object dump. Objects in both heap dumps withthe same ID and same class will be marked as notbeing "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
6.Java对栈跟踪工具: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@]<remoteserver 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
7.JIT生成代码反汇编:HSDIS
步骤:
1.下载安装Cygwin;
进入select包时,安装如下包:
- Devel - make
- Devel - gcc-core
- Devel - mingw64-x86_64-gcc-core
- Utils - diffutils
扫描二维码关注公众号,回复:
1565268 查看本文章
2.下载beanutil与openjdk源码,并复制到如下目录
openjdk下载步骤:tags——>相应版本—>bz2
- 将下载的binutils、openJDK源码/src/share/tools/hsdi解压到~下
- 修改~/hsdis/Makefile文件,搜索LIBRARIES,将
LIBRARIES = $(TARGET_DIR)/bfd/libbfd.a \
$(TARGET_DIR)/opcodes/libopcodes.a \
$(TARGET_DIR)/libiberty/libiberty.a
修改为
LIBRARIES = $(TARGET_DIR)/bfd/libbfd.a \
$(TARGET_DIR)/zlib/libz.a \
$(TARGET_DIR)/opcodes/libopcodes.a \
$(TARGET_DIR)/libiberty/libiberty.a
保存 - 在Cygwin窗口输入cd ~/hsdis,然后输入命令make OS=Linux MINGW=x86_64-w64-mingw32 'AR=$(MINGW)-ar' BINUTILS=~/binutils-2.27(最后的binutils路径需要与实际安装路径一致)
- 编译完,hsdis-amd64.dll现在就在~/hsdis/build/Linux-amd64里面了,把它复制到$JAVA_HOME/jre/bin/server(或$JAVA_HOME/jre/bin/client下面。
示例:
package hsdis; /** * HSDIS:JIT生成反代码汇编 * java -XX:+UnlockDIagnosticVMOptions -XX:+PrintAssembly * -Xcomp -XX:CompileCommand=dontinline,*Bar.sum * -XXCompileCommand=compileonly,*Bar.sum hsdis.Bar * @author shiker * */ public class Bar { int a=1; static int b=2; public int sum(int c){ return a+b+c; } public static void main(String[] args) { new Bar().sum(3); } }
编译后:
D:\Program\demo\bin>java -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -Xcomp -XX:CompileCommand=dontinline,*.Bar.sum -XX:CompileCommand=compileonly,*Bar.sum demo.Bar Java HotSpot(TM) 64-Bit Server VM warning: PrintAssembly is enabled; turning on DebugNonSafepoints to gain additional output CompilerOracle: unrecognized line "dontinline * Bar sum" Unrecognized text after command: sum CompilerOracle: compileonly *Bar.sum Loaded disassembler from D:\jdk1.7\jre\bin\server\hsdis-amd64.dll Decoding compiled method 0x00000000032431d0: Code: [Disassembling for mach='i386:x86-64'] [Entry Point] [Constants] # {method} 'sum' '(I)I' in 'demo/Bar' # this: rdx:rdx = 'demo/Bar' # parm0: r8 = int # [sp+0x20] (sp of caller) 0x0000000003243300: mov 0x8(%rdx),%r10d 0x0000000003243304: shl $0x3,%r10 0x0000000003243308: cmp %r10,%rax 0x000000000324330b: jne 0x0000000003217a60 ; {runtime_call} 0x0000000003243311: data16 xchg %ax,%ax 0x0000000003243314: nopl 0x0(%rax,%rax,1) 0x000000000324331c: data16 data16 xchg %ax,%ax [Verified Entry Point] 0x0000000003243320: sub $0x18,%rsp 0x0000000003243327: mov %rbp,0x10(%rsp) ;*synchronization entry ; - demo.Bar::sum@-1 (line 9) 0x000000000324332c: movabs $0x7d5d9b0a8,%r10 ; {oop(a 'java/lang/Class' = 'demo/Bar')} 0x0000000003243336: mov 0x58(%r10),%r10d 0x000000000324333a: add 0xc(%rdx),%r10d 0x000000000324333e: mov %r8d,%eax 0x0000000003243341: add %r10d,%eax ;*iadd ; - demo.Bar::sum@9 (line 9) 0x0000000003243344: add $0x10,%rsp 0x0000000003243348: pop %rbp 0x0000000003243349: test %eax,-0x230334f(%rip) # 0x0000000000f40000 ; {poll_return} 0x000000000324334f: retq 0x0000000003243350: hlt 0x0000000003243351: hlt 0x0000000003243352: hlt 0x0000000003243353: hlt 0x0000000003243354: hlt 0x0000000003243355: hlt 0x0000000003243356: hlt 0x0000000003243357: hlt 0x0000000003243358: hlt 0x0000000003243359: hlt 0x000000000324335a: hlt 0x000000000324335b: hlt 0x000000000324335c: hlt 0x000000000324335d: hlt 0x000000000324335e: hlt 0x000000000324335f: hlt [Exception Handler] [Stub Code] 0x0000000003243360: jmpq 0x00000000032400a0 ; {no_reloc} [Deopt Handler Code] 0x0000000003243365: callq 0x000000000324336a 0x000000000324336a: subq $0x5,(%rsp) 0x000000000324336f: jmpq 0x0000000003219000 ; {runtime_call} 0x0000000003243374: hlt 0x0000000003243375: hlt 0x0000000003243376: hlt 0x0000000003243377: hlt
JDK的可视化工具
1.Java监视与管理控制台:JConsole
启动JConsole:JDK/bin/jconsole.exe
功能:内存监控、线程监控
2.多合一故障处理工具:VisualVM
Visual插件安装
功能:生成、浏览堆转储快照
分析程序性能
BTrace动态日志跟踪
官方插件下载地址:http://visualvm.github.io/pluginscenters.html