Java Program CPU consumption analysis to find the most CPU-consuming threads

Source: Internet
Author: User

Java program CPU consumption is too high generally there are two situations: 1,US High, the application consumes too much CPU resources, it needs to find out the code executed by the CPU-consuming thread, and analyze the reason of locating the problem.The analysis steps are as follows:   (1) Use the top command to find the most CPU-intensive Java process

(2) Find the thread that consumes the highest CPU
TOP-HP 1781


(3) the highest CPU thread 17596 translates into 16 binary corresponding thread 44BC
With the command
printf "%x\n" 17596

(4) Print stack information for the highest CPU Java process 1781
Jstack1781> Stackdump.txt

The code is as follows: public class Cpuconsumetest {    public static void main (string[] args) {        in T count = Runtime.getruntime (). Availableprocessors ();         SYSTEM.OUT.PRINTLN ("processors" + count);         for (int i=0; i<count; i++) {            New Thread (New Consu Mecputest ()). Start ();        }                 for (int i=0; i<100; i++) {            New Thread (New Notconsumecputest ()). Start ();        }    }    } class Consumecputest implements Runnable {    pub LIC void Run () {        while (true) {           -for (int i=0; i<100 00000; i++) {                long L = 1000000;                 Math.acos (L); &nbsP          }             try {          &N Bsp     Thread.CurrentThread (). Sleep (20);            } catch (Interruptedexception e) {            &NB Sp   Thread.CurrentThread (). interrupt ();            }        }    }} class Notconsumecputest Implem Ents Runnable {    public void run () {        while (true) {        &N Bsp   try {                Thread.CurrentThread (). Sleep (+)     &NBSP ;      } catch (Interruptedexception e) {                Thread.cur Rentthread (). interrupt ();            }        }    }}


2, Sy too high, the context switch too frequently, the thread stack print out, see if there is any lock competition too intense

Java Program CPU consumption analysis to find the most CPU-consuming threads

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.