




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、java获得CPU使用率,系统内存,虚拟机内存等情况(不用JNI)2009年10月23日星期五16:30Java取得这些信息,并且获得在Windows下的内存使用率MonitorlnfoBeanpackagecom.hmw.system.monitor;publicclassMonitorlnfoBean/*.*/privatelongtotalMemory;/*.*/privatelongfreeMemory;/*.*/privatelongmaxMemory;/*.*/privateStringosName;/*.*/privatelongtotalMemorySize;/*.*/priv
2、atelongfreePhysicalMemorySize;/*.*/privatelongusedMemory;.*/privateinttotalThread;/*cpu.*/privatedoublecpuRatio;publiclonggetFreeMemory()returnfreeMemory;publicvoidsetFreeMemory(longfreeMemory)this.freeMemory=freeMemory;publiclonggetFreePhysicalMemorySize()returnfreePhysicalMemorySize;publicvoidsetF
3、reePhysicalMemorySize(longfreePhysicalMemorySize)this.freePhysicalMemorySize=freePhysicalMemorySize;publiclonggetMaxMemory()returnmaxMemory;publicvoidsetMaxMemory(longmaxMemory)this.maxMemory=maxMemory;publicStringgetOsName()returnosName;publicvoidsetOsName(StringosName)this.osName=osName;publiclong
4、getTotalMemory()returntotalMemory;publicvoidsetTotalMemory(longtotalMemory)this.totalMemory=totalMemory;publiclonggetTotalMemorySize()returntotalMemorySize;publicvoidsetTotalMemorySize(longtotalMemorySize)this.totalMemorySize=totalMemorySize;publicintgetTotalThread()returntotalThread;publicvoidsetTo
5、talThread(inttotalThread)this.totalThread=totalThread;publiclonggetUsedMemory()returnusedMemory;publicvoidsetUsedMemory(longusedMemory)this.usedMemory=usedMemory;publicdoublegetCpuRatio()returncpuRatio;publicvoidsetCpuRatio(doublecpuRatio)this.cpuRatio=cpuRatio;packagecom.hmw.system.monitor;*/public
6、interfaceIMonitorService/*return返回构造好的监控对象*throwsException*/publicMonitorInfoBeangetMonitorInfoBean()throwsException;MonitorServicelmpIpackagecom.hmw.system.monitor;importjava.io.BufferedReader;importjava.io.lOException;importjava.io.lnputStream;importjava.io.lnputStreamReader;importjava.io.LineNumb
7、erReader;importjava.utiI.StringTokenizer;importsun.management.ManagementFactory;importcom.sun.management.OperatingSystemMXBean;*authorGuoHuang*/publicclassMonitorServiceImplimplementsIMonitorServiceprivatestaticfinalintCPUTIME=30;privatestaticfinalintPERCENT=100;privatestaticfinalintFAULTLENGTH=10;p
8、rivatestaticStringlinuxVersion=null;*return返回构造好的监控对象*throwsException*authorGuoHuang*/publicMonitorInfoBeangetMonitorInfoBean()throwsExceptionintkb=1024;/可使用内存longtotalMemory=Runtime.getRuntime().totalMemory()/kb;/剩余内存longfreeMemory=Runtime.getRuntime().freeMemory()/kb;/最大可使用内存longmaxMemory=Runtime.
9、getRuntime().maxMemory()/kb;OperatingSystemMXBeanosmxb=(OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();/StringosName=System.getProperty();/总的物理内存longtotalMemorySize=osmxb.getTotalPhysicalMemorySize()/kb;/剩余的物理内存longfreePhysicalMemorySize=osmxb.getFreePhysicalMemorySize()/kb;/已使用的
10、物理内存longusedMemory=(osmxb.getTotalPhysicalMemorySize()-osmxb.getFreePhysicalMemorySize()/kb;/获得线程总数ThreadGroupparentThread;for(parentThread=Thread.currentThread().getThreadGroup();parentThread.getParent()!=null;parentThread=parentThread.getParent();inttotalThread=parentThread.activeCount();doublecpu
11、Ratio=0;if(osName.toLowerCase().startsWith(windows)cpuRatio=this.getCpuRatioForWindows();elsecpuRatio=getCpuRateForLinux();/构造返回对象MonitorInfoBeaninfoBean=newMonitorInfoBean();infoBean.setFreeMemory(freeMemory);infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize);infoBean.setMaxMemory(maxMemory
12、);infoBean.setOsName(osName);infoBean.setTotalMemory(totalMemory);infoBean.setTotalMemorySize(totalMemorySize);infoBean.setTotalThread(totalThread);infoBean.setUsedMemory(usedMemory);infoBean.setCpuRatio(cpuRatio);returninfoBean;privatestaticdoublegetCpuRateForLinux()InputStreamis=null;InputStreamRe
13、aderisr=null;BufferedReaderbrStat=null;StringTokenizertokenStat=null;trySystem.out.println(GetusagerateofCUP,linuxversion:+linuxVersion);Processprocess=Runtime.getRuntime().exec(top-b-n1);is=process.getInputStream();isr=newInputStreamReader(is);brStat=newBufferedReader(isr);if(linuxVersion.equals(2.
14、4)brStat.readLine();brStat.readLine();brStat.readLine();brStat.readLine();tokenStat=newStringTokenizer(brStat.readLine();tokenStat.nextToken();tokenStat.nextToken();Stringuser=tokenStat.nextToken();tokenStat.nextToken();Stringsystem=tokenStat.nextToken();tokenStat.nextToken();Stringnice=tokenStat.ne
15、xtToken();System.out.println(user+,+system+,+nice);user=user.substring(0,user.indexOf(%);system=system.substring(0,system.indexOf(%);nice=nice.substring(0,nice.indexOf(%);floatuserUsage=newFloat(user).floatValue();floatsystemUsage=newFloat(system).floatValue();floatniceUsage=newFloat(nice).floatValu
16、e();return(userUsage+systemUsage+niceUsage)/100;elsebrStat.readLine();brStat.readLine();tokenStat=newStringTokenizer(brStat.readLine();tokenStat.nextToken();tokenStat.nextToken();tokenStat.nextToken();tokenStat.nextToken();tokenStat.nextToken();tokenStat.nextToken();tokenStat.nextToken();StringcpuUs
17、age=tokenStat.nextToken();System.out.println(CPUidle:+cpuUsage);Floatusage=newFloat(cpuUsage.substring(0,cpuUsage.indexOf(%);return(1-usage.floatValue()/100);catch(IOExceptionioe)System.out.println(ioe.getMessage();freeResource(is,isr,brStat);return1;finallyfreeResource(is,isr,brStat);privatestaticv
18、oidfreeResource(InputStreamis,InputStreamReaderisr,BufferedReaderbr)tryif(is!=null)is.close();if(isr!=null)isr.close();if(br!=null)br.close();catch(IOExceptionioe)System.out.println(ioe.getMessage();/*CPU*returncpuauthorGuoHuang*/privatedoublegetCpuRatioForWindows()tryStringprocCmd=System.getenv(win
19、dir)+system32wbemwmic.exeprocessgetCaption,CommandLine,KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount/取进程信息longc0=readCpu(Runtime.getRuntime().exec(procCmd);Thread.sleep(CPUTIME);longc1=readCpu(Runtime.getRuntime().exec(procCmd);if(c0!=null&c1!=null)longidletime=c10-c
20、00;longbusytime=c11-c01;returnDouble.valueOf(PERCENT*(busytime)/(busytime+idletime).doubleValue();elsereturn0.0;catch(Exceptionex)ex.printStackTrace();return0.0;/*CPUparamprocreturn*authorGuoHuang*/privatelongreadCpu(finalProcessproc)longretn=newlong2;tryproc.getOutputStream().close();InputStreamRea
21、derir=newInputStreamReader(proc.getInputStream();LineNumberReaderinput=newLineNumberReader(ir);Stringline=input.readLine();if(line=null|line.length()FAULTLENGTH)returnnull;intcapidx=line.indexOf(Caption);intcmdidx=line.indexOf(CommandLine);introcidx=line.indexOf(ReadOperationCount);intumtidx=line.in
22、dexOf(UserModeTime);intkmtidx=line.indexOf(KernelModeTime);intwocidx=line.indexOf(WriteOperationCount);longidletime=0;longkneltime=0;longusertime=0;while(line=input.readLine()!=null)if(line.length()=0)continue;Strings1=Bytes.substring(line,kmtidx,rocidx-1).trim();Strings2=Bytes.substring(line,umtidx
23、,wocidx-1).trim();if(caption.equals(SystemIdleProcess)|caption.equals(System)if(s1.length()0)idletime+=Long.valueOf(s1).longValue();if(s2.length()0)idletime+=Long.valueOf(s2).longValue();continue;if(s1.length()0)kneltime+=Long.valueOf(s1).longValue();if(s2.length()0)usertime+=Long.valueOf(s2).longVa
24、lue();retn0=idletime;retn1=kneltime+usertime;returnretn;catch(Exceptionex)ex.printStackTrace();finallytryproc.getInputStream().close();catch(Exceptione)e.printStackTrace();returnnull;/*.*paramargs*throwsException*authorGuoHuang*/publicstaticvoidmain(Stringargs)throwsExceptionIMonitorServiceservice=newMonitorServiceImpl();MonitorInfoBeanmonitorInfo=service.getMonitorInfo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 装饰公司员工劳动合同
- 私人房屋装修合同
- 2025年绥化绥棱县公开招聘急需紧缺专业工作人员2人考试参考试题及答案解析
- 2025西安三博脑科医院招聘(10人)考试参考试题及答案解析
- 2025年柳河县总工会公开招聘工会社会工作者(2人)备考练习题库及答案解析
- 2025年立定跳远试题及答案
- 2025云南楚雄州牟定县机关事务服务中心城镇招聘工作人员2人备考练习试题及答案解析
- 2025年防护工程试题及答案
- 2025四川成都经开区(龙泉驿区)面向社会招募医疗卫生辅助岗项目人员70人备考练习试题及答案解析
- 2025山东青岛大学附属医院外包人员招聘28人考试参考试题及答案解析
- 台球厅合伙协议合同范本
- 女装销售店长培训课件
- 服装厂质检知识培训内容课件
- 2025年潍坊市中考物理真题卷(含答案)
- 酒管专业导论考试题及答案
- 2025外研社小学英语四年级上册单词表(带音标)
- 2025至2030中国体育赛事行业市场发展分析及发展前景与投资报告
- 重点金融政策解读
- 工贸行业安全管理和企业现场常见隐患排查解读(1)精
- 2025年注册核安全工程师执业资格考试(核安全专业实务·核技术)历年参考题库含答案详解(5套)
- 2025上海市中学生行为规范
评论
0/150
提交评论