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.*;
public class SysInfoUtils {
private static final SystemInfo systemInfo = new SystemInfo();
private static final HardwareAbstractionLayer hardware = systemInfo.getHardware();
private static final OperatingSystem operatingSystem = systemInfo.getOperatingSystem();
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;
}
public static Map<String, Object> getCpuIngInfo() {
Map<String, Object> cpuInfo = new HashMap<>();
CentralProcessor processor = hardware.getProcessor();
long[] prevTicks = processor.getSystemCpuLoadTicks();
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);
cpuInfo.put("cSys", new DecimalFormat("#.##%").format(cSys * 1.0 / totalCpu));
cpuInfo.put("user", new DecimalFormat("#.##%").format(user * 1.0 / totalCpu));
cpuInfo.put("iowait", new DecimalFormat("#.##%").format(iowait * 1.0 / totalCpu));
cpuInfo.put("used", idle);
cpuInfo.put("free", totalCpu - idle);
cpuInfo.put("idle", new DecimalFormat("#.##%").format(1.0 - (idle * 1.0 / totalCpu)));
return cpuInfo;
}
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;
}
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();
cpuInfo.put("total", formatByte(runtime.totalMemory()));
cpuInfo.put("free", formatByte(runtime.freeMemory()));
cpuInfo.put("max", formatByte(runtime.maxMemory()));
cpuInfo.put("user", formatByte(jvmTotalMemoryByte - freeMemoryByte));
cpuInfo.put("usageRate", new DecimalFormat("#.##%").format((jvmTotalMemoryByte - freeMemoryByte) * 1.0 / jvmTotalMemoryByte));
cpuInfo.put("jdkVersion", ((Properties) props).getProperty("java.version"));
cpuInfo.put("jdkHome", props.getProperty("java.home"));
return cpuInfo;
}
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());
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;
}
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();
info.put("cpuName", processor.getName());
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());
info.put("isCpu64bit", processor.isCpu64bit());
return info;
}
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());
}
}