2

Hi I need to get the details about operating system Physical memory and cpu usage and other details. I cannot pay any amount for already available APIs. I can use any free APIs or I can write my own API.

I need all the details in the below image.

enter image description here

In the above picture I have to get the following values

  1. Total
  2. Cached
  3. Available
  4. Free

like this all values I need. For this I have searched a lot and got some hint. I got first value Total physical memory value using the below code.

public class MBeanServerDemo {
    public MBeanServerDemo() {
        super();
    }

    public static void main(String... a) throws Exception {
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        Object attribute =
            mBeanServer.getAttribute(new ObjectName("java.lang", "type", "OperatingSystem"), "TotalPhysicalMemorySize");
        long l = Long.parseLong(attribute.toString());
        System.out.println("Total memory: " + (l / (1024*1024)));
    }
}

The below is the output for the above program

Total memory: 3293

Please help me . How do I achieve this.

Edit: I have searched a lot on google for solution and I found a lot of posts in stackoverflow.com. But in all these posts people discussed about only memory details. I need all details about Kernal(Paged and Non-Paged) etc. Please refer this post...

My Requirement

Thanks A lot.

Community
  • 1
  • 1
Abdul
  • 1,130
  • 4
  • 29
  • 65
  • possible duplicate of [Get memory and CPU usage](http://stackoverflow.com/questions/6284384/get-memory-and-cpu-usage) – Raedwald Jul 23 '15 at 14:04
  • possible duplicate of [How to get Windows load values](http://stackoverflow.com/questions/23222251/how-to-get-windows-load-values) – xmojmr Jul 23 '15 at 15:00
  • Please help with this..... Please – Abdul Jul 27 '15 at 12:51
  • Please help with this.. http://stackoverflow.com/questions/31667378/how-to-retrieve-kernal-memory-detailspaged-and-non-paged-using-sigar-api-in-j – Abdul Jul 28 '15 at 08:49

3 Answers3

2

can you please look at below API:

SIGAR API - System Information Gatherer And Reporter https://support.hyperic.com/display/SIGAR/Home

Some examples Usage : http://www.programcreek.com/java-api-examples/index.php?api=org.hyperic.sigar.Sigar

Durgesh Patel
  • 1,035
  • 8
  • 15
  • Do I need to get licence to use this. What is the licence cost. – Abdul Jul 23 '15 at 12:51
  • Thanks for your time Durgesh. Now I am using SIGAR API. Still I am unable to get the specified details. I am able to get total and free system memory. Can you please tell me how to get cached and Page and NON-Paged memory details using SIGAR API. Thank you. – Abdul Jul 27 '15 at 12:17
0

You can use JNA which offers a lot of access to platform specific apis such like win32.dll

JNA on Github

silentCode
  • 19
  • 3
0

Consider using jInterop for this task on Windows.

To get the total amount of RAM in MB:

  public int getRAMSizeMB() throws JIException
  {
    String query = "Select * From Win32_ComputerSystem";
    long size = 0;
    Object[] params = new Object[]
    {
      new JIString(query),
      JIVariant.OPTIONAL_PARAM()
    };
    JIVariant[] res = super.callMethodA("ExecQuery", params);
    JIVariant[][] resSet = Utils.enumToJIVariantArray(res);
    for (JIVariant[] resSet1 : resSet)
    {
      IJIDispatch ramVal = (IJIDispatch) JIObjectFactory.narrowObject(resSet1[0].getObjectAsComObject()
        .queryInterface(IJIDispatch.IID));
      size = ramVal.get("TotalPhysicalMemory").getObjectAsLong();
      break;
    }
    return Math.round((size / 1024) / 1024);

To get the total amount of CPUs:

  public int getCpuCount() throws JIException
  {
    String query = "Select NumberOfLogicalProcessors From Win32_Processor";
    Object[] params = new Object[]
    {
      new JIString(query),
      JIVariant.OPTIONAL_PARAM()
    };
    JIVariant[] res = super.callMethodA("ExecQuery", params);
    JIVariant[][] resSet = Utils.enumToJIVariantArray(res);
    for (JIVariant[] resSet1 : resSet)
    {
      IJIDispatch procVal = (IJIDispatch) JIObjectFactory.narrowObject(resSet1[0].getObjectAsComObject()
        .queryInterface(IJIDispatch.IID));
      return procVal.get("NumberOfLogicalProcessors").getObjectAsInt();
    }
    return -1;
  }

Using these functions as a template, you can look up the other properties/functions via the MSDN website to query other values.

Fallso
  • 1,311
  • 1
  • 9
  • 18