java 系统工具类 查询内存 CPU 系统基本信息 SysInfoUtils_java cpuinfoutil-程序员宅基地

技术标签: jvm  java  maven  工具  后端  

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());
    }
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zhongjianboy/article/details/126480842

智能推荐

Pytroch同一个优化器优化多个模型的参数并且保存优化后的参数_pytorch加载多个模型-程序员宅基地

文章浏览阅读4.5k次,点赞7次,收藏26次。在进行深度学习过程中会遇到几个模型进行串联,这几个模型需要使用同一个优化器,但每个模型的学习率或者动量等其他参数不一样这种情况。一种解决方法是新建一个模型将这几个模型进行串联,另一种解决方法便是往优化器里面传入这几个模型的参数。..._pytorch加载多个模型

计算机软考中级合格标准,中级软考多少分及格-程序员宅基地

文章浏览阅读1.4k次。原标题:中级软考多少分及格盛泰鼎盛 对于第一次报名软考的朋友,可能对于考试合格分数线不太了解,软考分为初、中、高三个级别,那么软考中级多少分及格呢?软考中级合格标准根据往年的软考合格分数线来看,各级别的合格标准基本上统一的。2019年上半年计算机技术与软件专业技术资格(水平)考试各级别各专业各科目合格标准均为45分(总分75分)。而2016下半年计算机技术与软件专业技术资格(水平)考试除了信息系统..._计算机程序设计员中级考试内容及合格标准

爬虫相关-程序员宅基地

文章浏览阅读50次。2019独角兽企业重金招聘Python工程师标准>>> ..._爬虫考虑安全法律因素

ASP.NET Identity 的“多重”身份验证-程序员宅基地

文章浏览阅读263次。本章主要内容有:  ● 实现基于微软账户的第三方身份验证  ● 实现双因子身份验证  ● 验证码机制实现基于微软账户的第三方身份验证  在微软提供的ASP.NET MVC模板代码中,默认添加了微软、Google、twitter以及Facebook的账户登录代码(虽然被注释了),另外针对国内的一些社交账户提供了相应的组件,所有组件都可以通过Nuget包管理器安装:    从..._identity 二次登录

2018年秋季学期课表-程序员宅基地

文章浏览阅读241次。李理论基础I、II课程编码:011D9101Z﹡ 课时:80 学分:4.00 课程属性:其它 主讲教师:聂思安 教学目的要求李群和李代数(Lie group and Lie algebra)是在1874年由挪威数学家SophusLie为研究微分方程的对称性而引进的。后经过E. Cartan 和H. Weyl等人的努力,李的理论已成了微分几何的重要研究工具并发展成完整的代数理论。上世纪..._层的上同调

iOS多线程-03-NSOperation与NSOperationQueue-程序员宅基地

文章浏览阅读33次。简介通过NSOperation与NSOperationQueue的组合也能实现多线程通常将任务封装成NSOperation对象,并将对象添加到NSOperationQueue中实现NSOperationNSOperation是一个抽象类,不能用来直接封装操作,通常使用它的子类来封装操作若不将NSOperation对象添加到NSOperationQueue中,操作只会在当前线程执...

随便推点

数据驱动的产品研发:如何利用数据驱动提高产品安全性-程序员宅基地

文章浏览阅读867次,点赞11次,收藏20次。1.背景介绍在当今的数字时代,数据已经成为企业和组织中最宝贵的资产之一。随着数据的增长和复杂性,数据驱动的决策变得越来越重要。数据驱动的产品研发是一种新兴的方法,它利用数据来优化产品的设计、开发和运营。这种方法可以帮助企业更有效地利用数据,提高产品的安全性和质量。在这篇文章中,我们将探讨数据驱动的产品研发的核心概念、算法原理、实例和未来发展趋势。我们将涉及到以下几个方面:背景介绍核...

基础类的DSP/BIOS API调用_clk_gethtime 返回值-程序员宅基地

文章浏览阅读1.3k次。转载自:http://blog.sina.com.cn/s/blog_48b82df90100bpfj.html基础类的DSP/BIOS API调用一、时钟管理CLK(1)Uns ncounts = CLK_countspms(void) 返回每毫秒的定时器高分辨率时钟的计数值(2)LgUns currtime = CLK_gethtime(void) _clk_gethtime 返回值

Appium环境搭建及“fn must be a function”问题解决-程序员宅基地

文章浏览阅读38次。由于appium在线安装比较困难,大多数应该是由于FQ造成的吧,索性直接下载appium安装包:http://pan.baidu.com/s/1bpfrvjDnodejs下载也很缓慢,现提供nodejs4.4.4下载地址:http://pan.baidu.com/s/1bIsS02环境搭建步骤可以参考:http://www.cnblogs.com/tobecrazy/p/4562199.h..._启动appium fn must be a function

基于单片机的语音存储与回放系统设计-程序员宅基地

文章浏览阅读1.3k次,点赞28次,收藏27次。在人类的历史长河中,语言的作用尤为重要,人们一直在思考一个问题,那就是如何把语言完全不差的记录下来。通过单片机控制语音芯片完成的语音存储与回访系统的电路比较大,而且回涉及到很多的模块电路,比如会涉及到单片机的最小系统、时钟电路、液晶显示模块等等,所以在焊接时要十分注意,涉及到多种模块的这种电路,哪怕只要存在一处的焊接错误,就会导致整个系统的检测无法完成,因为电路中交叉的线路非常多,所以在焊接过程中避免焊接错误和短路现象,如果电路连接错误,将给检测带来极大的不便,并且该电路具有更多的交叉线。_基于单片机的语音存储与回放系统设计

转载《一个射频工程师的职场日记》_射频工程师中年危机-程序员宅基地

文章浏览阅读3.7k次,点赞10次,收藏37次。本文转载自电子发烧友论坛http://bbs.elecfans.com/jishu_1674416_1_1.html转载此文章为了让更多刚毕业或者快要毕业的电子专业的同学,对于自己的未来工作学习能有些帮助,相信很多人快毕业的时候估计和我一样都不太清楚自己未来应该做些什么,读完这篇文章让我获益匪浅。以前大学毕业找工作的时候,就很希望有以前的同专业的师兄姐们写点面经什么的。但等到自己毕业了,从来就没有想过要把自己的求职经历和别人分享一下,给后来人做个参考。人人为我,我为人人。前人栽树后人乘凉。现在正准备跳_射频工程师中年危机

IntelliJ IDEA2020安装教程-程序员宅基地

文章浏览阅读6.8k次,点赞16次,收藏99次。IntelliJ IDEA2020安装教程[软件名称]:IntelliJ IDEA2020[软件语言]:中文 /英文[软件大小]:643.31MB[安装环境]:Win10/Win8/Win7[64位下载链接]:下载地址[提取码]:y3bu软件简介IDEA 全称 IntelliJ IDEA,是java编程语言开发的集成环境。IntelliJ在业界被公认为最好的java开发工具,尤其在智能代码助手、代码自动提示、重构、J2EE支持、各类版本工具(git、svn等)、JUnit、CVS整合、_intellij idea2020安装

推荐文章

热门文章

相关标签