java 服务器 cpu 在 linux 中的使用监控
java server cpu usage monitor in linux
我在 java 中测试了我的服务器,并监控了它的 cpu 使用情况。
首先我使用 top 命令,它显示如下:
然后,我使用命令
ps -C java -L -o pcpu,cpu,nice,state,cputime,pid,tid | sort
to show all the java thread
第一列是cpu每个线程的使用情况。
而且我刚刚发现最高使用率只有0.3,所有cpu使用率加起来远小于31.6。
我希望有人能告诉我为什么,THKS
编辑:
要查看更多信息,我使用 top -Hp 1234(1234 是我的 java pid)
我突然发现有一个java线程(pidis 1238)和cpu使用率通常突然提升到10-20甚至更多,我认为这应该受到指责。
然后我用
jstack 1234 |grep 4d6 -A 30
查看线程的更多详细信息,它向我展示了:
"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
希望你能帮我分析一下。
可能对pcpu
值有误解。来自 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%.
如果进程启动并具有高 CPU 利用率,则 %CPU
值高。当同一进程稍后具有低 CPU 利用率或处于睡眠状态时,该值将减小。 %CPU
代表cputime/realtime ratio
.
举个小例子
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);
}
}
按照命令启动一个会话,它将以一秒的间隔监视进程。
watch -n 1 ps -C java -o pcpu,state,cputime,etimes
现在 运行 在另一个会话中的示例代码。
只要 Java 代码在 foo-loops 内并且没有打印 will sleep now
%CPU
的值就非常高(大约 100)。当 Java 进程达到 Thread.sleep
时,%CPU
的值将不断减少(在本例中)。
我在 java 中测试了我的服务器,并监控了它的 cpu 使用情况。 首先我使用 top 命令,它显示如下:
然后,我使用命令
ps -C java -L -o pcpu,cpu,nice,state,cputime,pid,tid | sort to show all the java thread
第一列是cpu每个线程的使用情况。
而且我刚刚发现最高使用率只有0.3,所有cpu使用率加起来远小于31.6。
我希望有人能告诉我为什么,THKS
编辑:
要查看更多信息,我使用 top -Hp 1234(1234 是我的 java pid) 我突然发现有一个java线程(pidis 1238)和cpu使用率通常突然提升到10-20甚至更多,我认为这应该受到指责。
然后我用
jstack 1234 |grep 4d6 -A 30
查看线程的更多详细信息,它向我展示了:
"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
希望你能帮我分析一下。
可能对pcpu
值有误解。来自 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%.
如果进程启动并具有高 CPU 利用率,则 %CPU
值高。当同一进程稍后具有低 CPU 利用率或处于睡眠状态时,该值将减小。 %CPU
代表cputime/realtime ratio
.
举个小例子
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);
}
}
按照命令启动一个会话,它将以一秒的间隔监视进程。
watch -n 1 ps -C java -o pcpu,state,cputime,etimes
现在 运行 在另一个会话中的示例代码。
只要 Java 代码在 foo-loops 内并且没有打印 will sleep now
%CPU
的值就非常高(大约 100)。当 Java 进程达到 Thread.sleep
时,%CPU
的值将不断减少(在本例中)。