3

I've been trying to get a process CPU usage by using Sigar library but I always get values of 0 even when it should be higher.

I'm doing this on a timer every second:

Sigar sigar = new Sigar();
long pid = find.find("Exe.Name.ct=myprocess.exe");

ProcCpu cpu = sigar.getProcCpu(pid);
System.out.println(cpu.getPercent());

I've been taking a look at this other related question and its accepted answer but it doesn't seem to work properly plus things like TOTAL_TIME_UPDATE_LIMIT are kind of a mystery. I also took a look at this another one but it didn't provide to me anything new.

Has anyone tried to monitor the process CPU before with Sigar? What's the way to accomplish it?

Thanks.

Community
  • 1
  • 1
Alvaro
  • 40,778
  • 30
  • 164
  • 336

1 Answers1

0

use it like this

public static void main(String args[]) {
    try {
        final Sigar sigar = new Sigar();
        while (true) {
            ProcCpu cpu = sigar.getProcCpu("Exe.Name.ct=eclipse.exe");
            System.out.println(cpu.getPercent());
        }
    } catch (SigarException ex) {
        ex.printStackTrace();
    }

}

this is a sample output I got

0.0
0.11450381679389313
0.12213740458015267
0.10714285714285714
0.0
0.0
0.0

some time some processes always consume zero percentage of CPU. therfore, Go to the task manager and find a process that has a high CPU usage and give the exe of that process to the relevant place in code

lakshman
  • 2,641
  • 6
  • 37
  • 63