1

I want to find usage of every available processor in system using Java.I have got the number of available processors in system using following:

com.sun.management.OperatingSystemMXBean) ManagementFactory().getOperatingSystemMXBean().getAvailableProcessors();

But not able to find usage of every available processor.
Please suggest some solution.Also i need hard disk usage in a system.

Frank Pavageau
  • 11,477
  • 1
  • 43
  • 53
Surjit
  • 345
  • 1
  • 5
  • 13

1 Answers1

0

You can use the SIGAR API for this

or you can try this to find the CPU and RAM usage.

  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;
    }
    System.out.println(method.getName() + " = " + value);
    } 
 }
}

Check out this Thread

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331