<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>3.12.2</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.2.0</version>
</dependency>
import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import com.sun.management.OperatingSystemMXBean;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import oshi.SystemInfo;
import oshi.hardware.CentralProcessor; /**
* 系统监控
*/
public class SystemMonitor { public void init() {
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> {
try { SystemInfo systemInfo = new SystemInfo(); OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
// 椎内存使用情况
MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage(); // 初始的总内存
long initTotalMemorySize = memoryUsage.getInit();
// 最大可用内存
long maxMemorySize = memoryUsage.getMax();
// 已使用的内存
long usedMemorySize = memoryUsage.getUsed(); // 操作系统
String osName = System.getProperty("os.name");
// 总的物理内存
String totalMemorySize = new DecimalFormat("#.##")
.format(osmxb.getTotalPhysicalMemorySize() / 1024.0 / 1024 / 1024) + "G";
// 剩余的物理内存
String freePhysicalMemorySize = new DecimalFormat("#.##")
.format(osmxb.getFreePhysicalMemorySize() / 1024.0 / 1024 / 1024) + "G";
// 已使用的物理内存
String usedMemory = new DecimalFormat("#.##").format(
(osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / 1024.0 / 1024 / 1024)
+ "G";
// 获得线程总数
ThreadGroup parentThread;
for (parentThread = Thread.currentThread().getThreadGroup(); parentThread
.getParent() != null; parentThread = parentThread.getParent()) { } int totalThread = parentThread.activeCount(); // 磁盘使用情况
File[] files = File.listRoots();
for (File file : files) {
String total = new DecimalFormat("#.#").format(file.getTotalSpace() * 1.0 / 1024 / 1024 / 1024)
+ "G";
String free = new DecimalFormat("#.#").format(file.getFreeSpace() * 1.0 / 1024 / 1024 / 1024) + "G";
String un = new DecimalFormat("#.#").format(file.getUsableSpace() * 1.0 / 1024 / 1024 / 1024) + "G";
String path = file.getPath();
System.err.println(path + "总:" + total + ",可用空间:" + un + ",空闲空间:" + free);
System.err.println("=============================================");
} System.err.println("操作系统:" + osName);
System.err.println("程序启动时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(new Date(ManagementFactory.getRuntimeMXBean().getStartTime())));
System.err.println("pid:" + System.getProperty("PID"));
System.err.println("cpu核数:" + Runtime.getRuntime().availableProcessors());
printlnCpuInfo(systemInfo);
System.err.println("JAVA_HOME:" + System.getProperty("java.home"));
System.err.println("JAVA_VERSION:" + System.getProperty("java.version"));
System.err.println("USER_HOME:" + System.getProperty("user.home"));
System.err.println("USER_NAME:" + System.getProperty("user.name"));
System.err.println("初始的总内存(JVM):"
+ new DecimalFormat("#.#").format(initTotalMemorySize * 1.0 / 1024 / 1024) + "M");
System.err.println(
"最大可用内存(JVM):" + new DecimalFormat("#.#").format(maxMemorySize * 1.0 / 1024 / 1024) + "M");
System.err.println(
"已使用的内存(JVM):" + new DecimalFormat("#.#").format(usedMemorySize * 1.0 / 1024 / 1024) + "M");
System.err.println("总的物理内存:" + totalMemorySize);
System.err
.println("总的物理内存:"
+ new DecimalFormat("#.##").format(
systemInfo.getHardware().getMemory().getTotal() * 1.0 / 1024 / 1024 / 1024)
+ "M");
System.err.println("剩余的物理内存:" + freePhysicalMemorySize);
System.err
.println("剩余的物理内存:"
+ new DecimalFormat("#.##").format(
systemInfo.getHardware().getMemory().getAvailable() * 1.0 / 1024 / 1024 / 1024)
+ "M");
System.err.println("已使用的物理内存:" + usedMemory);
System.err.println("已使用的物理内存:"
+ new DecimalFormat("#.##").format((systemInfo.getHardware().getMemory().getTotal()
- systemInfo.getHardware().getMemory().getAvailable()) * 1.0 / 1024 / 1024 / 1024)
+ "M");
System.err.println("总线程数:" + totalThread);
System.err.println("===========================");
} catch (Exception e) {
e.printStackTrace();
}
}, 0, 60, TimeUnit.SECONDS);
} /**
* 打印 CPU 信息
*
* @param systemInfo
*/
private void printlnCpuInfo(SystemInfo systemInfo) throws InterruptedException {
CentralProcessor processor = systemInfo.getHardware().getProcessor();
long[] prevTicks = processor.getSystemCpuLoadTicks();
// 睡眠1s
TimeUnit.SECONDS.sleep(1);
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;
System.err.println("cpu核数:" + processor.getLogicalProcessorCount());
System.err.println("cpu系统使用率:" + new DecimalFormat("#.##%").format(cSys * 1.0 / totalCpu));
System.err.println("cpu用户使用率:" + new DecimalFormat("#.##%").format(user * 1.0 / totalCpu));
System.err.println("cpu当前等待率:" + new DecimalFormat("#.##%").format(iowait * 1.0 / totalCpu));
System.err.println("cpu当前空闲率:" + new DecimalFormat("#.##%").format(idle * 1.0 / totalCpu));
System.err.format("CPU load: %.1f%% (counting ticks)%n", processor.getSystemCpuLoadBetweenTicks() * 100);
System.err.format("CPU load: %.1f%% (OS MXBean)%n", processor.getSystemCpuLoad() * 100);
}
}

获取内存及cpu信息的更多相关文章

  1. Delphi 获取内存及CPU信息的函数

    Uses MemoryCpuUtils;//首先引用该单元 //声明下列变量用来存储读取的数值 Var iTotalPhysics, iTotalVirtual, iTotalPageFile, iC ...

  2. linux获取内存、cpu、负载、网口流量、磁盘信息

    内存信息 / meminfo 返回dict   #!/usr/bin/env python   def memory_stat():   mem = {}   f = open("/proc ...

  3. 查询系统状态 内存大小 cpu信息 设备负载情况

    1.1 查看内存状态 /proc/meminfo里面存放着内存的信息 查看内存命令(包括虚拟内存swap): free -h (低版本系统可能不支持-h) 或者 free -m (以mb单位显示) a ...

  4. [No0000112]ComputerInfo,C#获取计算机信息(cpu使用率,内存占用率,硬盘,网络信息)

    github地址:https://github.com/charygao/SmsComputerMonitor 软件用于实时监控当前系统资源等情况,并调用接口,当资源被超额占用时,发送警报到个人手机: ...

  5. Golang利用第三方包获取本机cpu使用率以及内存使用情况

    第三方包下载 $ github.com/shirou/gopsutil 获取内存方面的信息 package main import ( "fmt" "github.com ...

  6. CPU测试--通过proc获取CPU信息

    adb shell cat /proc/stat | grep cpu > totalcpu0 此处第一行的数值表示的是CPU总的使用情况,所以我们只要用第一行的数字计算就可以了.下表解析第一行 ...

  7. MD5做为文件名。机器唯一码有电脑的CPU信息和MAC地址,这两个信息需要在linux或unix系统下才能获取吧。

    可以采用机器(电脑)唯一码 + 上传IP + 当前时间戳 + GUID ( + 随机数),然后MD5做为文件名.机器唯一码有电脑的CPU信息和MAC地址,这两个信息需要在linux或unix系统下才能 ...

  8. Java如何获取系统cpu、内存、硬盘信息

    1 概述 前段时间摸索在Java中怎么获取系统信息包括cpu.内存.硬盘信息等,刚开始使用Java自带的包进行获取,但这样获取的内存信息不够准确并且容易出现找不到相应包等错误,所以后面使用sigar插 ...

  9. Android获取系统cpu信息,内存,版本,电量等信息

    本文转自:http://www.cnblogs.com/brainy/archive/2012/05/30/2526752.html 1.CPU频率,CPU信息:/proc/cpuinfo和/proc ...

  10. C#获取电脑型号、系统版本、内存大小、硬盘大小、CPU信息

    摘要 有时需要获取电脑的相关信息.这时可以通过调用windows api的方式,进行获取. 方法 可以通过在powershell中 通过下面的命令进行查询,然后可以通过c#调用获取需要的信息. gwm ...

随机推荐

  1. 你到底懂不懂JavaScript?来做做这12道面试题试试!

    携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第 21 天,点击查看活动详情 JavaScript 是每一个前端开发者都应该掌握的基础技术,但是很多时候,你可能并不完全懂 Jav ...

  2. 关于JWT中RSA数据加密协议在.net中应用

    加密协议有哪些 加密协议分为对称加密和非对称加密. 对称加密就是将信息使用一个密钥进行加密,解密时使用同样的密钥,同样的算法进行解密. 非对称加密,又称公开密钥加密,是加密和解密使用不同密钥的算法,广 ...

  3. [前端js] 爬取亿图脑图大纲

    这段程序使看到了好的东西,又没有零钱的产物 还是老师让画思维导图我不想画还想白嫖的想法 用时20分钟 就拿这个来作为例子 https://mm.edrawsoft.cn/template/286842 ...

  4. 使用Wireshark完成实验3-IP

    1.使用Wireshark打开ip-ethereal-trace-1,如图 电脑IP地址为192.168.1.102 2.如图,IP包头中上层协议字段的值为1,代表为ICMP 3.如图,IP头中有20 ...

  5. Pycharm报错:Error running ‘‘: Cannot run program “\python.exe“ (in directory ““)系统找不到指定文件夹?已解决!

    问题报错 报错原因:我修改的工程的名称/或者移动了工程位置,运行导致找不到之前的运行路径 解决办法1.在该项目文件夹下找到一个叫.idea的文件夹.(若没有,选择显示隐藏项目,可能被隐藏了)PyCha ...

  6. nginx(二) の 配置静态资源网站

    首先在开始配置前,要想明白,nginx 配置 静态资源 服务时基本逻辑,其实就是要 将路由地址与服务器中文件真实的存储地址进行映射. 配置静态资源样例 配置前,首先要将当前配置文件的路径,写入到 ng ...

  7. MybatisPlus #{param}和${param}的用法详解

    作用 mybatis-plus接口mapper方法中的注解(如@Select)或者xml(如)传入的参数是通过#{param}或者${param}来获取值. 区别 1.解析方式: #{param}:会 ...

  8. 4.git的指令应用

    1.stash 应用:   应用场景:在当前分支开发代码,开发到一半,代码没有提交,你想在当前分支的基础上切换到别的分支:或者创建新的分支,所以需要把当前开发的内容进行藏匿起来. 1.1藏匿指令:   ...

  9. 怎么才能卸载inventor?完全彻底卸载删除干净inventor各种残留注册表和文件的方法和步骤

    怎么才能卸载inventor?完全彻底卸载删除干净inventor各种残留注册表和文件的方法和步骤.如何卸载inventor呢?有很多同学想把inventor卸载后重新安装,但是发现inventor安 ...

  10. revit卸载工具,完全彻底卸载删除干净revit各种残留注册表和文件的方法和步骤。

    revit卸载工具,完全彻底卸载删除干净revit各种残留注册表和文件的方法和步骤.如何卸载revit呢?有很多同学想把revit卸载后重新安装,但是发现revit安装到一半就失败了或者显示revit ...