Java 使用 OperatingSystemMXBean 获取 CPU 使用率
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19781087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using OperatingSystemMXBean to get CPU usage
提问by Kon
I'm trying to use Java to get the percentage of CPU used by the currently running Java Virtual Machine. My research has pointed me to using the com.sun.management.OperatingSystemMXBean
class. Following examples online, I've written the following:
我正在尝试使用 Java 来获取当前运行的 Java 虚拟机使用的 CPU 的百分比。我的研究指出我使用这个com.sun.management.OperatingSystemMXBean
类。按照网上的例子,我写了以下内容:
import java.lang.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
public class TestClass {
public static void main (String[] args) {
OperatingSystemMXBean bean = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
System.out.println(bean.getProcessCpuLoad());
System.out.println(bean.getSystemCpuLoad());
}
}
After testing this on two machines, I can't stop getting a result of -1.0
for both calls. I have tried running this code using both 64- and 32-bit versions of Java 7 and 6 through various methods. Still, all I get is a print out of -1.0
which, according to the Javadocs,
在两台机器上测试后,我无法停止获得-1.0
两个调用的结果。我曾尝试通过各种方法使用 64 位和 32 位版本的 Java 7 和 6 运行此代码。尽管如此,我得到的只是打印出来的-1.0
,根据 Javadocs,
All values betweens 0.0 and 1.0 are possible depending of the activities going on in the system. If the system recent cpu usage is not available, the method returns a negative value.
All values betweens 0.0 and 1.0 are possible depending of the activities going on in the system. If the system recent cpu usage is not available, the method returns a negative value.
I don't know what else to do here. Getting the CPU load is critical, and I'd like to avoid using another library like Sigar. Anybody have any recommendations or have any insights into this problem?
我不知道这里还能做什么。获取 CPU 负载至关重要,我想避免使用像 Sigar 这样的其他库。任何人都有任何建议或对这个问题有任何见解?
采纳答案by Kon
Just answering my own question in case it could help somebody.
只是回答我自己的问题,以防它可以帮助某人。
What I was doing was technically correct, however I was not giving the OperatingSystemMXBean
enough time to gather CPU usage information. The JVM must actually be running for a few seconds before it can gather CPU usage information, and then the refresh resolution is implementation-dependent it would seem.
我所做的在技术上是正确的,但是我没有给OperatingSystemMXBean
足够的时间来收集 CPU 使用情况信息。JVM 必须实际运行几秒钟才能收集 CPU 使用率信息,然后刷新分辨率似乎取决于实现。
Running the code with the following change will start to produce usage information after ~2 seconds on my machine:
运行具有以下更改的代码将在我的机器上约 2 秒后开始生成使用信息:
public static void main(String[] args) {
OperatingSystemMXBean bean = (com.sun.management.OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean();
while (true) {
System.out.println(bean.getProcessCpuLoad());
System.out.println(bean.getSystemCpuLoad());
}
}
Hopefully this can help somebody
希望这可以帮助某人