How do I monitor the computer's CPU, memory, and disk usage in Java?

后端 未结 11 2820
粉色の甜心
粉色の甜心 2020-11-22 13:02

I would like to monitor the following system information in Java:

  • Current CPU usage** (percent)
  • Available memory* (free/total)
  • Available d

11条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 13:25

    This works for me perfectly without any external API, just native Java hidden feature :)

    import com.sun.management.OperatingSystemMXBean;
    ...
    OperatingSystemMXBean osBean = ManagementFactory.getPlatformMXBean(
                    OperatingSystemMXBean.class);
    // What % CPU load this current JVM is taking, from 0.0-1.0
    System.out.println(osBean.getProcessCpuLoad());
    
    // What % load the overall system is at, from 0.0-1.0
    System.out.println(osBean.getSystemCpuLoad());
    

提交回复
热议问题