JVM 监控 - jstack jmap top 结合使用详解

jstack jmap top详解

这篇文章写到命令使用下面介绍三个命令结合使用情况详细说明

这里再介绍一下每个命令作用

  1. jstack 可查看java进程里的存活线程状态
  2. jmap 可查看java进程堆内存使用情况
  3. top linux 命令,可查看java某进程的线程

场景:我们要查看java进程使用cpu资源情况

  • 首先使用top命令查看占用cpu高的进程得到pid
    • 这里看到java进程45778进程cpu使用率相对较高,下面使用top -Hp 45778列出cpu使用率较高的线程
Processes: 276 total, 2 running, 274 sleeping, 1602 threads                                                                         16:55:50
Load Avg: 2.89, 5.07, 4.58  CPU usage: 8.73% user, 7.86% sys, 83.40% idle  SharedLibs: 104M resident, 36M data, 94M linkedit.
MemRegions: 95599 total, 2790M resident, 62M private, 702M shared. PhysMem: 8130M used (2043M wired), 61M unused.
VM: 1379G vsize, 1314M framework vsize, 6375177(64) swapins, 6965891(0) swapouts. Networks: packets: 11379450/2746M in, 10750239/1217M out.
Disks: 6755494/97G read, 3489504/81G written.

PID    COMMAND      %CPU TIME     #TH   #WQ  #PORTS MEM    PURG   CMPRS  PGRP  PPID  STATE    BOOSTS           %CPU_ME %CPU_OTHRS UID
45802  top          11.6 00:00.95 1/1   0    25     4392K  0B     0B     45802 44216 running  *0[1]            0.00000 0.00000    0
45794  sleep        0.0  00:00.00 1     0    10     240K   0B     0B     45029 45219 sleeping *0[1]            0.00000 0.00000    0
45793  syncdefaults 0.0  00:00.11 2     1    69     3232K  0B     0B     45793 1     sleeping *1[1]            0.00000 0.00000    501
45778  java         0.7  00:02.31 17    1    73     31M+   0B     0B     43822 43822 sleeping *0[1]            0.00000 0.00000    501
45777  java         0.0  00:07.49 19    1    77     112M   0B     0B     43822 43822 sleeping *0[1]            0.00000 0.00000    501
  • top -Hp 45778
Cpu(s):  0.2%us,  0.0%sy,  0.0%ni, 99.8%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   3921784k total,  3609916k used,   311868k free,   330264k buffers
Swap:  8191992k total,        0k used,  8191992k free,  1383484k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                      
 7348 root      20   0 3588m 1.0g  12m S  0.0 27.5   0:00.00 java                                                                                          
 7349 root      20   0 3588m 1.0g  12m S  0.0 27.5   0:01.02 java                                                                                          
 7350 root      20   0 3588m 1.0g  12m S  0.0 27.5   0:00.34 java                                                                                          
 7351 root      20   0 3588m 1.0g  12m S  0.0 27.5   0:00.40 java                                                                                          
 7352 root      20   0 3588m 1.0g  12m S  0.0 27.5   0:00.44 java                                                                                          
 7353 root      20   0 3588m 1.0g  12m S  0.0 27.5   0:00.05 java                                                                                          
 7354 root      20   0 3588m 1.0g  12m S  0.0 27.5   0:00.05 java                                                                                          
 7355 root      20   0 3588m 1.0g  12m S  0.0 27.5   0:00.00 java             

上面是在测试服务器拿下的信息,主要是看一下结果,PID 转换为16进制 printf "%x\n" 7348

  • jstack 45778 查看进程线程情况
hongyanyang:~ admin$ jstack 48766
2019-04-04 17:58:49
Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.80-b11 mixed mode):

"Attach Listener" daemon prio=5 tid=0x00007ffa262a2800 nid=0xf07 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Service Thread" daemon prio=5 tid=0x00007ffa261fd000 nid=0x4403 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" daemon prio=5 tid=0x00007ffa261fc800 nid=0x4603 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" daemon prio=5 tid=0x00007ffa259e3800 nid=0x4803 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Monitor Ctrl-Break" daemon prio=5 tid=0x00007ffa2720f800 nid=0x4903 runnable [0x000070000d4f0000]
   java.lang.Thread.State: RUNNABLE
	at java.net.SocketInputStream.socketRead0(Native Method)
	at java.net.SocketInputStream.read(SocketInputStream.java:152)
	at java.net.SocketInputStream.read(SocketInputStream.java:122)
	at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
	at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
	at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
	- locked <0x00000007d5b38b48> (a java.io.InputStreamReader)
	at java.io.InputStreamReader.read(InputStreamReader.java:184)
	at java.io.BufferedReader.fill(BufferedReader.java:154)
	at java.io.BufferedReader.readLine(BufferedReader.java:317)
	- locked <0x00000007d5b38b48> (a java.io.InputStreamReader)
	at java.io.BufferedReader.readLine(BufferedReader.java:382)
	at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64)

"Signal Dispatcher" daemon prio=5 tid=0x00007ffa26807800 nid=0x4b03 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=5 tid=0x00007ffa26809800 nid=0x3003 in Object.wait() [0x000070000d2ea000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <0x00000007d5504858> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
	- locked <0x00000007d5504858> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
	at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)

"Reference Handler" daemon prio=5 tid=0x00007ffa27015800 nid=0x2e03 in Object.wait() [0x000070000d1e7000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <0x00000007d5504470> (a java.lang.ref.Reference$Lock)
	at java.lang.Object.wait(Object.java:503)
	at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133)
	- locked <0x00000007d5504470> (a java.lang.ref.Reference$Lock)

"main" prio=5 tid=0x00007ffa27001800 nid=0x1903 waiting on condition [0x000070000cbd5000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at cn.creditease.mammon.TestMain.main(TestMain.java:15)

"VM Thread" prio=5 tid=0x00007ffa2582d000 nid=0x2c03 runnable 

"GC task thread#0 (ParallelGC)" prio=5 tid=0x00007ffa2700d800 nid=0x2207 runnable 

"GC task thread#1 (ParallelGC)" prio=5 tid=0x00007ffa2700e000 nid=0x2a03 runnable 

"GC task thread#2 (ParallelGC)" prio=5 tid=0x00007ffa2700e800 nid=0x5303 runnable 

"GC task thread#3 (ParallelGC)" prio=5 tid=0x00007ffa2600f800 nid=0x5103 runnable 

"VM Periodic Task Thread" prio=5 tid=0x00007ffa259f4000 nid=0x3703 waiting on condition 

JNI global references: 148
  • 线程状态
    • RUNNABLE,线程处于执行中
    • BLOCKED,线程被阻塞
    • WAITING,线程正在等待
    • TIMED_WAITING 指定时间等待
  • 信息main线程
    • nid 是线程nid 是16进制,上面说的printf "%x\n" xxxx 是为了找到对应的线程就是依据这个nid
    • 未完待续....
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yanghye

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值