监控、故障处理工具

109 阅读5分钟

jps 虚拟机进程状况工具

查看当前本地运行的虚拟机进程

jstat otions pid

  • jps -l : 输出主类的全名,如果进程是jar包,输出jar路径
  • jps -v :输出虚拟机启动时的jvm参数

jstat 统计信息监视工具

jstat otions pid interval count

interval代表间隔,count代表次数。两参数可以不加,表示只查询一次

  • jstat -gc pid 监视java堆情况,已用空间、垃圾收集时间合计等信息
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU       CCSC     CCSU      YGC     YGCT    FGC     FGCT     GCT
 0.0    0.0    0.0    0.0   197632.0 60416.0   140288.0   101092.2  143052.0 135407.6 16844.0 15513.4     81    5.264     2      2.354    7.617
YGC平均耗时: 5.264 / 81 = 65ms
FGC平均耗时: 2.354 /2 = 1177ms
  • jstat -gcutil pid 1000 10 同-gc,输出主要关注已使用空间占总空间百分比
1秒统计一次 统计10次 

  S0     S1     E      O      M     CCS      YGC     YGCT    FGC             FGCT     GCT
  0.00 100.00  60.92  75.62  93.40  91.53    156    8.969     2              2.354   11.323
  0.00 100.00  61.49  75.62  93.40  91.53    156    8.969     2              2.354   11.323
  0.00 100.00  61.49  75.62  93.40  91.53    156    8.969     2              2.354   11.323
  0.00 100.00  61.49  75.62  93.40  91.53    156    8.969     2              2.354   11.323
 from  to     edan   old     meta           YGC次数 YGC时间   fullgc次数    fullfc时间  gc总时间    
 (前4个是所占比例)
  • jstat -class pid 监视类加载、卸载消耗空间、时间
Loaded  Bytes  Unloaded  Bytes     Time
23227 44577.0      374   556.2     117.78
  • jstat -gccause pid 最近一次gc的原因

jmap java内存映像工具

用于生成堆内存快照(heapdump)

  • jmap 指南 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>               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

  • jmap -heap [pid] : 显示堆的详细信息

jmap -heap 14618


Server compiler detected.
JVM version is 25.221-b11

using thread-local object allocation.
Parallel GC with 2 thread(s)

Heap Configuration:
   MinHeapFreeRatio         = 40
   MaxHeapFreeRatio         = 70
   MaxHeapSize              = 536870912 (512.0MB)               #最大堆
   NewSize                  = 1363144 (1.2999954223632812MB)    #新生代分配大小
   MaxNewSize               = 321912832 (307.0MB)               #最大可分配新生代大小
   OldSize                  = 5452592 (5.1999969482421875MB)
   NewRatio                 = 2                               
   SurvivorRatio            = 8
   MetaspaceSize            = 21807104 (20.796875MB)
   CompressedClassSpaceSize = 201326592 (192.0MB)
   MaxMetaspaceSize         = 209715200 (200.0MB)               #最大可分配元数据区大小
   G1HeapRegionSize         = 1048576 (1.0MB)   

Heap Usage:
G1 Heap:
   regions  = 512
   capacity = 536870912 (512.0MB)
   used     = 329252848 (313.99998474121094MB)
   free     = 207618064 (198.00001525878906MB)
   61.32812201976776% used
G1 Young Generation:
Eden Space:
   regions  = 139
   capacity = 173015040 (165.0MB)
   used     = 145752064 (139.0MB)
   free     = 27262976 (26.0MB)
   84.24242424242425% used
Survivor Space:
   regions  = 10
   capacity = 10485760 (10.0MB)
   used     = 10485760 (10.0MB)
   free     = 0 (0.0MB)
   100.0% used
G1 Old Generation:
   regions  = 165
   capacity = 219152384 (209.0MB)
   used     = 173015024 (164.99998474121094MB)
   free     = 46137360 (44.00001525878906MB)
   78.94736112019662% used
  • jmap -histo[:live] [pid] 打印对象实例数与大小
打印每个class的实例数目,内存占用,类全名信息.live子参数加上后,只统计活的对象数量,即先触发fulldc后再统计

jmap -histo:live 14618 |sort -nr

   7:         73030        2336960  java.util.HashMap$Node
   6:         71070        2842800  java.util.LinkedHashMap$Entry
   5:         10877        4222144  [B
   4:        164528        5264896  java.util.concurrent.ConcurrentHashMap$Node
   3:        257980        6191520  java.lang.String
   2:         93004        8184352  java.lang.reflect.Method
   1:        260104       30775560  [C
Total       1991557      101281216
 num     #instances         #bytes  class name
  • jmap -dump [pid] 将堆中对象dump到文件
live 只dump存活的对象,如果不加则会dump所有对象
format=b 表示以二进制格式
file=filepath 输出到某个文件中

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

jstack java堆栈跟踪工具

用于生成当前时刻的线程快照(threaddump / javacore),定位线程长时间停顿如思索、死循环、请求外部资源导致长时间挂起等。

  • jstack -l -F [pid]
查看是否有死锁信息
:jstack -l -F 14618
    Attaching to process ID 14618, please wait...
    Debugger attached successfully.
    Server compiler detected.
    JVM version is 25.221-b11
    Deadlock Detection:

    No deadlocks found.
  • jstack [pid] > xxx.txt
导出线程dump到文件
  • top -H -p [pid] 查看进程内线程资源
  • printf "%x" [pid] 根据线程dump 精确查找线程用到
nid:nid是线程的唯一标识符,是16进制的,通常用于定位某一个线程。

线程状态

java.lang.Thread.State:线程的状态标识。

NEW 未启动的新线程

RUNNABLE 正在运行的线程

BLOCKED 阻塞状态,进入同步方法或同步代码块,没有获取到锁,则会进入该状态

WAITING 等待状态

thread.join()  ,obj.wait() ,LockSupport.park()  等情况,表明线程正等待某个资源来唤醒自己  

TIMED_WAITING 有时间的等待状态

Thread.sleep(long)、thread.join(long),obj.wait(long),LockSupport#parkNanos() ...

TERMINATED 线程已退出 以上对应 java.lang.Thread.State 枚举类

其他jstack 堆栈信息名词

Dead 线程执行完毕,或者抛出了未捕获的异常之后,会进入dead状态,表示该线程结束

Deadlock 表示有死锁

Waiting on condition 等待某个资源或条件发生来唤醒自己。

Waiting on monitor entry 在等待获取锁

in Object.wait() 获得了monitor之后,又调用了obj.wait() 方法,放弃 Monitor,进入 “Wait Set”队列

Entry Set中等待的线程是 “Waiting for monitor entry”,而在 Wait Set中等待的线程是 “in Object.wait()”

可视化工具分析

1.www.ej-technologies.com/products/jp… 下载 Jprofiler,傻瓜式安装, 一个邮箱10天免费使用

2.Java\jdk1.8.0_73\bin\jvisualvm.exe 使用jvisualvm进行分析

插件:BTrace动态日志跟踪 通过HotSpot虚拟机的 Instrument功能动态加入原本不存在的调试代码。 (arthas也是基于此)