linux使用java获取cpu使用率,linux下用java程序获取cpu和内存的使用率

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 in = new BufferedReader(new InputStreamReader(p.getInputStream())); // in = new BufferedReader(new InputStreamReader(System.in)); String str = null; int linecount = 0; while ((str = in.

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

in = new BufferedReader(new InputStreamReader(p.getInputStream()));

// in = new BufferedReader(new InputStreamReader(System.in));

String str = null;

int linecount = 0;

while ((str = in.readLine()) != null) {

linecount++;

if (linecount == 4) {

String[] s = str.split("k ");

String memUsedstr = s[1];

String memTotalstr = s[0];

String memUsedstr1[] = memUsedstr.split(" ");

memUsed = Long.parseLong(memUsedstr1[memUsedstr1.length-1]);

String memTotalstr1[] = memTotalstr.split(" ");

memTotal = Long.parseLong(memTotalstr1[memTotalstr1.length-1]);

// System.out.println("IdleUsed:XXXXXXXXXXXX"+idleUsed);

memUsage = memUsed*100/memTotal;

// System.out.println(memUsed+"\n"+memTotal);

// System.out.println(memUsage);

// System.out.println(cpuUsed);

break;

}

}

// System.out.println(str);

}

// }

/*

}

catch (Exception e) {

e.printStackTrace();

} finally {

in.close();

}*/

return memUsage;

}

public static void main(String []args)

{

try

{

System.out.println(MySystem.getCpuUsage());

System.out.println(MySystem.getMemUsage());

}

catch(Exception e)

{

}

}

}

[1] [2]

test.jsp?url=http%3A%2F%2Fwww.chinaitlab.com%2Fcms%2Fimages%2Flogo14.gif&refer=http%3A%2F%2FLinux.chinaitlab.com%2Fadminister%2F960068_2.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值