java获取进程id 读写内存_java 获取进程ID 物理内存 cpu使用百分比(linux环境)

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

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值