java怎样获取cpu主频和java查看cpu使用率率

&nbsp>&nbsp
&nbsp>&nbsp
&nbsp>&nbsp
利用java获取计算机cpu利用率和内存使用信息
摘要:利用java程序实现获取计算机cpu利用率和内存使用信息。创建一个Bean用来存贮要得到的信publicclassMonitorInfoBean{/**可使用内存.*/privatelongtotalM/**剩余内存.*/privatelongfreeM/**最大可使用内存.*/privatelongmaxM/**操作系统.*/privateStringosN/**总的物理内存.*/privatelongtotalMemorySiz
利用java程序实现获取计算机cpu利用率和内存使用信息。
创建一个Bean用来存贮要得到的信
public class MonitorInfoBean {
/** 可使用内存. */
private long totalM
/** 剩余内存. */
private long freeM
/** 最大可使用内存. */
private long maxM
/** 操作系统. */
private String osN
/** 总的物理内存. */
private long totalMemoryS
/** 剩余的物理内存. */
private long freePhysicalMemoryS
/** 已使用的物理内存. */
private long usedM
/** 线程总数. */
private int totalT
/** cpu使用率. */
private double cpuR
public long getFreeMemory() {
return freeM
public void setFreeMemory(long freeMemory) {
this.freeMemory = freeM
public long getFreePhysicalMemorySize() {
return freePhysicalMemoryS
public void setFreePhysicalMemorySize(long freePhysicalMemorySize) {
this.freePhysicalMemorySize = freePhysicalMemoryS
public long getMaxMemory() {
return maxM
public void setMaxMemory(long maxMemory) {
this.maxMemory = maxM
public String getOsName() {
return osN
public void setOsName(String osName) {
this.osName = osN
public long getTotalMemory() {
return totalM
public void setTotalMemory(long totalMemory) {
this.totalMemory = totalM
public long getTotalMemorySize() {
return totalMemoryS
public void setTotalMemorySize(long totalMemorySize) {
this.totalMemorySize = totalMemoryS
public int getTotalThread() {
return totalT
public void setTotalThread(int totalThread) {
this.totalThread = totalT
public long getUsedMemory() {
return usedM
public void setUsedMemory(long usedMemory) {
this.usedMemory = usedM
public double getCpuRatio() {
return cpuR
public void setCpuRatio(double cpuRatio) {
this.cpuRatio = cpuR
之后,建立bean的接口
public interface IMonitorService {
public MonitorInfoBean getMonitorInfoBean() throws E
然后,就是最关键的,得到cpu的利用率,已用内存,可用内存,最大内存等信息。
import java.io.InputStreamR
import java.io.LineNumberR
import sun.management.ManagementF
import com.sun.management.OperatingSystemMXB
import java.io.*;
import java.util.StringT
* 获取系统信息的业务逻辑实现类.
* @author GuoHuang
public class MonitorServiceImpl implements IMonitorService {
private static final int CPUTIME = 30;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;
private static final File versionFile = new File(&/proc/version&);
private static String linuxVersion =
* 获得当前的监控对象.
* @return 返回构造好的监控对象
* @throws Exception
* @author GuoHuang
public MonitorInfoBean getMonitorInfoBean() throws Exception {
int kb = 1024;
// 可使用内存
long totalMemory = Runtime.getRuntime().totalMemory() /
// 剩余内存
long freeMemory = Runtime.getRuntime().freeMemory() /
// 最大可使用内存
long maxMemory = Runtime.getRuntime().maxMemory() /
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean();
// 操作系统
String osName = System.getProperty(&os.name&);
// 总的物理内存
long totalMemorySize = osmxb.getTotalPhysicalMemorySize() /
// 剩余的物理内存
long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize() /
// 已使用的物理内存
long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb
.getFreePhysicalMemorySize())
// 获得线程总数
ThreadGroup parentT
for (parentThread = Thread.currentThread().getThreadGroup(); parentThread
.getParent() != parentThread = parentThread.getParent())
int totalThread = parentThread.activeCount();
double cpuRatio = 0;
if (osName.toLowerCase().startsWith(&windows&)) {
cpuRatio = this.getCpuRatioForWindows();
cpuRatio = this.getCpuRateForLinux();
// 构造返回对象
MonitorInfoBean infoBean = new MonitorInfoBean();
infoBean.setFreeMemory(freeMemory);
infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize);
infoBean.setMaxMemory(maxMemory);
infoBean.setOsName(osName);
infoBean.setTotalMemory(totalMemory);
infoBean.setTotalMemorySize(totalMemorySize);
infoBean.setTotalThread(totalThread);
infoBean.setUsedMemory(usedMemory);
infoBean.setCpuRatio(cpuRatio);
return infoB
private static double getCpuRateForLinux(){
InputStream is =
InputStreamReader isr =
BufferedReader brStat =
StringTokenizer tokenStat =
System.out.println(&Get usage rate of CUP , linux version: &+linuxVersion);
Process process = Runtime.getRuntime().exec(&top -b -n 1&);
is = process.getInputStream();
isr = new InputStreamReader(is);
brStat = new BufferedReader(isr);
if(linuxVersion.equals(&2.4&)){
brStat.readLine();
brStat.readLine();
brStat.readLine();
brStat.readLine();
tokenStat = new StringTokenizer(brStat.readLine());
tokenStat.nextToken();
tokenStat.nextToken();
String user = tokenStat.nextToken();
tokenStat.nextToken();
String system = tokenStat.nextToken();
tokenStat.nextToken();
String nice = tokenStat.nextToken();
System.out.println(user+& , &+system+& , &+nice);
user = user.substring(0,user.indexOf(&%&));
system = system.substring(0,system.indexOf(&%&));
nice = nice.substring(0,nice.indexOf(&%&));
float userUsage = new Float(user).floatValue();
float systemUsage = new Float(system).floatValue();
float niceUsage = new Float(nice).floatValue();
return (userUsage+systemUsage+niceUsage)/100;
brStat.readLine();
brStat.readLine();
tokenStat = new StringTokenizer(brStat.readLine());
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
String cpuUsage = tokenStat.nextToken();
System.out.println(&CPU idle : &+cpuUsage);
Float usage = new Float(cpuUsage.substring(0,cpuUsage.indexOf(&%&)));
return (1-usage.floatValue()/100);
} catch(IOException ioe){
System.out.println(ioe.getMessage());
freeResource(is, isr, brStat);
} finally{
freeResource(is, isr, brStat);
private static void freeResource(InputStream is, InputStreamReader isr, BufferedReader br){
if(is!=null)
is.close();
if(isr!=null)
isr.close();
if(br!=null)
br.close();
}catch(IOException ioe){
System.out.println(ioe.getMessage());
* 获得CPU使用率.
* @return 返回cpu使用率
* @author GuoHuang
private double getCpuRatioForWindows() {
String procCmd = System.getenv(&windir&)
+ &//system32//wbem//wmic.exe process get Caption,CommandLine,&
+ &KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount&;
// 取进程信息
long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
Thread.sleep(CPUTIME);
long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
if (c0 != null &;&; c1 != null) {
long idletime = c1[0] - c0[0];
long busytime = c1[1] - c0[1];
return Double.valueOf(
PERCENT * (busytime) / (busytime + idletime))
.doubleValue();
return 0.0;
} catch (Exception ex) {
ex.printStackTrace();
return 0.0;
* 读取CPU信息.
* @param proc
* @author GuoHuang
private long[] readCpu(final Process proc) {
long[] retn = new long[2];
proc.getOutputStream().close();
InputStreamReader ir = new InputStreamReader(proc.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line = input.readLine();
if (line == null || line.length() & FAULTLENGTH) {
int capidx = line.indexOf(&Caption&);
int cmdidx = line.indexOf(&CommandLine&);
int rocidx = line.indexOf(&ReadOperationCount&);
int umtidx = line.indexOf(&UserModeTime&);
int kmtidx = line.indexOf(&KernelModeTime&);
int wocidx = line.indexOf(&WriteOperationCount&);
long idletime = 0;
long kneltime = 0;
long usertime = 0;
while ((line = input.readLine()) != null) {
if (line.length() & wocidx) {
// 字段出现顺序:Caption,CommandLine,KernelModeTime,ReadOperationCount,
// ThreadCount,UserModeTime,WriteOperation
String caption = Bytes.substring(line, capidx, cmdidx - 1)
String cmd = Bytes.substring(line, cmdidx, kmtidx - 1).trim();
if (cmd.indexOf(&wmic.exe&) &= 0) {
// log.info(&line=&+line);
if (caption.equals(&System Idle Process&)
|| caption.equals(&System&)) {
idletime += Long.valueOf(
Bytes.substring(line, kmtidx, rocidx - 1).trim())
.longValue();
idletime += Long.valueOf(
Bytes.substring(line, umtidx, wocidx - 1).trim())
.longValue();
kneltime += Long.valueOf(
Bytes.substring(line, kmtidx, rocidx - 1).trim())
.longValue();
usertime += Long.valueOf(
Bytes.substring(line, umtidx, wocidx - 1).trim())
.longValue();
retn[1] = kneltime +
} catch (Exception ex) {
ex.printStackTrace();
} finally {
proc.getInputStream().close();
} catch (Exception e) {
e.printStackTrace();
/** 测试方法.
* @param args
* @throws Exception
* @author GuoHuang
public static void main(String[] args) throws Exception {
IMonitorService service = new MonitorServiceImpl();
MonitorInfoBean monitorInfo = service.getMonitorInfoBean();
System.out.println(&cpu占有率=& + monitorInfo.getCpuRatio());
System.out.println(&可使用内存=& + monitorInfo.getTotalMemory());
System.out.println(&剩余内存=& + monitorInfo.getFreeMemory());
System.out.println(&最大可使用内存=& + monitorInfo.getMaxMemory());
System.out.println(&操作系统=& + monitorInfo.getOsName());
System.out.println(&总的物理内存=& + monitorInfo.getTotalMemorySize() + &kb&);
System.out.println(&剩余的物理内存=& + monitorInfo.getFreeMemory() + &kb&);
System.out.println(&已使用的物理内存=& + monitorInfo.getUsedMemory() + &kb&);
System.out.println(&线程总数=& + monitorInfo.getTotalThread() + &kb&);
其中,Bytes类用来处理字符串
public class Bytes {
public static String substring(String src, int start_idx, int end_idx){
byte[] b = src.getBytes();
String tgt = &&;
for(int i=start_ i&=end_ i++){
tgt +=(char)b[i];
以上是的内容,更多
的内容,请您使用右上方搜索功能获取相关信息。
若你要投稿、删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内给你回复。
云服务器 ECS
可弹性伸缩、安全稳定、简单易用
&40.8元/月起
预测未发生的攻击
&24元/月起
为您提供0门槛上云实践机会
你可能还喜欢
你可能感兴趣
阿里云教程中心为您免费提供
利用java获取计算机cpu利用率和内存使用信息相关信息,包括
的信息,所有利用java获取计算机cpu利用率和内存使用信息相关内容均不代表阿里云的意见!投稿删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内答复
售前咨询热线
支持与服务
资源和社区
关注阿里云
International&>&java获取计算机cpu利用率和内存使用
java获取计算机cpu利用率和内存使用
上传大小:14KB
java 获取计算机cpu利用率和内存使用信息,需要的自己下载测试吧。
综合评分:4
下载个数:
{%username%}回复{%com_username%}{%time%}\
/*点击出现回复框*/
$(".respond_btn").on("click", function (e) {
$(this).parents(".rightLi").children(".respond_box").show();
e.stopPropagation();
$(".cancel_res").on("click", function (e) {
$(this).parents(".res_b").siblings(".res_area").val("");
$(this).parents(".respond_box").hide();
e.stopPropagation();
/*删除评论*/
$(".del_comment_c").on("click", function (e) {
var id = $(e.target).attr("id");
$.getJSON('/index.php/comment/do_invalid/' + id,
function (data) {
if (data.succ == 1) {
$(e.target).parents(".conLi").remove();
alert(data.msg);
$(".res_btn").click(function (e) {
var parentWrap = $(this).parents(".respond_box"),
q = parentWrap.find(".form1").serializeArray(),
resStr = $.trim(parentWrap.find(".res_area_r").val());
console.log(q);
//var res_area_r = $.trim($(".res_area_r").val());
if (resStr == '') {
$(".res_text").css({color: "red"});
$.post("/index.php/comment/do_comment_reply/", q,
function (data) {
if (data.succ == 1) {
var $target,
evt = e || window.
$target = $(evt.target || evt.srcElement);
var $dd = $target.parents('dd');
var $wrapReply = $dd.find('.respond_box');
console.log($wrapReply);
//var mess = $(".res_area_r").val();
var mess = resS
var str = str.replace(/{%header%}/g, data.header)
.replace(/{%href%}/g, 'http://' + window.location.host + '/user/' + data.username)
.replace(/{%username%}/g, data.username)
.replace(/{%com_username%}/g, data.com_username)
.replace(/{%time%}/g, data.time)
.replace(/{%id%}/g, data.id)
.replace(/{%mess%}/g, mess);
$dd.after(str);
$(".respond_box").hide();
$(".res_area_r").val("");
$(".res_area").val("");
$wrapReply.hide();
alert(data.msg);
}, "json");
/*删除回复*/
$(".rightLi").on("click", '.del_comment_r', function (e) {
var id = $(e.target).attr("id");
$.getJSON('/index.php/comment/do_comment_del/' + id,
function (data) {
if (data.succ == 1) {
$(e.target).parent().parent().parent().parent().parent().remove();
$(e.target).parents('.res_list').remove()
alert(data.msg);
//填充回复
function KeyP(v) {
var parentWrap = $(v).parents(".respond_box");
parentWrap.find(".res_area_r").val($.trim(parentWrap.find(".res_area").val()));
评论共有4条
这个资源和我找的一样
我的觉得写的不够详细
对于多内存条的情况有错误。
VIP会员动态
CSDN下载频道资源及相关规则调整公告V11.10
下载频道用户反馈专区
下载频道积分规则调整V1710.18
spring mvc+mybatis+mysql+maven+bootstrap 整合实现增删查改简单实例.zip
资源所需积分/C币
当前拥有积分
当前拥有C币
输入下载码
为了良好体验,不建议使用迅雷下载
java获取计算机cpu利用率和内存使用
会员到期时间:
剩余下载个数:
剩余积分:0
为了良好体验,不建议使用迅雷下载
积分不足!
资源所需积分/C币
当前拥有积分
您可以选择
程序员的必选
绿色安全资源
资源所需积分/C币
当前拥有积分
当前拥有C币
为了良好体验,不建议使用迅雷下载
资源所需积分/C币
当前拥有积分
当前拥有C币
为了良好体验,不建议使用迅雷下载
资源所需积分/C币
当前拥有积分
当前拥有C币
您的积分不足,将扣除 10 C币
为了良好体验,不建议使用迅雷下载
无法举报自己的资源
你当前的下载分为234。
你还不是VIP会员
开通VIP会员权限,免积分下载
你下载资源过于频繁,请输入验证码
您因违反CSDN下载频道规则而被锁定帐户,如有疑问,请联络:!
若举报审核通过,可返还被扣除的积分
被举报人:
rainbow0216
举报的资源分:
请选择类型
资源无法下载 ( 404页面、下载失败、资源本身问题)
资源无法使用 (文件损坏、内容缺失、题文不符)
侵犯版权资源 (侵犯公司或个人版权)
虚假资源 (恶意欺诈、刷分资源)
含色情、危害国家安全内容
含广告、木马病毒资源
*详细原因:
java获取计算机cpu利用率和内存使用博客分类:
JXM:Monitoring and Management Interface for the Java? Platform
通过jmx可以监控vm内存使用,系统内存使用等
以下是网上某博客代码,特点是通过window和linux命令获得CPU使用率。
利用java程序实现获取计算机cpu利用率和内存使用信息。
创建一个Bean用来存贮要得到的信
public class MonitorInfoBean {
/** 可使用内存. */
private long totalM
剩余内存. */
private long freeM
/** 最大可使用内存. */
private long maxM
/** 操作系统. */
private String osN
/** 总的物理内存. */
private long totalMemoryS
/** 剩余的物理内存. */
private long freePhysicalMemoryS
/** 已使用的物理内存. */
private long usedM
/** 线程总数. */
private int totalT
/** cpu使用率. */
private double cpuR
public long getFreeMemory() {
return freeM
public void setFreeMemory(long freeMemory) {
this.freeMemory = freeM
public long getFreePhysicalMemorySize() {
return freePhysicalMemoryS
public void setFreePhysicalMemorySize(long freePhysicalMemorySize) {
this.freePhysicalMemorySize = freePhysicalMemoryS
public long getMaxMemory() {
return maxM
public void setMaxMemory(long maxMemory) {
this.maxMemory = maxM
public String getOsName() {
return osN
public void setOsName(String osName) {
this.osName = osN
public long getTotalMemory() {
return totalM
public void setTotalMemory(long totalMemory) {
this.totalMemory = totalM
public long getTotalMemorySize() {
return totalMemoryS
public void setTotalMemorySize(long totalMemorySize) {
this.totalMemorySize = totalMemoryS
public int getTotalThread() {
return totalT
public void setTotalThread(int totalThread) {
this.totalThread = totalT
public long getUsedMemory() {
return usedM
public void setUsedMemory(long usedMemory) {
this.usedMemory = usedM
public double getCpuRatio() {
return cpuR
public void setCpuRatio(double cpuRatio) {
this.cpuRatio = cpuR
之后,建立bean的接口
public interface IMonitorService {
public MonitorInfoBean getMonitorInfoBean() throws E
然后,就是最关键的,得到cpu的利用率,已用内存,可用内存,最大内存等信息。
import java.io.InputStreamR
import java.io.LineNumberR
import sun.management.ManagementF
import com.sun.management.OperatingSystemMXB
import java.io.*;
import java.util.StringT
* 获取系统信息的业务逻辑实现类.
* @author GuoHuang
public class MonitorServiceImpl implements IMonitorService {
private static final int CPUTIME = 30;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;
private static final File versionFile = new File("/proc/version");
private static String linuxVersion =
* 获得当前的监控对象.
* @return 返回构造好的监控对象
* @throws Exception
* @author GuoHuang
public MonitorInfoBean getMonitorInfoBean() throws Exception {
int kb = 1024;
// 可使用内存
long totalMemory = Runtime.getRuntime().totalMemory() /
// 剩余内存
long freeMemory = Runtime.getRuntime().freeMemory() /
// 最大可使用内存
long maxMemory = Runtime.getRuntime().maxMemory() /
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean();
// 操作系统
String osName = System.getProperty("os.name");
// 总的物理内存
long totalMemorySize = osmxb.getTotalPhysicalMemorySize() /
// 剩余的物理内存
long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize() /
// 已使用的物理内存
long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb
.getFreePhysicalMemorySize())
// 获得线程总数
ThreadGroup parentT
for (parentThread = Thread.currentThread().getThreadGroup(); parentThread
.getParent() != parentThread = parentThread.getParent())
int totalThread = parentThread.activeCount();
double cpuRatio = 0;
if (osName.toLowerCase().startsWith("windows")) {
cpuRatio = this.getCpuRatioForWindows();
cpuRatio = this.getCpuRateForLinux();
// 构造返回对象
MonitorInfoBean infoBean = new MonitorInfoBean();
infoBean.setFreeMemory(freeMemory);
infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize);
infoBean.setMaxMemory(maxMemory);
infoBean.setOsName(osName);
infoBean.setTotalMemory(totalMemory);
infoBean.setTotalMemorySize(totalMemorySize);
infoBean.setTotalThread(totalThread);
infoBean.setUsedMemory(usedMemory);
infoBean.setCpuRatio(cpuRatio);
return infoB
private static double getCpuRateForLinux(){
InputStream is =
InputStreamReader isr =
BufferedReader brStat =
StringTokenizer tokenStat =
System.out.println("Get usage rate of CUP , linux version: "+linuxVersion);
Process process = Runtime.getRuntime().exec("top -b -n 1");
is = process.getInputStream();
isr = new InputStreamReader(is);
brStat = new BufferedReader(isr);
if(linuxVersion.equals("2.4")){
brStat.readLine();
brStat.readLine();
brStat.readLine();
brStat.readLine();
tokenStat = new StringTokenizer(brStat.readLine());
tokenStat.nextToken();
tokenStat.nextToken();
String user = tokenStat.nextToken();
tokenStat.nextToken();
String system = tokenStat.nextToken();
tokenStat.nextToken();
String nice = tokenStat.nextToken();
System.out.println(user+" , "+system+" , "+nice);
user = user.substring(0,user.indexOf("%"));
system = system.substring(0,system.indexOf("%"));
nice = nice.substring(0,nice.indexOf("%"));
float userUsage = new Float(user).floatValue();
float systemUsage = new Float(system).floatValue();
float niceUsage = new Float(nice).floatValue();
return (userUsage+systemUsage+niceUsage)/100;
brStat.readLine();
brStat.readLine();
tokenStat = new StringTokenizer(brStat.readLine());
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
String cpuUsage = tokenStat.nextToken();
System.out.println("CPU idle : "+cpuUsage);
Float usage = new Float(cpuUsage.substring(0,cpuUsage.indexOf("%")));
return (1-usage.floatValue()/100);
} catch(IOException ioe){
System.out.println(ioe.getMessage());
freeResource(is, isr, brStat);
} finally{
freeResource(is, isr, brStat);
private static void freeResource(InputStream is, InputStreamReader isr, BufferedReader br){
if(is!=null)
is.close();
if(isr!=null)
isr.close();
if(br!=null)
br.close();
}catch(IOException ioe){
System.out.println(ioe.getMessage());
* 获得CPU使用率.
* @return 返回cpu使用率
* @author GuoHuang
private double getCpuRatioForWindows() {
String procCmd = System.getenv("windir")
+ "\\system32\\wbem\\wmic.exe process get Caption,CommandLine,"
+ "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
// 取进程信息
long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
Thread.sleep(CPUTIME);
long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
if (c0 != null && c1 != null) {
long idletime = c1[0] - c0[0];
long busytime = c1[1] - c0[1];
return Double.valueOf(
PERCENT * (busytime) / (busytime + idletime))
.doubleValue();
return 0.0;
} catch (Exception ex) {
ex.printStackTrace();
return 0.0;
* 读取CPU信息.
* @param proc
* @author GuoHuang
private long[] readCpu(final Process proc) {
long[] retn = new long[2];
proc.getOutputStream().close();
InputStreamReader ir = new InputStreamReader(proc.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line = input.readLine();
if (line == null || line.length() & FAULTLENGTH) {
int capidx = line.indexOf("Caption");
int cmdidx = line.indexOf("CommandLine");
int rocidx = line.indexOf("ReadOperationCount");
int umtidx = line.indexOf("UserModeTime");
int kmtidx = line.indexOf("KernelModeTime");
int wocidx = line.indexOf("WriteOperationCount");
long idletime = 0;
long kneltime = 0;
long usertime = 0;
while ((line = input.readLine()) != null) {
if (line.length() & wocidx) {
// 字段出现顺序:Caption,CommandLine,KernelModeTime,ReadOperationCount,
// ThreadCount,UserModeTime,WriteOperation
String caption = Bytes.substring(line, capidx, cmdidx - 1)
String cmd = Bytes.substring(line, cmdidx, kmtidx - 1).trim();
if (cmd.indexOf("wmic.exe") &= 0) {
// log.info("line="+line);
if (caption.equals("System Idle Process")
|| caption.equals("System")) {
idletime += Long.valueOf(
Bytes.substring(line, kmtidx, rocidx - 1).trim())
.longValue();
idletime += Long.valueOf(
Bytes.substring(line, umtidx, wocidx - 1).trim())
.longValue();
kneltime += Long.valueOf(
Bytes.substring(line, kmtidx, rocidx - 1).trim())
.longValue();
usertime += Long.valueOf(
Bytes.substring(line, umtidx, wocidx - 1).trim())
.longValue();
retn[1] = kneltime +
} catch (Exception ex) {
ex.printStackTrace();
} finally {
proc.getInputStream().close();
} catch (Exception e) {
e.printStackTrace();
* @param args
* @throws Exception
* @author GuoHuang
public static void main(String[] args) throws Exception {
IMonitorService service = new MonitorServiceImpl();
MonitorInfoBean monitorInfo = service.getMonitorInfoBean();
System.out.println("cpu占有率=" + monitorInfo.getCpuRatio());
System.out.println("可使用内存=" + monitorInfo.getTotalMemory());
System.out.println("剩余内存=" + monitorInfo.getFreeMemory());
System.out.println("最大可使用内存=" + monitorInfo.getMaxMemory());
System.out.println("操作系统=" + monitorInfo.getOsName());
System.out.println("总的物理内存=" + monitorInfo.getTotalMemorySize() + "kb");
System.out.println("剩余的物理内存=" + monitorInfo.getFreeMemory() + "kb");
System.out.println("已使用的物理内存=" + monitorInfo.getUsedMemory() + "kb");
System.out.println("线程总数=" + monitorInfo.getTotalThread() + "kb");
其中,Bytes类用来处理字符串
public class Bytes {
public static String substring(String src, int start_idx, int end_idx){
byte[] b = src.getBytes();
String tgt = "";
for(int i=start_ i&=end_ i++){
tgt +=(char)b[i];
浏览: 458298 次
来自: 广州
用pageoffice把.可以实现在线的文档操作.直接转pdf ...
推荐一款轻量开源的支付宝组件:https://github.c ...
太好了,非常有用,谢谢分享~
http://www.atool.org/json2javab ...
这样只要是手机登录了微信的用户。扫描二维码后都可以登录进入网站 ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'

我要回帖

更多关于 java cpu使用率过高 的文章

 

随机推荐