1

I test my server in java, and monitored the cpu usage for it. First I use top command, and it show me as follows:

enter image description here

And then ,I use command

ps -C java -L -o pcpu,cpu,nice,state,cputime,pid,tid | sort to show all the java thread

enter image description here

the first column is cpu usage of each thread.

And I just found that the highest usage is only 0.3 and all cpu usage Sum up much less than 31.6.

I wish some one can tell me why ,THKS

edit:

To see more information , I use top -Hp 1234 (1234is my java pid) and I suddenly found that there exists a java thread(the pidis 1238) and the cpu usage of which usually suddenly promoted to 10-20 and even more, and I think it is this to be blamed.

And then I use

jstack 1234 |grep 4d6 -A 30

to see more detail of the thread, and it showed me :

"VM Thread" prio=10 tid=0x00007ff6ec060000 nid=0x4d6 runnable

"VM Periodic Task Thread" prio=10 tid=0x00007ff6ec091800 nid=0x4dd waiting on condition

JNI global references: 262

And I wish you can help me to analyse it.

5
  • try use htop for detailed information Commented Jan 22, 2016 at 9:13
  • THKS for answer, I have installed and used to htop, but it didn't solve my question, what I want is to see the cpu of usage of each java thread, because in my monitor it show too high, but I was given that the java cpu usage of each java thread is less than 0.3% each. Commented Jan 22, 2016 at 9:25
  • Because the list of thread is output by ps at a different moment than the sum value (31.6). The two doesn't match. Commented Jan 22, 2016 at 9:49
  • Thks,that is true and I found that there exist a java thread, the cpu usage of which suddenly promote to 10-20 percent even more ,and I think it was this thread to be blame, I added the detail in my question, wish you can help more Commented Jan 22, 2016 at 10:16
  • Using the small Linux tool "threadcpu" can help you to list the threads with high CPU usage. It does this by using a measuring interval and not by counting all jiffies from process start. tuxad.com/blog/archives/2018/10/01/… Commented Oct 15, 2018 at 20:07

1 Answer 1

1

Maybe there is a misunderstanding of the pcpu value. From man ps

CPU usage is currently expressed as the percentage of time spent running during the entire lifetime of a process. This is not ideal, and it does not conform to the standards that ps otherwise conforms to. CPU usage is unlikely to add up to exactly 100%.

If the process starts and has a high CPU utilization, then the %CPU value is high. When the same process later has a low CPU utilization or is in sleep state this value will decrease. %CPU represents the cputime/realtime ratio.

As a small example

class Loop {
    public static void main(String...args) throws Exception {
        long l = 0;
        for (int i = 0; i >= 0; i++) {
            for (int j = 0; j < 50; j++) {
            l++;
            }
        }
        System.out.println("will sleep now");
        Thread.sleep(30_000);
    }
}

Start in one session following command, which will monitor the process in an interval of one second.

watch -n 1 ps -C java -o pcpu,state,cputime,etimes

Now run in another session the example code.

As long the Java code is inside the foo-loops and has not printed will sleep now the value of %CPU is really high (around 100). When the Java process reach the Thread.sleep the value of %CPU will constantly be decreasd (in this example).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.