High CPU Utilization in java application - why?

前端 未结 6 716
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-29 22:44

I have a Java Application (web-based) that at times shows very high CPU Utilization (almost 90%) for several hours. Linux TOP command shows this. On application res

相关标签:
6条回答
  • 2021-01-29 22:55

    If a profiler is not applicable in your setup, you may try to identify the thread following steps in this post.

    Basically, there are three steps:

    1. run top -H and get PID of the thread with highest CPU.
    2. convert the PID to hex.
    3. look for thread with the matching HEX PID in your thread dump.
    0 讨论(0)
  • 2021-01-29 22:58

    Your first approach should be to find all references to Thread.sleep and check that:

    1. Sleeping is the right thing to do - you should use some sort of wait mechanism if possible - perhaps careful use of a BlockingQueue would help.

    2. If sleeping is the right thing to do, are you sleeping for the right amount of time - this is often a very difficult question to answer.

    The most common mistake in multi-threaded design is to believe that all you need to do when waiting for something to happen is to check for it and sleep for a while in a tight loop. This is rarely an effective solution - you should always try to wait for the occurrence.

    The second most common issue is to loop without sleeping. This is even worse and is a little less easy to track down.

    0 讨论(0)
  • 2021-01-29 23:01

    During these peak CPU times, what is the user load like? You say this is a web based application, so the culprits that come to mind is memory utilization issues. If you store a lot of stuff in the session, for instance, and the session count gets high enough, the app server will start thrashing about. This is also a case where the GC might make matters worse depending on the scheme you are using. More information about the app and the server configuration would be helpful in pointing towards more debugging ideas.

    0 讨论(0)
  • 2021-01-29 23:04

    In the thread dump you can find the Line Number as below.

    for the main thread which is currently running...

    "main" #1 prio=5 os_prio=0 tid=0x0000000002120800 nid=0x13f4 runnable [0x0000000001d9f000]
       java.lang.Thread.State: **RUNNABLE**
        at java.io.FileOutputStream.writeBytes(Native Method)
        at java.io.FileOutputStream.write(FileOutputStream.java:313)
        at com.rana.samples.**HighCPUUtilization.main(HighCPUUtilization.java:17)**
    
    0 讨论(0)
  • 2021-01-29 23:14

    You may be victim of a garbage collection problem.

    When your application requires memory and it's getting low on what it's configured to use the garbage collector will run often which consume a lot of CPU cycles. If it can't collect anything your memory will stay low so it will be run again and again. When you redeploy your application the memory is cleared and the garbage collection won't happen more than required so the CPU utilization stays low until it's full again.

    You should check that there is no possible memory leak in your application and that it's well configured for memory (check the -Xmx parameter, see What does Java option -Xmx stand for?)

    Also, what are you using as web framework? JSF relies a lot on sessions and consumes a lot of memory, consider being stateless at most!

    0 讨论(0)
  • 2021-01-29 23:14

    You did not assign the "linux" to the question but you mentioned "Linux top". And thus this might be helpful:

    Use the small Linux tool threadcpu to identify the most cpu using threads. It calls jstack to get the thread name. And with "sort -n" in pipe you get the list of threads ordered by cpu usage.

    More details can be found here: http://www.tuxad.com/blog/archives/2018/10/01/threadcpu_-_show_cpu_usage_of_threads/index.html

    And if you still need more details then create a thread dump or run strace on the thread.

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