java占用CPU分析

找到对应进程的PID

[root@master1 rundeck]# top

top - 03:10:32 up  8:06,  2 users,  load average: 0.08, 0.03, 0.05

Tasks: 243 total,  2 running, 241 sleeping,  0 stopped,  0 zombie

%Cpu(s):  0.1 us,  0.1 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

KiB Mem :  3874956 total,  383964 free,  1264232 used,  2226760 buff/cache

KiB Swap:  4063228 total,  4063228 free,        0 used.  2329668 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM    TIME+ COMMAND           

25136 root      20  0 4769720 940148  16168 S  0.6 24.3  3:56.49 java               

25389 root      20  0  148200  2128  1420 R  0.6  0.1  0:00.07 top               

    1 root      20  0  43388  3644  2352 S  0.0  0.1  0:04.88 systemd           

    2 root      20  0      0      0      0 S  0.0  0.0  0:00.03 kthreadd           

    3 root      20  0      0      0      0 S  0.0  0.0  0:03.43 ksoftirqd/0       

    5 root      0 -20      0      0      0 S  0.0  0.0  0:00.00 kworker/0:0H       

    7 root      rt  0      0      0      0 S  0.0  0.0  0:00.14 migration/0       

    8 root      20  0      0      0      0 S  0.0  0.0  0:00.00 rcu_bh             

    9 root      20  0      0      0      0 S  0.0  0.0  0:00.00 rcuob/0           

  10 root      20  0      0      0      0 S  0.0  0.0  0:00.00 rcuob/1           

  11 root      20  0      0      0      0 S  0.0  0.0  0:00.00 rcuob/2           

  12 root      20  0      0      0      0 S  0.0  0.0  0:00.00 rcuob/3           

  13 root      20  0      0      0      0 S  0.0  0.0  0:00.00 rcuob/4       

显示线程列表,同时按照CPU占用高的线程排序

[root@master1 rundeck]# ps -mp 25136 -o THREAD,tid,time |sort -rn

USER    %CPU PRI SCNT WCHAN  USER SYSTEM  TID    TIME

root      5.9  -    - -        -      -    - 00:03:56

root      1.5  19    - futex_    -      - 25148 00:01:01

root      1.5  19    - futex_    -      - 25147 00:01:01

root      1.5  19    - futex_    -      - 25138 00:01:01

root      0.2  19    - futex_    -      - 25151 00:00:08

root      0.2  19    - futex_    -      - 25149 00:00:11

root      0.0  19    - inet_c    -      - 25166 00:00:00

root      0.0  19    - futex_    -      - 25195 00:00:00

root      0.0  19    - futex_    -      - 25194 00:00:00

root      0.0  19    - futex_    -      - 25193 00:00:00

root      0.0  19    - futex_    -      - 25188 00:00:00

root      0.0  19    - futex_    -      - 25187 00:00:01

root      0.0  19    - futex_    -      - 25186 00:00:01

root      0.0  19    - futex_    -      - 25185 00:00:01

root      0.0  19    - futex_    -      - 25184 00:00:00

root      0.0  19    - futex_    -      - 25183 00:00:00

root      0.0  19    - futex_    -      - 25182 00:00:00

root      0.0  19    - futex_    -      - 25181 00:00:01

root      0.0  19    - futex_    -      - 25180 00:00:00

root      0.0  19    - futex_    -      - 25179 00:00:00

root      0.0  19    - futex_    -      - 25178 00:00:01

root      0.0  19    - futex_    -      - 25177 00:00:00

root      0.0  19    - futex_    -      - 25176 00:00:00

root      0.0  19    - futex_    -      - 25175 00:00:00

root      0.0  19    - futex_    -      - 25174 00:00:00

root      0.0  19    - futex_    -      - 25173 00:00:00

root      0.0  19    - futex_    -      - 25172 00:00:00

root      0.0  19    - futex_    -      - 25171 00:00:00

root      0.0  19    - futex_    -      - 25170 00:00:03

root      0.0  19    - futex_    -      - 25168 00:00:00

root      0.0  19    - futex_    -      - 25165 00:00:00

root      0.0  19    - futex_    -      - 25163 00:00:00

root      0.0  19    - futex_    -      - 25162 00:00:00

root      0.0  19    - futex_    -      - 25161 00:00:00

root      0.0  19    - futex_    -      - 25160 00:00:00

root      0.0  19    - futex_    -      - 25158 00:00:00

root      0.0  19    - futex_    -      - 25155 00:00:00

root      0.0  19    - futex_    -      - 25154 00:00:01

root      0.0  19    - futex_    -      - 25150 00:00:00

root      0.0  19    - futex_    -      - 25146 00:00:00

root      0.0  19    - futex_    -      - 25145 00:00:00

root      0.0  19    - futex_    -      - 25144 00:00:00

root      0.0  19    - futex_    -      - 25143 00:00:01

root      0.0  19    - futex_    -      - 25142 00:00:03

root      0.0  19    - futex_    -      - 25141 00:00:03

root      0.0  19    - futex_    -      - 25140 00:00:03

root      0.0  19    - futex_    -      - 25139 00:00:03

root      0.0  19    - futex_    -      - 25136 00:00:00

root      0.0  19    - ep_pol    -      - 25167 00:00:00

root      0.0  19    - ep_pol    -      - 25164 00:00:00

将线程的PID转换为16进制

[root@master1 rundeck]# printf "%x\n" 25148

623c

使用jstack打印该进程下面的此线程的堆栈信息

[root@master1 rundeck]# jstack 25136 | grep "623c" -A 30

"C2 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007ff4a41e6800 nid=0x623c waiting on condition [0x0000000000000000]

  java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007ff4a41d7000 nid=0x623b waiting on condition [0x0000000000000000]

  java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007ff4a41d5000 nid=0x623a runnable [0x0000000000000000]

  java.lang.Thread.State: RUNNABLE

"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007ff4a41ab000 nid=0x6239 in Object.wait() [0x00007ff4941f0000]

  java.lang.Thread.State: WAITING (on object monitor)

at java.lang.Object.wait(Native Method)

at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)

- locked <0x00000000c000d3d8> (a java.lang.ref.ReferenceQueue$Lock)

at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)

at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)

"Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007ff4a41a6000 nid=0x6238 in Object.wait() [0x00007ff4942f1000]

  java.lang.Thread.State: WAITING (on object monitor)

at java.lang.Object.wait(Native Method)

at java.lang.Object.wait(Object.java:502)

at java.lang.ref.Reference.tryHandlePending(Reference.java:191)

- locked <0x00000000c000d590> (a java.lang.ref.Reference$Lock)

at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

"VM Thread" os_prio=0 tid=0x00007ff4a419c800 nid=0x6237 runnable

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007ff4a405e000 nid=0x6233 runnable

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007ff4a4060000 nid=0x6234 runnable

打印GC 信息

[root@master1 rundeck]# jstat -gcutil 25136 200 50

  S0    S1    E      O      M    CCS    YGC    YGCT    FGC    FGCT    GCT 

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

52.90  0.00  18.26  60.04  96.67  91.29    60    2.571    4    2.037    4.608

导出内存快照

[root@master1 rundeck]# jmap -dump:live,format=b,file=dump.hprof 25136

Dumping heap to /home/rundeck/rundeck/dump.hprof ...

Heap dump file created

寻找合适的工具分析内存。

你可能感兴趣的:(java占用CPU分析)