提高cpu的程序,做个无限循环即可
package bbb;
public class Te {
public static void main(String[] args) throws InterruptedException {
for (;;) {
System.out.println("================");
}
}
}
运行top -c查看使用cpu最高的程序,是java -jar bbb.jar,记住PID4325
[root@localhost ~]# top -c
top - 19:56:00 up 3 days, 5:45, 3 users, load average: 1.26, 0.80, 0.38
Tasks: 97 total, 3 running, 94 sleeping, 0 stopped, 0 zombie
%Cpu(s): 8.1 us, 40.9 sy, 0.0 ni, 51.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1863004 total, 195504 free, 308728 used, 1358772 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 1364848 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
4325 root 20 0 2455956 34968 11472 S 35.9 1.9 1:41.95 java -jar bbb.jar
4302 root 20 0 158904 5604 4260 R 13.0 0.3 0:36.19 sshd: root@pts/1
290 root 20 0 0 0 0 R 0.3 0.0 0:05.55 [xfsaild/sda3]
4170 root 20 0 0 0 0 S 0.3 0.0 0:14.12 [kworker/0:3]
4279 root 20 0 158904 5608 4260 S 0.3 0.3 0:00.35 sshd: root@pts/0
1 root 20 0 128036 6628 4160 S 0.0 0.4 0:04.21 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
2 root 20 0 0 0 0 S 0.0 0.0 0:00.04 [kthreadd]
4 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kworker/0:0H]
5 root 20 0 0 0 0 S 0.0 0.0 0:28.16 [kworker/u256:0]
6 root 20 0 0 0 0 S 0.0 0.0 0:19.05 [ksoftirqd/0]
7 root rt 0 0 0 0 S 0.0 0.0 0:00.00 [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 3:28.15 [rcu_sched]
10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [lru-add-drain]
11 root rt 0 0 0 0 S 0.0 0.0 0:03.81 [watchdog/0]
13 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [kdevtmpfs]
使用top -Hp查看4325的运行的线程
如下,发现线程4326使用cpu最高
[root@localhost ~]# top -Hp 4325
top - 19:57:18 up 3 days, 5:47, 3 users, load average: 0.86, 0.79, 0.41
Threads: 10 total, 0 running, 10 sleeping, 0 stopped, 0 zombie
%Cpu(s): 8.4 us, 41.1 sy, 0.0 ni, 50.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1863004 total, 195612 free, 308620 used, 1358772 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 1364956 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
4326 root 20 0 2455956 34968 11472 S 36.2 1.9 2:09.68 java
4325 root 20 0 2455956 34968 11472 S 0.0 1.9 0:00.02 java
4327 root 20 0 2455956 34968 11472 S 0.0 1.9 0:00.17 java
4328 root 20 0 2455956 34968 11472 S 0.0 1.9 0:00.00 java
4329 root 20 0 2455956 34968 11472 S 0.0 1.9 0:00.00 java
4330 root 20 0 2455956 34968 11472 S 0.0 1.9 0:00.00 java
4331 root 20 0 2455956 34968 11472 S 0.0 1.9 0:00.06 java
4332 root 20 0 2455956 34968 11472 S 0.0 1.9 0:00.01 java
4333 root 20 0 2455956 34968 11472 S 0.0 1.9 0:00.00 java
4334 root 20 0 2455956 34968 11472 S 0.0 1.9 0:00.19 java
用计算器计算4326的16进制是10E6
打印PID4325的栈
[root@localhost ~]# jstack -l 4325 >4325.stack
查询栈里面nid=0x10e6线程运行
[root@localhost ~]# cat 4325.stack | grep 10e6 -A 50
"main" #1 prio=5 os_prio=0 tid=0x00007fc048009800 nid=0x10e6 runnable [0x00007fc051c99000]
java.lang.Thread.State: RUNNABLE
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:326)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
- locked <0x00000000ed01d850> (a java.io.BufferedOutputStream)
at java.io.PrintStream.write(PrintStream.java:482)
- locked <0x00000000ed015380> (a java.io.PrintStream)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:104)
- locked <0x00000000ed015338> (a java.io.OutputStreamWriter)
at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:185)
at java.io.PrintStream.newLine(PrintStream.java:546)
- eliminated <0x00000000ed015380> (a java.io.PrintStream)
at java.io.PrintStream.println(PrintStream.java:807)
- locked <0x00000000ed015380> (a java.io.PrintStream)
at bbb.Te.main(Te.java:6)
Locked ownable synchronizers:
- None
"VM Thread" os_prio=0 tid=0x00007fc048095000 nid=0x10e7 runnable
"VM Periodic Task Thread" os_prio=0 tid=0x00007fc0480de000 nid=0x10ee waiting on condition
JNI global references: 5