java 系统工具类 查询内存 CPU 系统基本信息 SysInfoUtils

34 篇文章 0 订阅
11 篇文章 0 订阅

java 系统工具类 查询内存 CPU 系统基本信息 SysInfoUtils

maven依赖

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

工具类代码

import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.ComputerSystem;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.OSProcess;
import oshi.software.os.OperatingSystem;
import oshi.util.Util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zhong
 */
public class SysInfoUtils {
    /**
     * 信息类
     */
    private static final SystemInfo systemInfo = new SystemInfo();
    /**
     * 硬件信息类
     */
    private static final HardwareAbstractionLayer hardware = systemInfo.getHardware();
    /**
     * 操作系统信息类
     */
    private static final OperatingSystem operatingSystem = systemInfo.getOperatingSystem();

    /**
     * 获取系统信息
     *
     * @return 品牌 制造商 系统名称 版本 提供商
     */
    public static Map<String, Object> getSysInfo() {
        Map<String, Object> info = new HashMap<>();
        ComputerSystem computerSystem = hardware.getComputerSystem();
        // 获取电脑品牌
        info.put("pcModel:", computerSystem.getModel());
        // 电脑制造商
        info.put("pcManufacturer", computerSystem.getManufacturer());
        // 系统名称
        System.out.println("osName" + operatingSystem.getFamily());
        // 系统版本
        System.out.println("osVersion" + operatingSystem.getVersionInfo());
        // 系统供应商
        System.out.println("osSupplier" + operatingSystem.getManufacturer());
        return info;
    }

    /**
     * 获取Cpu的运行情况
     *
     * @return
     */
    public static Map<String, Object> getCpuIngInfo() {
        Map<String, Object> cpuInfo = new HashMap<>();
        CentralProcessor processor = hardware.getProcessor();
        // CPU信息
        long[] prevTicks = processor.getSystemCpuLoadTicks();
        //休眠1秒
        Util.sleep(1000);
        long[] ticks = processor.getSystemCpuLoadTicks();
        long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
        long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
        long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
        long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
        long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
        long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
        long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
        long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
        long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
        cpuInfo.put("total", totalCpu);
        //cpu系统使用率
        cpuInfo.put("cSys", new DecimalFormat("#.##%").format(cSys * 1.0 / totalCpu));
        //cpu用户使用率
        cpuInfo.put("user", new DecimalFormat("#.##%").format(user * 1.0 / totalCpu));
        //cpu当前等待率
        cpuInfo.put("iowait", new DecimalFormat("#.##%").format(iowait * 1.0 / totalCpu));
        //cpu当前使用率
        cpuInfo.put("used", idle);
        //空闲
        cpuInfo.put("free", totalCpu - idle);
        //百分百
        cpuInfo.put("idle", new DecimalFormat("#.##%").format(1.0 - (idle * 1.0 / totalCpu)));
        return cpuInfo;
    }

    /**
     * 获取系统信息内存
     *
     * @return
     */
    public static Map<String, Object> getRamInfo() {
        Map<String, Object> ram = new HashMap();
        GlobalMemory memory = systemInfo.getHardware().getMemory();
        //总内存
        long totalByte = memory.getTotal();
        //剩余
        long availableByte = memory.getAvailable();
        //总内存
        ram.put("total", totalByte);
        ram.put("totalStr", formatByte(totalByte));
        //使用
        ram.put("used", (totalByte - availableByte));
        ram.put("usedStr", formatByte(totalByte - availableByte));
        //剩余内存
        ram.put("free", availableByte);
        ram.put("freeStr", formatByte(availableByte));
        //使用率
        ram.put("usageRate", new DecimalFormat("#.##").format((totalByte - availableByte) * 1.0 / totalByte));
        ram.put("usageRateStr", new DecimalFormat("#.##%").format((totalByte - availableByte) * 1.0 / totalByte));
        return ram;
    }

    /**
     * 系统jvm信息
     */
    public static Map<String, Object> getJvmInfo() {
        Map<String, Object> cpuInfo = new HashMap<>();
        Properties props = System.getProperties();
        Runtime runtime = Runtime.getRuntime();
        long jvmTotalMemoryByte = runtime.totalMemory();
        long freeMemoryByte = runtime.freeMemory();
        //jvm总内存
        cpuInfo.put("total", formatByte(runtime.totalMemory()));
        //空闲空间
        cpuInfo.put("free", formatByte(runtime.freeMemory()));
        //jvm最大可申请
        cpuInfo.put("max", formatByte(runtime.maxMemory()));
        //vm已使用内存
        cpuInfo.put("user", formatByte(jvmTotalMemoryByte - freeMemoryByte));
        //jvm内存使用率
        cpuInfo.put("usageRate", new DecimalFormat("#.##%").format((jvmTotalMemoryByte - freeMemoryByte) * 1.0 / jvmTotalMemoryByte));
        //jdk版本
        cpuInfo.put("jdkVersion", ((Properties) props).getProperty("java.version"));
        //jdk路径
        cpuInfo.put("jdkHome", props.getProperty("java.home"));
        return cpuInfo;
    }

    /**
     * 获取系统所有进程
     *
     * @return
     */
    public static List<Map<String, Object>> getOSProcess() {
        List<Map<String, Object>> infoList = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<OSProcess> lists = operatingSystem.getProcesses();
        for (OSProcess process : lists) {
            Map<String, Object> info = new HashMap<>();
            // 进程更新时间
            info.put("updateTime", format.format(new Date(process.getStartTime() + process.getUpTime())));
            // 进程名称
            info.put("name", process.getName());
            // 进程用户
            info.put("user", process.getUser());
            // 进程ID
            info.put("id", process.getProcessID());
            // 文件地址
            info.put("path", process.getPath());
            // 位数
            info.put("bitness", process.getBitness());
            // 启动时间
            info.put("startTime", format.format(new Date(process.getStartTime())));
            infoList.add(info);
        }
        return infoList;
    }

    /**
     * 获取CPU信息
     *
     * @return
     */
    public static Map<String, Object> getCpuInfo() {
        Map<String, Object> info = new HashMap<>();
        CentralProcessor centralProcessor = hardware.getProcessor();
        // 逻辑处理器数量
        info.put("cpuLogicCount", centralProcessor.getLogicalProcessorCount());
        // 物理处理器数量
        info.put("cpuPhysicalCount", centralProcessor.getPhysicalProcessorCount());
        CentralProcessor.ProcessorIdentifier processor = centralProcessor.getProcessorIdentifier();
        // 获取CPU名称
        info.put("cpuName", processor.getName());
        // 获取CPU型号
        info.put("cpuModel", processor.getModel());
        // 获取家族
        info.put("cpuFamily", processor.getFamily());
        // 频率
        info.put("cpuFreq", processor.getVendorFreq());
        // 供货商
        info.put("cpuVendor", processor.getVendor());
        //  微架构
        info.put("cpuMicroarchitecture", processor.getMicroarchitecture());
        info.put("cpuId", processor.getProcessorID());
        // 步进
        info.put("cpuStepping", processor.getStepping());
        // 是否64位
        info.put("isCpu64bit", processor.isCpu64bit());
        return info;
    }

    /**
     * 格式化转换
     *
     * @param byteNumber 传入byte
     * @return 格式化结果
     */
    private static String formatByte(long byteNumber) {
        //换算单位
        double FORMAT = 1024.0;
        double kbNumber = byteNumber / FORMAT;
        if (kbNumber < FORMAT) {
            return new DecimalFormat("#.##KB").format(kbNumber);
        }
        double mbNumber = kbNumber / FORMAT;
        if (mbNumber < FORMAT) {
            return new DecimalFormat("#.##MB").format(mbNumber);
        }
        double gbNumber = mbNumber / FORMAT;
        if (gbNumber < FORMAT) {
            return new DecimalFormat("#.##GB").format(gbNumber);
        }
        double tbNumber = gbNumber / FORMAT;
        return new DecimalFormat("#.##TB").format(tbNumber);
    }

    public static void main(String[] args) throws InterruptedException {
        System.out.println(getCpuInfo());
    }
}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卑微小钟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值