通过java命令查看线程信息

内容源于工作中的总结和网络搜索。

  1. 命令找到java线程pid:jps,下面pid是6
root@5pcng:/# jps
6 jar
889 Jps
  1. 命令查看pid进程下的线程:ps -T -p pid
root@5pcng:/# ps -T -p 6
  PID  SPID TTY          TIME CMD
    6     6 ?        00:00:00 java
    6    37 ?        00:00:00 Service Thread
    6    38 ?        00:00:26 VM Periodic Tas
    6    40 ?        00:00:00 logback-1
    6    42 ?        00:00:01 mysql-cj-abando
    6    47 ?        00:00:01 redisson-netty-
    6    80 ?        00:00:03 quartzScheduler
    6   112 ?        00:00:00 MyHikariCP hous
    6   254 ?        00:00:02 pool-3-thread-1
    6   292 ?        00:00:02 pool-3-thread-2
    6   299 ?        00:00:01 pool-3-thread-3
    6   316 ?        00:00:01 pool-3-thread-4
    6   323 ?        00:00:01 pool-3-thread-5
    6   330 ?        00:00:01 pool-3-thread-6
    6   347 ?        00:00:01 pool-3-thread-7
    6   354 ?        00:00:00 pool-3-thread-8
    6   361 ?        00:00:01 pool-3-thread-9

只查看线程数可用:ps -T -p ${pid}| wc -l

root@5pcng:/# ps -T -p 6| wc -l
124

3.查看线程详细信息:jstack -l pid

root@5pcng:/# jstack -l 6
"C1 CompilerThread8" #13 daemon prio=9 os_prio=0 tid=0x00007fd90c2d0000 nid=0x21 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread7" #12 daemon prio=9 os_prio=0 tid=0x00007fd90c2ce000 nid=0x20 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread6" #11 daemon prio=9 os_prio=0 tid=0x00007fd90c2cc000 nid=0x1f waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread5" #10 daemon prio=9 os_prio=0 tid=0x00007fd90c2c9800 nid=0x1e waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread4" #9 daemon prio=9 os_prio=0 tid=0x00007fd90c2c7800 nid=0x1d waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread3" #8 daemon prio=9 os_prio=0 tid=0x00007fd90c2bd800 nid=0x1c waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
"VM Periodic Task Thread" os_prio=0 tid=0x00007fd90c2ee800 nid=0x26 waiting on condition 

JNI global references: 1955

4.查看jvm内存占用及垃圾回收情况:jstat -gcutil pid intervalTime(ms)

root@5pcng:/# jstat -gcutil 6 3000
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00  99.01  16.04   8.97  94.88      -     15    0.334     3    0.279    0.613
  0.00  99.01  16.04   8.97  94.88      -     15    0.334     3    0.279    0.613
  0.00  99.01  16.04   8.97  94.88      -     15    0.334     3    0.279    0.613
  0.00  99.01  16.04   8.97  94.88      -     15    0.334     3    0.279    0.613

指标说明

S0 : 新生代中Survivor space 0区已使用空间的百分比
S1: 新生代中Survivor space 1区已使用空间的百分比
E: 新生代已使用空间的百分比
O: 老年代已使用空间的百分比
M: 元数据区使用比例

CCS:压缩使用比例
YGC: 从应用程序启动到当前,发生Yang GC 的次数
YGCT: 从应用程序启动到当前,Yang GC所用的时间【单位秒】
FGC: 从应用程序启动到当前,发生Full GC的次数
FGCT: 从应用程序启动到当前,Full GC所用的时间
GCT: 从应用程序启动到当前,用于垃圾回收的总时间【单位秒】

jstat 选项

class:类加载统计(Displays statistics about the behavior of the class loader.)
compiler:编译统计(Displays statistics about the behavior of the Java HotSpot VM Just-in-Time compiler.)
gc:垃圾回收统计(Displays statistics about the behavior of the garbage collected heap.)
gccapacity:堆内存统计(Displays statistics about the capacities of the generations and their corresponding spaces.
gccause:垃圾回收原因(Displays a summary about garbage collection statistics (same as -gcutil), with the cause of the last and current (when applicable) garbage collection events.)
gcnew:新生代垃圾回收统计(Displays statistics of the behavior of the new generation.)
gcnewcapacity:新生代内存统计(Displays statistics about the sizes of the new generations and its corresponding spaces.)
gcold:老年代垃圾回收统计(Displays statistics about the behavior of the old generation and metaspace statistics.)
gcoldcapacity:老年代内存统计(Displays statistics about the sizes of the old generation.)
gcmetacapacity:元数据空间统计(Displays statistics about the sizes of the metaspace.)
gcutil:总结垃圾回收统计(Displays a summary about garbage collection statistics.)
printcompilation:JVM编译方法统计(Displays Java HotSpot VM compilation method statistics.)

gccause (推荐使用)
gcutil拥有的输出列gccause也都拥有,还额外多了最后一次GC原因和当前GC原因。

指标说明:
LGCC:最后一次GC原因(Cause of last garbage collection)
GCC:当前GC原因(Cause of current garbage collection)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容