Get OS-level system information

后端 未结 16 1795
情话喂你
情话喂你 2020-11-22 02:02

I\'m currently building a Java app that could end up being run on many different platforms, but primarily variants of Solaris, Linux and Windows.

Has anyone been abl

相关标签:
16条回答
  • 2020-11-22 02:41

    For windows I went this way.

        com.sun.management.OperatingSystemMXBean os = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    
        long physicalMemorySize = os.getTotalPhysicalMemorySize();
        long freePhysicalMemory = os.getFreePhysicalMemorySize();
        long freeSwapSize = os.getFreeSwapSpaceSize();
        long commitedVirtualMemorySize = os.getCommittedVirtualMemorySize();
    

    Here is the link with details.

    0 讨论(0)
  • 2020-11-22 02:43

    I think the best method out there is to implement the SIGAR API by Hyperic. It works for most of the major operating systems ( darn near anything modern ) and is very easy to work with. The developer(s) are very responsive on their forum and mailing lists. I also like that it is GPL2 Apache licensed. They provide a ton of examples in Java too!

    SIGAR == System Information, Gathering And Reporting tool.

    0 讨论(0)
  • 2020-11-22 02:45

    Usually, to get low level OS information you can call OS specific commands which give you the information you want with Runtime.exec() or read files such as /proc/* in Linux.

    0 讨论(0)
  • 2020-11-22 02:46

    To get the System Load average of 1 minute, 5 minutes and 15 minutes inside the java code, you can do this by executing the command cat /proc/loadavg using and interpreting it as below:

        Runtime runtime = Runtime.getRuntime();
    
        BufferedReader br = new BufferedReader(
            new InputStreamReader(runtime.exec("cat /proc/loadavg").getInputStream()));
    
        String avgLine = br.readLine();
        System.out.println(avgLine);
        List<String> avgLineList = Arrays.asList(avgLine.split("\\s+"));
        System.out.println(avgLineList);
        System.out.println("Average load 1 minute : " + avgLineList.get(0));
        System.out.println("Average load 5 minutes : " + avgLineList.get(1));
        System.out.println("Average load 15 minutes : " + avgLineList.get(2));
    

    And to get the physical system memory by executing the command free -m and then interpreting it as below:

    Runtime runtime = Runtime.getRuntime();
    
    BufferedReader br = new BufferedReader(
        new InputStreamReader(runtime.exec("free -m").getInputStream()));
    
    String line;
    String memLine = "";
    int index = 0;
    while ((line = br.readLine()) != null) {
      if (index == 1) {
        memLine = line;
      }
      index++;
    }
    //                  total        used        free      shared  buff/cache   available
    //    Mem:          15933        3153        9683         310        3097       12148
    //    Swap:          3814           0        3814
    
    List<String> memInfoList = Arrays.asList(memLine.split("\\s+"));
    int totalSystemMemory = Integer.parseInt(memInfoList.get(1));
    int totalSystemUsedMemory = Integer.parseInt(memInfoList.get(2));
    int totalSystemFreeMemory = Integer.parseInt(memInfoList.get(3));
    
    System.out.println("Total system memory in mb: " + totalSystemMemory);
    System.out.println("Total system used memory in mb: " + totalSystemUsedMemory);
    System.out.println("Total system free memory in mb: "   + totalSystemFreeMemory);
    
    0 讨论(0)
提交回复
热议问题