public class CpuUsage { public double getCpuUsage() throws Exception { double cpuUsed = 0; Runtime rt = Runtime.getRuntime(); Process p = rt.exec("top -b -n 1");// 调用系统的“top"命令 BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(p.getInputStream())); String str = null; String[] strArray = null; while ((str = in.readLine()) != null) { int m = 0; if (str.indexOf(" R ") != -1 && str.indexOf("top") == -1) {// 只分析正在运行的进程,top进程本身除外 strArray = str.split(" "); for (String tmp : strArray) { if (tmp.trim().length() == 0) continue; if (++m == 9) {// 第9列为CPU的使用百分比(RedHat 9) cpuUsed += Double.parseDouble(tmp); } } // System.out.println(str); } } } catch (Exception e) { e.printStackTrace(); } finally { in.close(); } return cpuUsed; } public static void main(String[] args) throws Exception { CpuUsage cpu = new CpuUsage(); System.out.println("cpu used:" + cpu.getCpuUsage() + "%"); } }
Exception in thread "main" java.io.IOException: CreateProcess: top -b -n 1 error=2 at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.<init>(ProcessImpl.java:81) at java.lang.ProcessImpl.start(ProcessImpl.java:30) at java.lang.ProcessBuilder.start(ProcessBuilder.java:451) at java.lang.Runtime.exec(Runtime.java:591) at java.lang.Runtime.exec(Runtime.java:429) at java.lang.Runtime.exec(Runtime.java:326) at CpuUsage.getCpuUsage(CpuUsage.java:8) at CpuUsage.main(CpuUsage.java:38)有bug啊
楼主【liyong_301】截止到2008-07-24 23:07:06的历史汇总数据(不包括此帖):
发帖的总数量:23 发帖的总分数:530 每贴平均分数:23
回帖的总数量:17 得分贴总数量:6 回帖的得分率:35%
结贴的总数量:1 结贴的总分数:10
无满意结贴数:0 无满意结贴分:0
未结的帖子数:22 未结的总分数:520
结贴的百分比:4.35 % 结分的百分比:1.89 %
无满意结贴率:0.00 % 无满意结分率:0.00 %
float freeMemory = (float) r.freeMemory();
float totalMemory = (float) r.totalMemory();
freeMemory:空闲内存
totalMemory:全部内存。
1-freeMemory/totalMemory = 当前内存使用率
public class CpuUsage {
public double getCpuUsage() throws Exception {
double cpuUsed = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("top -b -n 1");// 调用系统的“top"命令
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
if (str.indexOf(" R ") != -1 && str.indexOf("top") == -1) {// 只分析正在运行的进程,top进程本身除外
strArray = str.split(" ");
for (String tmp : strArray) {
if (tmp.trim().length() == 0)
continue;
if (++m == 9) {// 第9列为CPU的使用百分比(RedHat 9)
cpuUsed += Double.parseDouble(tmp);
}
}
// System.out.println(str);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return cpuUsed;
} public static void main(String[] args) throws Exception {
CpuUsage cpu = new CpuUsage();
System.out.println("cpu used:" + cpu.getCpuUsage() + "%");
}
}
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
at java.lang.Runtime.exec(Runtime.java:591)
at java.lang.Runtime.exec(Runtime.java:429)
at java.lang.Runtime.exec(Runtime.java:326)
at CpuUsage.getCpuUsage(CpuUsage.java:8)
at CpuUsage.main(CpuUsage.java:38)有bug啊
3楼的怎么改成给windows用呢???
3楼的怎么改成给windows用呢???
3楼的怎么改成给windows用呢???3楼的怎么改成给windows用呢???
3楼的怎么改成给windows用呢???
3楼的怎么改成给windows用呢???
3楼的怎么改成给windows用呢???
3楼的怎么改成给windows用呢???3楼的怎么改成给windows用呢???
3楼的怎么改成给windows用呢???
3楼的怎么改成给windows用呢???
3楼的怎么改成给windows用呢???
3楼的怎么改成给windows用呢???3楼的怎么改成给windows用呢???
3楼的怎么改成给windows用呢???