I am working on with some code to monitor Cpu load using Java. I am using following code in my app to get system load
public static double getCPULoad() {
OperatingSystemMXBean osMBean
= (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
double load = osMBean.getSystemLoadAverage();
return load;
}
But on windows it returns me a negative value (-1.0). Is there any way i can solve this problem?
The key to calculating a load averages on Windows is the ability to monitor the depth of the processor queue. The Microsoft article, Observing Processor Queue Length, describes the Windows System\Processor Queue Length metric, the critical measurement that allows the Host sFlow agent to calculate load averages.
CpuStres is a utility that can be used to simulate CPU activity by running up to 64 threads in a tight loop.
System load/CPU Load – is a measurement of CPU over or under-utilization in a Linux system; the number of processes which are being executed by the CPU or in waiting state. Load average – is the average system load calculated over a given period of time of 1, 5 and 15 minutes.
Please read this blog.
It's not an official solution but when using com.sun.management.OperatingSystemMXBean
instead of java.lang.management.OperatingSystemMXBean
there are two methods getSystemCpuLoad()
and getProcessCpuLoad()
that are working.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With