Get OS-level system information

后端 未结 16 1779
情话喂你
情话喂你 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:19

    CPU usage isn't straightforward -- java.lang.management via com.sun.management.OperatingSystemMXBean.getProcessCpuTime comes close (see Patrick's excellent code snippet above) but note that it only gives access to time the CPU spent in your process. it won't tell you about CPU time spent in other processes, or even CPU time spent doing system activities related to your process.

    for instance i have a network-intensive java process -- it's the only thing running and the CPU is at 99% but only 55% of that is reported as "processor CPU".

    don't even get me started on "load average" as it's next to useless, despite being the only cpu-related item on the MX bean. if only sun in their occasional wisdom exposed something like "getTotalCpuTime"...

    for serious CPU monitoring SIGAR mentioned by Matt seems the best bet.

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

    Have a look at the APIs available in the java.lang.management package. For example:

    • OperatingSystemMXBean.getSystemLoadAverage()
    • ThreadMXBean.getCurrentThreadCpuTime()
    • ThreadMXBean.getCurrentThreadUserTime()

    There are loads of other useful things in there as well.

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

    You can get some limited memory information from the Runtime class. It really isn't exactly what you are looking for, but I thought I would provide it for the sake of completeness. Here is a small example. Edit: You can also get disk usage information from the java.io.File class. The disk space usage stuff requires Java 1.6 or higher.

    public class Main {
      public static void main(String[] args) {
        /* Total number of processors or cores available to the JVM */
        System.out.println("Available processors (cores): " + 
            Runtime.getRuntime().availableProcessors());
    
        /* Total amount of free memory available to the JVM */
        System.out.println("Free memory (bytes): " + 
            Runtime.getRuntime().freeMemory());
    
        /* This will return Long.MAX_VALUE if there is no preset limit */
        long maxMemory = Runtime.getRuntime().maxMemory();
        /* Maximum amount of memory the JVM will attempt to use */
        System.out.println("Maximum memory (bytes): " + 
            (maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory));
    
        /* Total memory currently available to the JVM */
        System.out.println("Total memory available to JVM (bytes): " + 
            Runtime.getRuntime().totalMemory());
    
        /* Get a list of all filesystem roots on this system */
        File[] roots = File.listRoots();
    
        /* For each filesystem root, print some info */
        for (File root : roots) {
          System.out.println("File system root: " + root.getAbsolutePath());
          System.out.println("Total space (bytes): " + root.getTotalSpace());
          System.out.println("Free space (bytes): " + root.getFreeSpace());
          System.out.println("Usable space (bytes): " + root.getUsableSpace());
        }
      }
    }
    
    0 讨论(0)
  • 2020-11-22 02:24

    On Windows, you can run the systeminfo command and retrieves its output for instance with the following code:

    private static class WindowsSystemInformation
    {
        static String get() throws IOException
        {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec("systeminfo");
            BufferedReader systemInformationReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    
            StringBuilder stringBuilder = new StringBuilder();
            String line;
    
            while ((line = systemInformationReader.readLine()) != null)
            {
                stringBuilder.append(line);
                stringBuilder.append(System.lineSeparator());
            }
    
            return stringBuilder.toString().trim();
        }
    }
    
    0 讨论(0)
  • 2020-11-22 02:27

    There's a Java project that uses JNA (so no native libraries to install) and is in active development. It currently supports Linux, OSX, Windows, Solaris and FreeBSD and provides RAM, CPU, Battery and file system information.

    • https://github.com/oshi/oshi
    0 讨论(0)
  • 2020-11-22 02:28

    Add OSHI dependency via maven:

    <dependency>
        <groupId>com.github.dblock</groupId>
        <artifactId>oshi-core</artifactId>
        <version>2.2</version>
    </dependency>
    

    Get a battery capacity left in percentage:

    SystemInfo si = new SystemInfo();
    HardwareAbstractionLayer hal = si.getHardware();
    for (PowerSource pSource : hal.getPowerSources()) {
        System.out.println(String.format("%n %s @ %.1f%%", pSource.getName(), pSource.getRemainingCapacity() * 100d));
    }
    
    0 讨论(0)
提交回复
热议问题