Java如何获取系统CPU及内存情况呢?
下文笔者讲述java代码获取CPU及内存情况的方法分享,如下所示
1.引入相应的jar包 2.使用工具类中相应的方法即可实现获取CPU和内存的信息例:获取CPU及内存情况
<!-- oshi监控系统内存 -->
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>3.5.0</version>
</dependency>
二、工具类
package org.ming.utils;
import lombok.extern.slf4j.Slf4j;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import java.text.DecimalFormat;
/**
* 监控系统内存
*/
@Slf4j
public class MonSystem {
public static void main(String[] args) throws InterruptedException {
while (true) {
getCpuUtilization();
getMemoryUtilization();
Thread.sleep(1000);
}
}
/**
* 获取内存利用率
*/
public static void getMemoryUtilization() throws InterruptedException {
SystemInfo systemInfo = new SystemInfo();
GlobalMemory memory = systemInfo.getHardware().getMemory();
long totalByte = memory.getTotal();
long acaliableByte = memory.getAvailable();
log.info("内存大小 = {},内存使用率 ={}", formatByte(totalByte),
new DecimalFormat("#.##%").format((totalByte - acaliableByte) * 1.0 / totalByte));
}
public 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);
}
/**
* 获取CPU利用率
* @throws InterruptedException
*/
public static void getCpuUtilization() throws InterruptedException {
SystemInfo systemInfo = new SystemInfo();
CentralProcessor processor = systemInfo.getHardware().getProcessor();
long[] prevTicks = processor.getSystemCpuLoadTicks();
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;
log.info("CPU总数 = {},CPU利用率 ={}", processor.getLogicalProcessorCount(),
new DecimalFormat("#.##%").format(1.0 - (idle * 1.0 / totalCpu)));
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


