Hope is a good thing, and maybe the best thing of all

编程不止是一份工作,还是一种乐趣!!!

Java常用命令行工具

查看java进程

jcmd -l #限当前用户
jps -l #限当前用户
ps -ef | grep java


线程栈Thread Dump

jstack ${pid} | more
jcmd ${pid} Thread.print | more


堆直方图

jmap -histo ${pid} | more #输出包括死对象
jmap -histo:live ${pid} | more #输出前会先执行full GC
jcmd ${pid} GC.class_histogram | more #输出前会先执行full GC


堆转储

jcmd ${pid} GC.heap_dump > ${file}.hprof
jmap -dump:live,file=${file}.hprof ${pid}


查看GC情况

jstat -gcutil ${pid} [interval [count]]

$ jstat -gcutil 13034
S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
50.05  50.05  0.00   0.80  54.78  56.93  39      0.038   0      0.000    0.038

字段说明:

  • S0: Survivor space 0 utilization as a percentage of the space’s current capacity.

  • S1: Survivor space 1 utilization as a percentage of the space’s current capacity.

  • E: Eden space utilization as a percentage of the space’s current capacity.

  • O: Old space utilization as a percentage of the space’s current capacity.

  • M: Metaspace utilization as a percentage of the space’s current capacity.

  • CCS: Compressed class space utilization as a percentage.

  • YGC: Number of young generation GC events.

  • YGCT: Young generation garbage collection time.

  • FGC: Number of full GC events.

  • FGCT: Full garbage collection time.

  • GCT: Total garbage collection time.

-gccause选项与-gcutil一样,但是会多显示两个字段。

$ jstat -gccause 13034 1s 100
S0     S1     E      O      M      CCS     YGC     YGCT    FGC    FGCT     GCT    LGCC                 GCC
50.05  0.00   39.49  0.86   54.79  56.93   2926    2.209     0    0.000    2.209  Allocation Failure   No GC
0.00   50.05  12.51  0.86   54.79  56.93   2929    2.211     0    0.000    2.211  Allocation Failure   No GC

字段说明:

  • LGCC: Cause of last garbage collection

  • GCC: Cause of current garbage collection


查到堆空间统计信息

jstat -gc ${pid} [interval [count]]

$ jstat -gc 13034
S0C    S1C    S0U    S1U  EC      EU  OC      OU    MC     MU     CCSC   CCSU  YGC  YGCT  FGC FGCT  GCT   
2048.0 2048.0 1025.0 0.0  8192.0  0.0 38912.0 336.1 4864.0 2664.8 512.0  291.5 1054 0.814 0   0.000 0.814

字段说明:

  • S0C: Current survivor space 0 capacity (kB).

  • S1C: Current survivor space 1 capacity (kB).

  • S0U: Survivor space 0 utilization (kB).

  • S1U: Survivor space 1 utilization (kB).

  • EC: Current eden space capacity (kB).

  • EU: Eden space utilization (kB).

  • OC: Current old space capacity (kB).

  • OU: Old space utilization (kB).

  • MC: Metaspace capacity (kB).

  • MU: Metacspace utilization (kB).

  • CCSC: Compressed class space capacity (kB).

  • CCSU: Compressed class space used (kB).

  • YGC: Number of young generation garbage collection events.

  • YGCT: Young generation garbage collection time.

  • FGC: Number of full GC events.

  • FGCT: Full garbage collection time.

  • GCT: Total garbage collection time.


JVM参数

jcmd ${pid} VM.flags
jinfo -flags ${pid} # 输出信息与jcmd ${pid} VM.flags一样,还包括jcmd ${pid} VM.command_line的信息
jinfo -flag ${参数名} ${pid} # 指定VM参数值
jinfo -flag [+|-]${参数名} ${pid} # 设置或取消指定M参数的布尔值
jinfo -flag ${参数名}=${参数值} ${pid} # 设置指定java虚拟机的参数的值。

### 示例 ###
$ jinfo -flag HeapDumpAfterFullGC 392 # 查询392进程的HeapDumpAfterFullGC值,可以看到是关闭状态
-XX:-HeapDumpAfterFullGC
$ jinfo -flag +HeapDumpAfterFullGC 392 #设置HeapDumpAfterFullGC状态为开启
$ jinfo -flag HeapDumpAfterFullGC 392 # 再次查看
-XX:+HeapDumpAfterFullGC
$ jinfo -flag -HeapDumpAfterFullGC 392 # 设置HeapDumpAfterFullGC状态为关闭
$ jinfo -flag HeapDumpAfterFullGC 392 # 再次查看
-XX:-HeapDumpAfterFullGC

### java -XX:+PrintFlagsFinal -version | grep manageable
### 查看哪些VM参数支持动态配置
$ java -XX:+PrintFlagsFinal -version | grep manageable
     intx CMSAbortablePrecleanWaitMillis            = 100                                 {manageable}
     intx CMSTriggerInterval                        = -1                                  {manageable}
     intx CMSWaitDuration                           = 2000                                {manageable}
     bool HeapDumpAfterFullGC                       = false                               {manageable}
     bool HeapDumpBeforeFullGC                      = false                               {manageable}
     bool HeapDumpOnOutOfMemoryError                = false                               {manageable}
    ccstr HeapDumpPath                              =                                     {manageable}
    uintx MaxHeapFreeRatio                          = 100                                 {manageable}
    uintx MinHeapFreeRatio                          = 0                                   {manageable}
     bool PrintClassHistogram                       = false                               {manageable}
     bool PrintClassHistogramAfterFullGC            = false                               {manageable}
     bool PrintClassHistogramBeforeFullGC           = false                               {manageable}
     bool PrintConcurrentLocks                      = false                               {manageable}
     bool PrintGC                                   = false                               {manageable}
     bool PrintGCDateStamps                         = false                               {manageable}
     bool PrintGCDetails                            = false                               {manageable}
     bool PrintGCID                                 = false                               {manageable}
     bool PrintGCTimeStamps                         = false                               {manageable}
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)


其它

jcmd ${pid} VM.version # 查看JVM版本
jcmd ${pid} VM.uptime # 查看JVM启动时间
jcmd ${pid} VM.system_properties # 查看JVM环境系统属性
jcmd ${pid} help # 该JVM支持的命令