博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux下使用java获取cpu、内存使用率
阅读量:4120 次
发布时间:2019-05-25

本文共 2762 字,大约阅读时间需要 9 分钟。

思路如下:Linux系统中可以用top命令查看进程使用CPU和内存情况,通过Runtime类的exec()方法执行命令"top”,获取"top"的输出,从而得到CPU和内存的使用情况。

使用top命令获取系统信息: 

top -b -n -1 | sed -n '3p'(使用sed命令将top输出内容中的第三行打印出来)

%Cpu(s):  6.5 us,  2.2 sy,  0.7 ni, 87.0 id,  3.5 wa,  0.0 hi,  0.1 si,  0.0 st

top -b -n 1 | sed -n '3p' | awk '{print $8}'(将第三行第八列打印出来)

87.0

获取单个进程CPU,内存的占用率 

cmd脚本命令:top -b -n 1 -p $pid |  sed -n '$p' 
上面的$pid,就是进程的PID 

Java Runtime类

每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。可以通过 getRuntime 方法获取当前运行时。 

应用程序不能创建自己的 Runtime 类实例。

示例程序(针对suse平台,如果是其他Linux,可能需要稍微修改程序)

import java.io.BufferedReader;import java.io.InputStreamReader;public class MySystem {	public static float getCpuUsage() {		float cpuUsage = 0;		float idleUsage = 0;		Runtime rt = Runtime.getRuntime();		String[] cmd = { "/bin/sh", "-c",				"top -b -n 1 | sed -n '3p' | awk '{print $5}'" };//如果使用的命令带有空格、重定向等,必须使用命令串(字符串数组)		BufferedReader in = null;		String str = "";		try{		Process p = rt.exec(cmd);		in = new BufferedReader(new InputStreamReader(p.getInputStream()));		str = in.readLine();		}catch(Exception e){					}		str = str.substring(0,3);		idleUsage = Float.parseFloat(str);		cpuUsage = 100 - idleUsage;		cpuUsage = FormatFloat.formatFloat(cpuUsage);		System.out.println("CpuUsage:");		System.out.println("	"+cpuUsage);		return cpuUsage;	}		public static void getCPUMEMByPID(){		Runtime rt = Runtime.getRuntime();		String[] cmd = { "/bin/sh", "-c",				"top -b -n 1 | sed -n '3p' | awk '{print $5}'" };		BufferedReader in = null;		String str = "";		try{		Process p = rt.exec(cmd);		in = new BufferedReader(new InputStreamReader(p.getInputStream()));		str = in.readLine();		}catch(Exception e){					}	}		public static float getMemUsage() {		long memUsed = 0;		long memTotal = 0;		float memUsage = 0;		Runtime rt = Runtime.getRuntime();		String[] cmd = { "/bin/sh", "-c",				"top -b -n 1 | sed -n '4p' | awk '{print $2 \"\t\" $4}'" };		BufferedReader in = null;		String str = "";		try{			Process p = rt.exec(cmd);			in = new BufferedReader(new InputStreamReader(p.getInputStream()));			str = in.readLine();		}catch(Exception e){					}				String[] mems = str.split("\t");		mems[0] = mems[0].substring(0,mems[0].length()-2);		memTotal = Long.parseLong(mems[0]);		mems[1] = mems[1].substring(0,mems[1].length()-2);		memUsed = Long.parseLong(mems[1]);		memUsage = (float) memUsed / memTotal * 100;		memUsage = FormatFloat.formatFloat(memUsage);		System.out.println("MemUsage:");		System.out.println("	"+memUsage);		return memUsage;	}}
获取cpu、内存的使用率还有其他方法

proc文件系统(http://www.cnblogs.com/yoleung/articles/1638922.html,http://blog.csdn.net/blue_jjw/article/details/8741000)

参考文章:http://zengjz88.iteye.com/blog/1595535 http://cumtyjp.blog.163.com/blog/static/7611480820093157512732/ http://blog.csdn.net/hemingwang0902/article/details/4054709

你可能感兴趣的文章
X-code beta 开发iWatch项目,运行没有错误,但是某些操作一点就崩,而且找不错误的原因场景一
查看>>
Xcode 报错: Extra argument in call
查看>>
iTunes Connect 上传APP报错: Communication error. please use diagnostic mode to check connectivity.
查看>>
#import <Cocoa/Cocoa.h> 报错 Lexical or Preprocessor Issue 'Cocoa/Cocoa.h' file not found
查看>>
`MQTTClient (~> 0.2.6)` required by `Podfile`
查看>>
X-Code 报错 ld: library not found for -lAFNetworking
查看>>
Bitcode
查看>>
If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
查看>>
3.5 YOLO9000: Better,Faster,Stronger(YOLO9000:更好,更快,更强)
查看>>
iOS菜鸟学习--如何避免两个按钮同时响应
查看>>
How to access the keys in dictionary in object-c
查看>>
iOS菜鸟学习—— NSSortDescriptor的使用
查看>>
hdu 3787 hdoj 3787
查看>>
hdu 3790 hdoj 3790
查看>>
hdu 3789 hdoj 3789
查看>>
hdu 3788 hdoj 3788
查看>>
zju 1003 zoj 1003
查看>>
zju 1004 zoj 1004
查看>>
zju 1005 zoj 1005
查看>>
zju 1006 zoj 1006
查看>>