We use the jstack from the JDK to analyze it. When Linux CPU is consumed by Java programs, the following process might help you:
1.top Find out which process consumes a high CPU
21125 co_ad2 0 1817m 776m 9712 S 3.3 4.9 12:03.24 java
5284 co_ad 0 3028m 2.5g 9432 S 1.0 16.3 6629:44 java
21994 MySQL 0 449m 88m 5072 S 1.0 0.6 67582:38 mysqld
8657 Co_sparr 0 2678m 892m 9220 S 0.3 5.7 103:06.13 Java
Here we analyze 21125 of this Java process.
2.top shift+h Find out which thread consumes a high CPU
Enter Top-p 21125 First, and then press SHIFT+H. This means that only 21125 of the process is viewed, and the thread is displayed.
21233 co_ad2 0 1807m 630m 9492 S 1.3 4.0 0:05.12 Java
20503 co_ad2_s 0 1360m 560m 9176 s 0.3 3.6 0:46.72 Java
21134 co_ad2 0 1807m 630m 9492 S 0.3 4.0 0:00.72 Java
22673 co_ad2 0 1807m 630m 9492 S 0.3 4.0 0:03.12 Java
Here we analyze 21233 of this thread, and note that this thread is part of the 21125 process.
3.jstack find information for this thread
Jstack [Process]|grep-a 10 [thread 16]
That
Java code
- Jstack 21125|grep-a 52f1
-A 10 means finding the following 10 rows of the row. 21233 Convert the calculator to 16 binary 52f1, note that the letter is lowercase.
Results:
Java code
- "http-8081-11" daemon prio=tid=0x00002aab049a1800 nid=0x52f1 in object.wait () [0x0000000042c75000]
- Java.lang.Thread.State:WAITING (on object monitor)
- At java.lang.Object.wait (Native Method)
- At Java.lang.Object.wait (Object.java:485)
- At Org.apache.tomcat.util.net.jioendpoint$worker.await (Jioendpoint.java:416)
You might be able to locate the offending code at once.
Using Jstack to analyze high CPU consumption issues