Which Java thread is hogging the CPU?

前端 未结 12 1716
萌比男神i
萌比男神i 2020-12-07 07:48

Let\'s say your Java program is taking 100% CPU. It has 50 threads. You need to find which thread is guilty. I have not found a tool that can help. Currently I use the follo

相关标签:
12条回答
  • 2020-12-07 08:23

    I would recommend taking a look at Arthas tool open sourced by Alibaba.

    It contains a bunch of useful commands that can help you debug your production code:

    • Dashboard: Overview of Your Java Process
    • SC: Search Class Loaded by Your JVM
    • Jad: Decompile Class Into Source Code
    • Watch: View Method Invocation Input and Results
    • Trace: Find the Bottleneck of Your Method Invocation
    • Monitor: View Method Invocation Statistics
    • Stack: View Call Stack of the Method
    • Tt: Time Tunnel of Method Invocations

    Example of the dashboard:

    0 讨论(0)
  • 2020-12-07 08:26

    jvmtop can show you the top consuming threads:

        TID NAME                                 STATE     CPU    TOTALCPU
         25 http-8080-Processor13                RUNNABLE  4.55%     1.60%
     128022 RMI TCP Connection(18)-10.101.       RUNNABLE  1.82%     0.02%
      36578 http-8080-Processor164               RUNNABLE  0.91%     2.35%
     128026 JMX server connection timeout   TIMED_WAITING  0.00%     0.00%
    
    0 讨论(0)
  • 2020-12-07 08:27

    Have a look at the Top Threads plugin for JConsole.

    0 讨论(0)
  • 2020-12-07 08:27

    If you're running under Windows, try Process Explorer. Bring up the properties dialog for your process, then select the Threads tab.

    0 讨论(0)
  • 2020-12-07 08:27

    If you suspect VisualVM is a good tool, try it (because it does this) Find out the threads(s) only helps you in the general direction of why it is consuming so much CPU.

    However, if its that obvious I would go straight to using a profiler to find out why you are consuming so much CPU.

    0 讨论(0)
  • 2020-12-07 08:29

    An option you could consider is querying your threads for the answer from within application. Via the ThreadMXBean you can query CPU usage of threads from within your Java application and query stack traces of the offending thread(s).

    The ThreadMXBean option allows you to build this kind of monitoring into your live application. It has negligible impact and has the distinct advantage that you can make it do exactly what you want.

    0 讨论(0)
提交回复
热议问题