Skip to Content

Get CPU metrics from Java application

Apr 10, 2017 at 10:17 AM


avatar image

I need to develope a Java application which does CPU metrics snapshots and stores the data in a custom table. Is there any Java class which provides a functionality for CPU metrics collection like ABAP GET_CPU_ALL FM? The target system has Java stack only and I cannot use ABAP code there...

Thank you very much in advance!

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Richard Zhao
Nov 20, 2017 at 06:55 AM

I recommend you use the SIGAR API. I use the SIGAR API in one of my own applications and it is great. You'll find it is stable, well supported, and full of useful examples. It is open-source with an Apache 2.0 license. Check it out. I have a feeling it will meet your needs. Using Java and the Sigar API you can get Memory, CPU, Disk, Load-Average, Network Interface info and metrics, Process Table information, Route info, etc.or you can get your CPU and RAM by using the following code snippet. See ManagementFactory for more details.

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

private static void printUsage() {
  OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
  for (Method method : operatingSystemMXBean.getClass().getDeclaredMethods()) {
    if (method.getName().startsWith("get")
        && Modifier.isPublic(method.getModifiers())) {
            Object value;
        try {
            value = method.invoke(operatingSystemMXBean);
        } catch (Exception e) {
            value = e;
        } // try
        System.out.println(method.getName() + " = " + value);
    } // if
  } // for
Show 1 Share
10 |10000 characters needed characters left characters exceeded

this is the result of this function

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco}

getCommittedVirtualMemorySize = 8453869568

getTotalSwapSpaceSize = 0

getFreeSwapSpaceSize = 0

getProcessCpuTime = 81979000

getFreePhysicalMemorySize = 2236596224

getTotalPhysicalMemorySize = 17179869184

getOpenFileDescriptorCount = 5

getMaxFileDescriptorCount = 10240

getSystemCpuLoad = 0.0

getProcessCpuLoad = 0.0