• Java 获取系统cpu 内存


    原文链接:https://blog.csdn.net/CallMeV6/article/details/83176840

    说明:获取的数据是操作系统整体的资源占用情况,不是当前 java进程占用的资源

    1. 获取系统CPU占用情况 :

    import java.lang.management.ManagementFactory;
    import com.sun.management.OperatingSystemMXBean;
     
    private static OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    public static int cpuLoad() {
        double cpuLoad = osmxb.getSystemCpuLoad();
        int percentCpuLoad = (int) (cpuLoad * 100);
        return percentCpuLoad;
    
    }

    注意:JDK必须是1.8及以上的

               返回的值是CPU的百分比,取的是整数值

    2. 获取系统内存占用情况 

    import java.lang.management.ManagementFactory;
    import com.sun.management.OperatingSystemMXBean;
     
    private static OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    public static int memoryLoad() {
        double totalvirtualMemory = osmxb.getTotalPhysicalMemorySize();
        double freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
            
        double value = freePhysicalMemorySize/totalvirtualMemory;
        int percentMemoryLoad = (int) ((1-value)*100);
        return percentMemoryLoad;
    
    }
    

    注意:JDK为1.6及以上版本

               返回的值是内存的百分比,取的是整数值

  • 相关阅读:
    Bean
    DI
    require import export
    JSON转js
    vue路由相关
    JS引号区别
    Go语言系列之标准库strconv
    Go语言系列之标准库flag
    Go语言系列之并发编程
    Go语言系列之自定义实现日志库
  • 原文地址:https://www.cnblogs.com/lshan/p/11681804.html
Copyright © 2020-2023  润新知