28

I want to calculate percentage of CPU usage of OS from java code.

  1. There are several ways to find it by unix command [e.g. using mpstat, /proc/stat etc...] and use it from Runtime.getRuntime().exec

But I don't want to use the system calls.

I tried ManagementFactory.getOperatingSystemMXBean()

OperatingSystemMXBean osBean =
         (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
System.out.println(osBean.getSystemLoadAverage());

But it gives the cpu load but not the cpu usage. Is there anyway to find the usage percentage?

Gray
  • 115,027
  • 24
  • 293
  • 354
G.S
  • 10,413
  • 7
  • 36
  • 52
  • Can you link to Javadoc for the `OperatingSystemMXBean` please? Is percentage calculated from load or vice versa? I do not know, but it might be. – Freiheit Aug 28 '13 at 13:34
  • 4
    No. Load is nothing to do with percentage. [wiki](http://en.wikipedia.org/wiki/Load_%28computing%29) and [javadoc](http://docs.oracle.com/javase/6/docs/api/java/lang/management/OperatingSystemMXBean.html) – G.S Aug 28 '13 at 13:39

2 Answers2

43

In Java 7 you can get it like so:

public static double getProcessCpuLoad() throws Exception {

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

    if (list.isEmpty())     return Double.NaN;

    Attribute att = (Attribute)list.get(0);
    Double value  = (Double)att.getValue();

    // usually takes a couple of seconds before we get real values
    if (value == -1.0)      return Double.NaN;
    // returns a percentage value with 1 decimal point precision
    return ((int)(value * 1000) / 10.0);
}
Gray
  • 115,027
  • 24
  • 293
  • 354
isapir
  • 21,295
  • 13
  • 115
  • 116
  • Does this only work on certain operating systems? Running Java 7u15 on 64bit Windows 7, `NaN` is returned, as `value` is negative one. – FThompson Feb 22 '14 at 23:42
  • It should work on all. I tested it on Window 7 Java7u51 64bit and it works fine after a few seconds. The first few seconds return -1.0 because it takes some time to initialize some of the objects/values. – isapir Feb 22 '14 at 23:58
  • 1
    Interesting, it seems to take about one second on my machine for the CPU load to be an actual value. Thanks for the clarification. +1 – FThompson Feb 23 '14 at 00:08
  • 2
    I added a comment in the code to clarify for future users who might not read the comments here. – isapir Feb 23 '14 at 00:18
  • @Igal thank you for the answer. I am using windows-7 + java 7u45 but still no luck for me. By the way, the attribute list you provided in code is `ProcessCpuLoad`. Is it the CPU load or CPU usage? – G.S Feb 24 '14 at 14:57
  • 10
    ProcessCpuLoad is the % CPU load of that JVM for the system. there is also SystemCpuLoad which gives the load of the whole system. did you try to call it in a loop? I noticed that the first few calls return -1.0 – isapir Feb 24 '14 at 16:14
  • It always returns NaN in Mac OS X, even after several minutes. – TheDoctor Aug 05 '14 at 14:52
  • This one worked like a charm ! I cross checked with jconsole and they gave similar value (more or less), – Lynx777 May 09 '16 at 15:21
  • (value * 1000) / 10.0) == value * 100 – Raheel Jul 17 '17 at 17:23
  • @Raheel actually, not. by dividing by `10.0` you convert the `int` to `double`. – isapir Jul 17 '17 at 22:29
  • You would realize this when you calculate these stats... :-) – acthota Mar 13 '19 at 11:46
0

You can use the SIGAR API. It is cross platform ( but I've only use it on Windows).

The Javadoc is available here and the binaries are here

It is licensed under the terms of the Apache 2.0 license.

Julien
  • 2,256
  • 3
  • 26
  • 31
  • Thanks! But in javadoc of this API I see only "Get the Total system cpu time" not the usage under in Cpu class. Can you please guide me where i can get the CPU usage? – G.S Aug 28 '13 at 14:01
  • If you have downloaded Sigar from SourceForge, you should find several examples availables at hyperic-sigar-1.6.4\bindings\java\examples\. I think that CpuInfo.java or Top.java could be what you are looking for. – Julien Aug 28 '13 at 14:26
  • It seems like nothing later than Ubuntu 9 is certified for SIGAR API as per the site. Should that be a problem? – Shivaji_Vidhale Jun 05 '15 at 22:27
  • 1
    we need to take into account of SIGAR making JVM crash also. Strict NO NO for production environment . – AKS Jun 14 '17 at 05:04
  • The SIGAR link is dead. Is the project dead as well? Or perhaps acquired by vmware, judging by the second link – Fractaly Aug 12 '19 at 16:49