简体   繁体   English

如何通过Java进程查找CPU使用率

[英]how to find cpu usage by java process

I want to moniter cpu usage of some java application. 我想监视某些Java应用程序的CPU使用情况。 I don't want to use tools like jconsole (JMX) 我不想使用jconsole(JMX)之类的工具

if there is any way to check by java code? 是否有任何方法可以通过Java代码检查?

Use JavaSysMon 使用JavaSysMon

Currently it reports: 目前它报告:

  • Process table (also available as a tree), including pid, ppid, name, command line, user, process size, resident process size, user time, and kernel time. 进程表(也可以以树形式提供),包括pid,ppid,名称,命令行,用户,进程大小,驻留进程大小,用户时间和内核时间。
  • Uptime 正常运行时间
  • Current process's pid 当前进程的pid
  • Total and free RAM 总计和可用RAM
  • Total and free swap 完全和自由互换
  • CPU usage CPU使用率
  • CPU frequency CPU频率
  • Number of CPUs CPU数量

Also you can use jmx mbeans or get PID using Runtime.exec() and using that call platform-specific command like ps. 您也可以使用jmx mbeans或通过Runtime.exec()并使用特定于调用平台的命令(例如ps)来获取PID。

One way would be to use 一种方法是使用

Runtime.getRuntime().exec(command);

and run top | grep java 并运行top | grep java top | grep java command. top | grep java命令。

You should retrieve the MX Bean ThreadMXBean from the Java class itself and use the methods get*CpuTime() . 您应该从Java类本身检索MX Bean ThreadMXBean ,并使用get*CpuTime()

Otherwise, you can use good old top and grep on your Java process PID. 否则,您可以在Java进程PID上使用旧的top和grep。

If you are brave enough, there is also perf tool in linux-tools package. 如果您足够勇敢,则linux-tools软件包中也有perf工具。

Hope that helps ! 希望能有所帮助!

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

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