简体   繁体   English

Java + JDBC的CPU使用率

[英]CPU usage of Java + JDBC

While testing my webapp under load I got following top excerpt under Linux: 在负载下测试我的webapp时,我得到了Linux下的top摘录:

 PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
3964 nobody    20   0 4965m 622m 6048 S  8.5 11.0   6:02.49 java 
1985 mysql     20   0  294m 125m 3804 S  2.1  2.2   0:05.39 /usr/sbin/mysqld

I need an explanation on java's %CPU column. 我需要对java的%CPU列进行解释。 As I understand it, during web request one of java 's thread is performing some pure Java logic, consuming some of CPU time (let it be 5ms). 据我了解,在web请求期间, java的一个线程正在执行一些纯Java逻辑,消耗一些CPU时间(让它为5ms)。 Then it connects to database via JDBC, sends some sql query and waits, say 10ms , for response. 然后它通过JDBC连接到数据库,发送一些sql查询并等待,例如10ms ,以进行响应。 These 10ms are obviously counted as /usr/sbin/mysqld CPU usage. 这些10ms显然被计为/usr/sbin/mysqld CPU使用率。 Then java thread resumes it's operation and finishes, consuming another 20ms and amounting to 5+10+20=30ms total execution time. 然后java线程恢复它的操作并完成,消耗另外20ms并且总计执行时间为5 + 10 + 20 = 30ms。

And the newbie question is: don't you think that these db-related 10ms are counted twice: one time as java thread waiting for db to process the query and second time as database CPU usage itself? 而新手的问题是:难道你不认为这些与db相关的10ms被计算两次:一次是java线程等待db处理查询而第二次作为数据库CPU使用本身? What don't I understand here? 我在这里不明白什么?

When a process is simply waiting on a socket it will not normally be using significant amounts of CPU. 当进程只是在套接字上等待时,通常不会使用大量的CPU。 Basically, the kernel knows that it is in a waiting state and will not schedule it to run - except for little things like signal handling, which can be used eg to run the garbage collector regularly. 基本上,内核知道它处于等待状态并且不会安排它运行 - 除了信号处理之类的小事情,例如可以用来定期运行垃圾收集器。

(OK, if there is a lot of garbage to collect, that "little thing" might not be so little.) (好吧,如果要收集大量垃圾,那么“小东西”可能不会那么少。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM