Skip to Content
avatar image
Former Member

Get CPU metrics from Java application

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!

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • 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.management.ManagementFactory;
    import java.lang.management.OperatingSystemMXBean;
    import java.lang.reflect.Method;
    import java.lang.reflect.Modifier;
    
    private static void printUsage() {
      OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
      for (Method method : operatingSystemMXBean.getClass().getDeclaredMethods()) {
        method.setAccessible(true);
        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
    }
    Add comment
    10|10000 characters needed 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