6

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The "this blog" link is no longer available. Can somebody expand? How do we get the other one?
Ok, I got it to work by just calling com.sun.management.OperatingSystemMXBean#getSystemCpuLoad.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.