获取内存及cpu信息
<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信息的更多相关文章
- Delphi 获取内存及CPU信息的函数
Uses MemoryCpuUtils;//首先引用该单元 //声明下列变量用来存储读取的数值 Var iTotalPhysics, iTotalVirtual, iTotalPageFile, iC ...
- linux获取内存、cpu、负载、网口流量、磁盘信息
内存信息 / meminfo 返回dict #!/usr/bin/env python def memory_stat(): mem = {} f = open("/proc ...
- 查询系统状态 内存大小 cpu信息 设备负载情况
1.1 查看内存状态 /proc/meminfo里面存放着内存的信息 查看内存命令(包括虚拟内存swap): free -h (低版本系统可能不支持-h) 或者 free -m (以mb单位显示) a ...
- [No0000112]ComputerInfo,C#获取计算机信息(cpu使用率,内存占用率,硬盘,网络信息)
github地址:https://github.com/charygao/SmsComputerMonitor 软件用于实时监控当前系统资源等情况,并调用接口,当资源被超额占用时,发送警报到个人手机: ...
- Golang利用第三方包获取本机cpu使用率以及内存使用情况
第三方包下载 $ github.com/shirou/gopsutil 获取内存方面的信息 package main import ( "fmt" "github.com ...
- CPU测试--通过proc获取CPU信息
adb shell cat /proc/stat | grep cpu > totalcpu0 此处第一行的数值表示的是CPU总的使用情况,所以我们只要用第一行的数字计算就可以了.下表解析第一行 ...
- MD5做为文件名。机器唯一码有电脑的CPU信息和MAC地址,这两个信息需要在linux或unix系统下才能获取吧。
可以采用机器(电脑)唯一码 + 上传IP + 当前时间戳 + GUID ( + 随机数),然后MD5做为文件名.机器唯一码有电脑的CPU信息和MAC地址,这两个信息需要在linux或unix系统下才能 ...
- Java如何获取系统cpu、内存、硬盘信息
1 概述 前段时间摸索在Java中怎么获取系统信息包括cpu.内存.硬盘信息等,刚开始使用Java自带的包进行获取,但这样获取的内存信息不够准确并且容易出现找不到相应包等错误,所以后面使用sigar插 ...
- Android获取系统cpu信息,内存,版本,电量等信息
本文转自:http://www.cnblogs.com/brainy/archive/2012/05/30/2526752.html 1.CPU频率,CPU信息:/proc/cpuinfo和/proc ...
- C#获取电脑型号、系统版本、内存大小、硬盘大小、CPU信息
摘要 有时需要获取电脑的相关信息.这时可以通过调用windows api的方式,进行获取. 方法 可以通过在powershell中 通过下面的命令进行查询,然后可以通过c#调用获取需要的信息. gwm ...
随机推荐
- Java异常之Error和Exception
异常Exception 实际工作中,遇到的情况不可能是非常完美的.比如:你写的某个模块,用户输入不一定符合你的要求.你的程序要打开某个文件,这个文件可能不存在或者文件格式不对,你要读取数据库的数据,数 ...
- Dependency Analyzer
Dependency Analyzer idea插件 查找maven依赖 1.Setting---->Plugins------>Dependency Analyzer 2.使用 po ...
- spring boot创建多线程定时任务
@Component@EnableScheduling // 1.开启定时任务@EnableAsync // 2.开启多线程public class MultithreadScheduleTask { ...
- Unity 使用IO流获取PNG/JPG/GIF/BMP的宽高【转】
原文链接:https://blog.csdn.net/flj135792468/article/details/107963280?utm_medium=distribute.pc_relevant. ...
- Tech&W_STU_Resource_bookmark
整合资料 1 architecture: Enterprise integration patterns : https://www.enterpriseintegrationpatterns.com ...
- linux中安装doker
1.更新yum为最新的 yum -y update 2.卸载旧版的docker sudo yum remove -y docker* 3.安装需要的软件包 yum install -y yum-uti ...
- Keil5 STM32 C++开发 ARM V6编译器的使用教程
Keil5更新之后,开始支持ARM V6编译器,新版本的编译器对C++有了更多的支持,在编译方面也做了很多的改善,具体的没有详细了解,本文只是对STM32 开发下,使用V6版本的编译器进行STM32的 ...
- 软件工程日报一——Andriod的安装与配置
建民老师给我们布置了一个编写记事本app的任务,因此需要我们下载andriod studio,下面是我的下载过程 在下载andriod studio之前,需要我们配置Java环境 进入控制面板,输入 ...
- SQL Server修改表的时候出现错误:未更新任何行,未删除任何行
无论点击什么,一直报错:未更新任何行,未删除任何行,就差点砸电脑啦. 参考这个大神才解决的问题: https://blog.csdn.net/weixin_44690047/article/detai ...
- Apache SSI远程命令执行漏洞复现
漏洞原理:当目标服务器开启了SSI与CGI支持,就可以上传shtml文件,并利用语法执行任意命令,例如<!- - #exec cmd="ls"- ->,cmd后面跟要执 ...