Identify excessive CPU usage in Java code

Source: Internet
Author: User

When a Java process consumes too much CPU, it can be a logical problem. How do I troubleshoot the problem?

1. Use the top tool to list all processes, SHITF + p lists high CPU usage processes

2. Find the problem process number, use Top-h-p PID to list all threads of the process

3. Then SHIFT + P sort by CPU usage

4. Identify the problem process number, print out its 16 binary value using Python, printing ("0x"% ppid), for example: 76A3

5. Jstack pid > T.dat record thread Stack, vi open Find 76A3 thread number, combined with source location problem

The following example of a dead loop is used to explain:

2015-07-26 19:52:04full thread dump OpenJDK 64-bit Server VM (20.0-b11 mixed mode): "Attach Listener" daemon prio=10 tid=0x 00007fa04c001000 nid=0x7ac9 waiting on condition [0x0000000000000000] Java.lang.Thread.State:RUNNABLE "DESTROYJAVAVM" prio=10 tid=0x00007fa070007000 nid=0x7697 waiting on condition [0x0000000000000000] Java.lang.Thread.State:RUNNABLE " Ttwo "prio=10 tid=0x00007fa0700b2000 nid=0x76a3 runnable [0x00007fa074260000] Java.lang.Thread.State:RUNNABLEat main$ T2.run (main.java:31) at Java.lang.Thread.run (thread.java:679) "tone" prio=10 tid=0x00007fa0700b0000 nid=0x76a2 Waiting on condition [0x00007fa074361000] Java.lang.Thread.State:TIMED_WAITING (sleeping) at Java.lang.Thread.sleep ( Native Method) at Main$t1.run (main.java:19) @ java.lang.Thread.run (thread.java:679) "Low Memory Detector" Daemon prio= Ten tid=0x00007fa070098800 nid=0x76a0 runnable [0x0000000000000000] Java.lang.Thread.State:RUNNABLE "C2 CompilerThread1 "Daemon prio=10 tid=0x00007fa070096000 nid=0x769f Waiting on condition [0x0000000000000000] Java.lang.Thread.State:RUNNABLE "C2 CompilerThread0" daemon prio=10 tid=0x00007fa0 70093800 nid=0x769e waiting on condition [0x0000000000000000] Java.lang.Thread.State:RUNNABLE "Signal Dispatcher" Daemo n prio=10 tid=0x00007fa070085000 nid=0x769d runnable [0x0000000000000000] Java.lang.Thread.State:RUNNABLE "Finalizer" Daemon prio=10 tid=0x00007fa070073000 nid=0x769c in object.wait () [0x00007fa074973000] Java.lang.Thread.State:WAITING (on object monitor) at java.lang.Object.wait (Native Method) – Waiting on <0x00000000ec0b1310> (a Java.lang.ref.referencequeue$lock) at Java.lang.ref.ReferenceQueue.remove (referencequeue.java:133)-Locked < 0x00000000ec0b1310> (a java.lang.ref.referencequeue$lock) at Java.lang.ref.ReferenceQueue.remove ( referencequeue.java:149) at Java.lang.ref.finalizer$finalizerthread.run (finalizer.java:177) "Reference Handler" Daemon prio=10 tid=0x00007fa070071000 nid=0x769b in object.wait () [0x00007fa074a74000] Java.lanG.thread.state:waiting (on object monitor) at java.lang.Object.wait (Native Method)-Waiting on <0x00000000ec0b11e8 > (a Java.lang.ref.reference$lock) at java.lang.Object.wait (object.java:502) at java.lang.ref.reference$ Referencehandler.run (reference.java:133)-Locked <0x00000000ec0b11e8> (a java.lang.ref.reference$lock) "VM Thread "prio=10 tid=0x00007fa07006a800 nid=0x769a runnable" GC task thread#0 (PARALLELGC) "prio=10 tid= 0x00007fa070011800 nid=0x7698 runnable "GC task thread#1 (PARALLELGC)" Prio=10 tid=0x00007fa070013800 nid=0x7699 Runnable "VM periodic Task Thread" prio=10 tid=0x00007fa07009b000 nid=0x76a1 waiting on condition JNI global References:8 65

The above code starts two threads, thread T1 consumes a small amount of CPU, and thread T2 fills a CPU.

First we list processes by top, sort by CPU usage (SHIFT + p)


You can see that the process number of the problem is 30358.

Then use Top-h-P 30358 to get the following results, and then use SHITF + p to sort by CPU usage


Here you can see that the thread is 30371 consuming too much CPU, this thread has a problem

Next use Jstack 30358 > T.dat to record the thread stack

Use Python to print out 30371 of the 16 binary values (print ("0x"% 30371 results for 76A3

Locate the 76A3 process in the thread stack, and then carefully review the stack information


Here you can see the ttwo thread name, run to the main 31 lines, at this time we go to the source code to carefully review the nearby logic, the problem at a glance

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Identify excessive CPU usage in Java code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.