3

I would like to get cpu usage to update my database using java continuously. At the first loop, this code is readable the correct cpu usage. After then, it returns wrong values less than 0. So, I stucked.

I used jdk 1.8.124.

plz, let me know how to get cpu usage continuously.

lib

import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.HardwareAbstractionLayer;

src

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());
        try {
            Thread.sleep(3000);
        }
        catch (Exception e){
            System.out.println(e.toString());
            break;
        }
  }

}
8
  • Why not use a library like Dropwizard Metrics or Micrometer ? Commented Apr 2, 2020 at 6:30
  • Below URL can be helpful. stackoverflow.com/questions/47177/… Commented Apr 2, 2020 at 6:38
  • @cricket_007 I will do it Commented Apr 3, 2020 at 0:02
  • 1
    @서민봉MinbongDavidSeo 해봤는데, 안됩니다. 일단 CPU 사용률 값들이 작업관리자에 나오는 값들과 매칭되질 않아요. 분명 CPU 사용률은 80%이상 올라가 있음에도 불구하고 12%로 계속 뜬다거나, SIGAR API는 특정 컴퓨터에서는 에러 로그만 작성되고 정상적으로 돌지 않습니다. (Windows 환경인데 원보드 타입의 개발보드 (ATOM processor)에서는 특히 작동을 안해요) 다른 것들도 첫번째에서만 제대로된 답을 내놓고 두번째 아웃풋부터는 소숫점 이하의 값을 결과로 보여줍니다 ㅠㅠ Commented Apr 3, 2020 at 0:06
  • => i tried but it's still not working. the CPU usage values was not matched with the values in the task manager. Obviously, even though the cpu usage has risen above 80%, it keeps indicating around 12%. Using sigar API generated an error log file on certain computers and then turned off. ( on windows environment it tested, it doesn't work especially on a dev board(ATOM processor)). Most of sample code gives correct answer at the first time only. Other outputs are still less th Commented Apr 3, 2020 at 0:15

2 Answers 2

6

It's done by using Oshi lib.

I can get cpu usage every 20 seconds lib

import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.HardwareAbstractionLayer;

src

private SystemInfo si = new SystemInfo();
private HardwareAbstractionLayer hal = si.getHardware();
private CentralProcessor cpu = hal.getProcessor();
long[] prevTicks = new long[TickType.values().length];

public static double getCPU()
{
    double cpuLoad = cpu.getSystemCpuLoadBetweenTicks( prevTicks ) * 100;
    prevTicks = cpu.getSystemCpuLoadTicks();
    System.out.println("cpuLoad : " + cpuLoad);
    return cpuLoad;
}

public static void main(String[] args) throws Exception{
    while(true) {
        // Sleep 20 seconds
        tCPU = (int)getCPU();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Actually, under Windows I don't see a big difference between bean.getSystemCpuLoad() and the result of the Oshi lib when Using Java 11. It's pretty much the same. But there is a huge difference to the CPU utilization shown in the Windows task manager.
3

I use the following code to get the CPU load, which works without invoking hidden methods in com.sun.* classes:

public static Double getProcessCpuLoad() {
    try {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem");
        AttributeList list = mbs.getAttributes(name, new String[]{"ProcessCpuLoad"});

        return Optional.ofNullable(list)
                .map(l -> l.isEmpty() ? null : l)
                .map(List::iterator)
                .map(Iterator::next)
                .map(Attribute.class::cast)
                .map(Attribute::getValue)
                .map(Double.class::cast)
                .orElse(null);

    } catch (Exception ex) {
        return null;
    }
}

Works fine, tested with JRE 8/11/13.

3 Comments

I added up your code. And multiply getProcessCpuLoad by 10. And, this is a result of it: 7.243884075601578 0.6300612162235574 0.12868694055771907 0.12894463720532992 0.12908448934655176 0.06456276270743097 0.12845790173533678 0.06463014120467704 0.12915222118015093 it still not working. :(
why do you multiply by 10 and not by 100 (to the the % value)? Also the values look ok - what was your expectation?
it was my typo. I multiplied by 100. So 7.243884075601578 looks right answer but rest of answers were not good. On my task manager, CPU usage indicated more than 30% at the first answer. So, could you gave me more information about this like imported library?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.