Java获取cpu、内存、磁盘信息(开箱即用)

依赖

<dependency>
    <groupId>org.fusesource</groupId>
    <artifactId>sigar</artifactId>
    <version>1.6.4</version>
</dependency>

相关文件

https://pan.baidu.com/s/1vxTIml5iKotYoSec_SW6zg
提取码: 9d84

代码

import org.hyperic.sigar.*;

import java.util.Properties;

/**
 * @author :guohui.wang
 * @date :2020-11-2 16:14:04
 * @description:获取cpu、内存、磁盘信息
 */
/*
    <dependency>
        <groupId>org.fusesource</groupId>
        <artifactId>sigar</artifactId>
        <version>1.6.4</version>
    </dependency>

    Windows:
        系统是64位则把sigar-amd64-winnt.dll添加到jdk的bin目录
        系统是32位则把sigar-x86-winnt.dll添加到jdk的bin目录
        不好使就放到c:/windows/system32/下面试试
    Linux:
        系统是64位则把libsigar-amd64-Linux.so添加到jdk的bin目录
        系统是32位则把libsigar-x86-linux.so添加到jdk的bin目录
        不好使就添加到/usr/lib64或者/lib64或者/lib或者/usr/lib,只需放其中一个目录
    tips:
        目录还是不对的话百度一下吧
*/
public class SigarUtils {
    public static void main(String[] args) {
        cpu();
        memory();
        disk();
    }

    public static void cpu() {
        Properties props = System.getProperties();
        String systemName = props.getProperty("os.name");

        // 使用sigar获取CPU信息及内存
        Sigar sigar = new Sigar();

        // 获取CPU信息
        CpuInfo[] infos = new CpuInfo[0];
        try {
            infos = sigar.getCpuInfoList();
        } catch (SigarException e) {
            e.printStackTrace();
        }

        CpuInfo infoss = infos[0];

        CpuPerc cpu = null;
        try {
            cpu = sigar.getCpuPerc();
        } catch (SigarException e) {
            e.printStackTrace();
        }
        // CPU总数
        Integer totalCPUs = infoss.getTotalCores();

        String cpuinfo = infoss.getVendor() + "  " + infoss.getModel();
        // 获取CPU基准速度
        double referenceSpeed = infoss.getMhz();

        String referenceSpeeds = String.format("%.2f", referenceSpeed / 1000) + " GHz";

        // 获取CPU用户利用率
        double userUsedPerc = cpu.getUser();
        // 获取CPU系统利用率
        double sysUsedPerc = cpu.getSys();
        // 获取CPU利用率
        double cpuUsedPerc = cpu.getCombined();

        String userPers = "";
        String sysPers = "";
        String cpuPers = "";
        if (systemName.startsWith("win") || systemName.startsWith("Win")) {
            //Windows系统 Perc * 100
            userPers = String.format("%.1f", userUsedPerc * 100) + "%";
            sysPers = String.format("%.1f", sysUsedPerc * 100) + "%";
            cpuPers = String.format("%.1f", cpuUsedPerc * 100) + "%";
        } else {
            //Linux系统 Perc * 1000
            cpuPers = String.format("%.1f", cpuUsedPerc * 1000) + "%";
            sysPers = String.format("%.1f", sysUsedPerc * 1000) + "%";
            userPers = String.format("%.1f", userUsedPerc * 1000) + "%";
        }

        System.out.println(String.format("%-15s\t", "CPU:") + cpuinfo);
        System.out.println(String.format("%-15s\t", "CPU总数:") + totalCPUs);
        System.out.println(String.format("%-15s\t", "CPU基准速度:") + referenceSpeeds);
        System.out.println(String.format("%-15s\t", "CPU用户利用率:") + userPers);
        System.out.println(String.format("%-15s\t", "CPU系统利用率:") + sysPers);
        System.out.println(String.format("%-15s\t", "CPU利用率:") + cpuPers);

        // 关闭sigar
        sigar.close();
    }

    public static void memory() {
        // 使用sigar获取CPU信息及内存
        Sigar sigar = new Sigar();
        double memTotal = 0;
        double memRam = 0;
        double memUsed = 0;// mem
        double memFree = 0;
        double memUsedPerc = 0;
        try {
            memTotal = sigar.getMem().getTotal();
            memRam = sigar.getMem().getRam();
            memUsed = sigar.getMem().getActualUsed();
            memFree = sigar.getMem().getActualFree();
            memUsedPerc = sigar.getMem().getUsedPercent();
        } catch (SigarException e) {
            e.printStackTrace();
        }

        String memory = String.format("%.0f", memTotal / 1024 / 1024 / 1024) + " GB";
        String memRamStr = String.format("%.1f", memRam / 1024) + " GB";
        String memused = String.format("%.2f", memUsed / 1024 / 1024 / 1024) + " GB";
        String memFrees = String.format("%.2f", memFree / 1024 / 1024 / 1024) + " GB";
        String memoryUsage = String.format("%.2f", memUsedPerc) + " %";

        System.out.println(String.format("%-15s\t", "内存总量:") + memory);
        System.out.println(String.format("%-15s\t", "内存使用量:") + memRamStr);
        System.out.println(String.format("%-15s\t", "内存使用中:") + memused);
        System.out.println(String.format("%-15s\t", "内存可用量:") + memFrees);
        System.out.println(String.format("%-15s\t", "内存使用率:") + memoryUsage);

        // 关闭sigar
        sigar.close();
    }

    public static void disk() {
        Sigar sigar = new Sigar();
        try {
            FileSystem[] fileSystemList = sigar.getFileSystemList();
            for (FileSystem fileSystem : fileSystemList) {
                System.out.println(fileSystem.getDirName() + "的磁盘空间使用率是:" + sigar.getFileSystemUsage(fileSystem.getDirName()).getUsePercent());
            }

        } catch (SigarException e) {
            e.printStackTrace();
        } finally {
            sigar.close();
        }
    }

}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 点我我会动 设计师:白松林 返回首页