Java获取Linux系统cpu使用率详解编程语言
2023-06-13 09:20:28 时间
long total = FileSystemUtils.freeSpaceKb("/home");
double disk = (double) total / 1024 / 1024;
return (int) disk;
} catch (IOException e) {
e.printStackTrace();
return 0;
/**
* 功能:获取Linux系统cpu使用率
* */
public static int cpuUsage() {
try {
Map ?, ? map1 = OSUtils.cpuinfo();
Thread.sleep(5 * 1000);
Map ?, ? map2 = OSUtils.cpuinfo();
long user1 = Long.parseLong(map1.get("user").toString());
long nice1 = Long.parseLong(map1.get("nice").toString());
long system1 = Long.parseLong(map1.get("system").toString());
long idle1 = Long.parseLong(map1.get("idle").toString());
long user2 = Long.parseLong(map2.get("user").toString());
long nice2 = Long.parseLong(map2.get("nice").toString());
long system2 = Long.parseLong(map2.get("system").toString());
long idle2 = Long.parseLong(map2.get("idle").toString());
long total1 = user1 + system1 + nice1;
long total2 = user2 + system2 + nice2;
float total = total2 - total1;
long totalIdle1 = user1 + nice1 + system1 + idle1;
long totalIdle2 = user2 + nice2 + system2 + idle2;
float totalidle = totalIdle2 - totalIdle1;
float cpusage = (total / totalidle) * 100;
return (int) cpusage;
} catch (InterruptedException e) {
e.printStackTrace();
return 0;
/**
* 功能:CPU使用信息
* */
public static Map ?, ? cpuinfo() {
InputStreamReader inputs = null;
BufferedReader buffer = null;
Map String, Object map = new HashMap String, Object
try {
inputs = new InputStreamReader(new FileInputStream("/proc/stat"));
buffer = new BufferedReader(inputs);
String line = "";
while (true) {
line = buffer.readLine();
if (line == null) {
break;
if (line.startsWith("cpu")) {
StringTokenizer tokenizer = new StringTokenizer(line);
List String temp = new ArrayList String
while (tokenizer.hasMoreElements()) {
String value = tokenizer.nextToken();
temp.add(value);
map.put("user", temp.get(1));
map.put("nice", temp.get(2));
map.put("system", temp.get(3));
map.put("idle", temp.get(4));
map.put("iowait", temp.get(5));
map.put("irq", temp.get(6));
map.put("softirq", temp.get(7));
map.put("stealstolen", temp.get(8));
break;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
buffer.close();
inputs.close();
} catch (Exception e2) {
e2.printStackTrace();
return map;
/**
* 功能:内存使用率
* */
public static int memoryUsage() {
Map String, Object map = new HashMap String, Object
InputStreamReader inputs = null;
BufferedReader buffer = null;
try {
inputs = new InputStreamReader(new FileInputStream("/proc/meminfo"));
buffer = new BufferedReader(inputs);
String line = "";
while (true) {
line = buffer.readLine();
if (line == null)
break;
int beginIndex = 0;
int endIndex = line.indexOf(":");
if (endIndex != -1) {
String key = line.substring(beginIndex, endIndex);
beginIndex = endIndex + 1;
endIndex = line.length();
String memory = line.substring(beginIndex, endIndex);
String value = memory.replace("kB", "").trim();
map.put(key, value);
long memTotal = Long.parseLong(map.get("MemTotal").toString());
long memFree = Long.parseLong(map.get("MemFree").toString());
long memused = memTotal - memFree;
long buffers = Long.parseLong(map.get("Buffers").toString());
long cached = Long.parseLong(map.get("Cached").toString());
double usage = (double) (memused - buffers - cached) / memTotal * 100;
return (int) usage;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
buffer.close();
inputs.close();
} catch (Exception e2) {
e2.printStackTrace();
return 0;
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10229.html
cjavalinux相关文章
- Linux快速安装PHP的指南(linux一键安装php)
- 【Linux配置文件:驱动系统的调节器】(什么是linux配置文件)
- 保持系统时间更新:在Linux下完成(linux更新系统时间)
- 如何查看Linux系统版本信息(怎么看linux版本信息)
- 系统监控 Linux 系统:不容忽视的重要环节(监控linux)
- 掌握Linux系统FTP服务器配置技巧(linux设置ftp)
- 使用DNW烧写Linux系统:操作指南(dnw烧写linux)
- Linux系统下用户安全设置密码(linux用户设置密码)
- Linux下删除无效连接的方法(linux删除连接)
- Linux网络命令深度解析(linux网络命令详解)
- Linux安装PHP: 一步一步教你搞定(linux上安装php)
- Linux系统是否需要杀毒软件?(linux需要杀毒软件吗)
- 低配置电脑,搭载Linux系统上线(低配置电脑装linux)
- Linux粘滞位:安全着陆的新伙伴(linux粘滞位)
- Linux双机热备,保障应用安全(linux双机热备软件)
- 如何备份 Linux 系统?(备份linux系统)
- Linux平台编程语言全景观测:探索多样语言,拓展开发领域(linux下编程语言)
- 编译Linux内核-深入探索系统的运行机制(linux内核编译)
- 掌握Linux中df命令,精准监测磁盘空间利用率(linux中df命令)
- Linux技术人才需求旺盛:一个新机遇(linux的职位)
- Linux 一键查询配置,轻松掌握系统信息(linux查看配置情况)
- Linux下轻松解压:一步步操作攻略(解压linux)
- Linux下NTPD服务器构建实践(linux下ntp服务器)
- 深入探索Linux系统的配置内容(查看linux的配置)
- java利用反射实现动态代理示例