java 获取进程ID 物理内存 cpu使用百分比(linux环境)
2015-10-10 18:05
731 查看
http://hhhk.iteye.com/blog/1197434
java 获取进程ID
Java代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import org.apache.log4j.Logger;
import sun.management.ManagementFactory;
import com.ailk.ts.message.bean.MonitorInfoBean;
public class SystemInfoTools {
final static boolean isNotWindows = System.getProperties().getProperty("os.name").toLowerCase().indexOf("windows") < 0;
final static BigDecimal DIVISOR = BigDecimal.valueOf(1024);
private final static Logger logger = Logger.getLogger(SystemInfoTools.class);
public static int getPid(){
return Integer.parseInt(ManagementFactory.getRuntimeMXBean().getName().split("@")[0]);
}
public static MonitorInfoBean getMonitorInfoBean() {
MonitorInfoBean monitorInfo = new MonitorInfoBean();
if(!isNotWindows){
monitorInfo.setMemUsage(500);
return monitorInfo;
}
Runtime rt = Runtime.getRuntime();
BufferedReader in = null;
try {
int pid = getPid();
String[] cmd = {
"/bin/sh",
"-c",
"top -b -n 1 | grep " + pid
};
Process p = rt.exec(cmd);
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strArray = null;
while ((str = in.readLine()) != null) {
logger.debug("top: " + str);
int m = 0;
strArray = str.split(" ");
for (int i = 0; i < strArray.length; i++) {
String info = strArray[i];
if (info.trim().length() == 0){
continue;
}
if(m == 5) {//第5列为进程占用的物理内存值
String unit = info.substring(info.length() - 1);
if(unit.equalsIgnoreCase("g")) {
monitorInfo.setMemUseSize(Double.parseDouble(info));
} else if(unit.equalsIgnoreCase("m")) {
BigDecimal memUseSize = new BigDecimal(info.substring(0, info.length() - 1));
monitorInfo.setMemUseSize(memUseSize.divide(DIVISOR, 2, BigDecimal.ROUND_HALF_UP).doubleValue());
} else {
BigDecimal memUseSize = new BigDecimal(info).divide(DIVISOR);
monitorInfo.setMemUseSize(memUseSize.divide(DIVISOR, 2, BigDecimal.ROUND_HALF_UP).doubleValue());
}
}
if(m == 8) {//第9列为CPU的使用百分比
monitorInfo.setCpuUsage(Double.parseDouble(info));
}
if(m == 9) {//第10列为内存的使用百分比
monitorInfo.setMemUsage(Double.parseDouble(info));
}
m++;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return monitorInfo;
}
}
MonitorInfoBean.java
Java代码
public class MonitorInfoBean {
/** cpu使用率 */
private double cpuUsage;
/** 内存使用率 */
private double memUsage;
/** 内存使用的大小 */
private double memUseSize;
public double getCpuUsage() {
return cpuUsage;
}
public void setCpuUsage(double cpuUsage) {
this.cpuUsage = cpuUsage;
}
public double getMemUsage() {
return memUsage;
}
public void setMemUsage(double memUsage) {
this.memUsage = memUsage;
}
public double getMemUseSize() {
return memUseSize;
}
public void setMemUseSize(double memUseSize) {
this.memUseSize = memUseSize;
}
}
物理内存 cpu使用百分比(linux环境)
相关文章推荐
- linux 高并发socket通信模型
- linux下vim不显示高亮解决方案
- Linux中/var空间不足的解决办法
- linux设备树使用手册
- Linux下如何修改MySQL物理目录
- linux 信号注册处理发送signal
- Linux Pmap 命令:查看进程用了多少内存
- linux笔记 第二天 Linux系统入门及基础命令
- linux sar
- Linux 下系统调用的三种方法
- linux下利用vsftpd搭建ftp服务器
- GitHub Linux下使用方法
- Linux系统调用和库函数调用的区别
- Linux 文件操作总结
- linux sysbench
- linux 新建分区 、格式化 并挂载的命令
- linux-3.12.48内核向at91sam9g25平台移植小结
- CentOS6.4上单个目录下最大能建的文件个数
- Linux grep命令全析
- Linux 命令 - passwd: 更改用户密码