使用OperatingSystemMXBean获取CPU使用率 [英] Using OperatingSystemMXBean to get CPU usage

查看:1017
本文介绍了使用OperatingSystemMXBean获取CPU使用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java获取当前正在运行的Java虚拟机使用的CPU百分比。我的研究表明我使用了 com.sun.management.OperatingSystemMXBean 类。以下在线示例,我编写了以下内容:

  import java.lang.management.ManagementFactory; 
import com.sun.management.OperatingSystemMXBean;


公共类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());

}

}

测试后在两台计算机上,我无法停止得到两个调用的 -1.0 的结果。我尝试通过各种方法使用Java 7和6的64位和32位版本运行此代码。不过,我得到的只是 -1.0 的打印件,其中根据Javadocs



根据系统中正在进行的活动,所有介于0.0和1.0之间的值都是可能的。如果系统最近的CPU使用率不可用,则该方法返回负值。



我不知道在这里还可以做什么。获取CPU负载至关重要,我想避免使用其他库,例如Sigar。有人对这个问题有任何建议或见解吗?

解决方案

只要回答我自己的问题,以防可能对某人有所帮助。 / p>

我在做什么在技术上是正确的,但是我没有给 OperatingSystemMXBean 足够的时间来收集CPU使用率信息。 JVM实际上必须运行几秒钟才能收集CPU使用情况信息,然后刷新分辨率似乎与实现有关。



使用以下更改将在我的计算机上大约2秒钟后开始产生使用信息:

  public static void main(String [] args ){
OperatingSystemMXBean bean =(com.sun.management.OperatingSystemMXBean)ManagementFactory
.getOperatingSystemMXBean();

而(true){
System.out.println(bean.getProcessCpuLoad());
System.out.println(bean.getSystemCpuLoad());
}

}

希望这可以对某人有所帮助


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:

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,

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?

解决方案

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.

Running the code with the following change will start to produce usage information after ~2 seconds on my machine:

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

这篇关于使用OperatingSystemMXBean获取CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆