欢迎来到天天文库
浏览记录
ID:15388595
大小:58.00 KB
页数:17页
时间:2018-08-03
《java获得cpu使用率》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、java获得CPU使用率,系统内存,虚拟机内存等情况(不用JNI)2009年10月23日星期五16:30在Java中,可以获得总的物理内存、剩余的物理内存、已使用的物理内存等信息,下面例子可以取得这些信息,并且获得在Windows下的内存使用率。 首先编写一个MonitorInfoBean类,用来装载监控的一些信息,包括物理内存、剩余的物理内存、已使用的物理内存、内存使用率等字段,该类的代码如下:packagecom.hmw.system.monitor;publicclassMonitorInfoBean{/**可使用内存.*/privatelongtotalMemo
2、ry;/**剩余内存.*/privatelongfreeMemory;/**最大可使用内存.*/privatelongmaxMemory;/**操作系统.*/privateStringosName;/**总的物理内存.*/privatelongtotalMemorySize;/**剩余的物理内存.*/privatelongfreePhysicalMemorySize;/**已使用的物理内存.*/privatelongusedMemory;/**线程总数.*/privateinttotalThread;/**cpu使用率.*/privatedoublecpuRatio;publ
3、iclonggetFreeMemory(){returnfreeMemory;}publicvoidsetFreeMemory(longfreeMemory){this.freeMemory=freeMemory;}publiclonggetFreePhysicalMemorySize(){returnfreePhysicalMemorySize;}publicvoidsetFreePhysicalMemorySize(longfreePhysicalMemorySize){this.freePhysicalMemorySize=freePhysicalMemorySize;
4、}publiclonggetMaxMemory(){returnmaxMemory;}publicvoidsetMaxMemory(longmaxMemory){this.maxMemory=maxMemory;}publicStringgetOsName(){returnosName;}publicvoidsetOsName(StringosName){this.osName=osName;}publiclonggetTotalMemory(){returntotalMemory;}publicvoidsetTotalMemory(longtotalMemory){this
5、.totalMemory=totalMemory;}publiclonggetTotalMemorySize(){returntotalMemorySize;}publicvoidsetTotalMemorySize(longtotalMemorySize){this.totalMemorySize=totalMemorySize;}publicintgetTotalThread(){returntotalThread;}publicvoidsetTotalThread(inttotalThread){this.totalThread=totalThread;}publicl
6、onggetUsedMemory(){returnusedMemory;}publicvoidsetUsedMemory(longusedMemory){this.usedMemory=usedMemory;}publicdoublegetCpuRatio(){returncpuRatio;}publicvoidsetCpuRatio(doublecpuRatio){this.cpuRatio=cpuRatio;}}接着编写一个获得当前的监控信息的接口,该类的代码如下所示:packagecom.hmw.system.monitor;/***获取系统信息的业务逻辑类接口.*/p
7、ublicinterfaceIMonitorService{/***获得当前的监控对象.*@return返回构造好的监控对象*@throwsException*/publicMonitorInfoBeangetMonitorInfoBean()throwsException;}该类的实现类MonitorServiceImpl如下所示:packagecom.hmw.system.monitor;importjava.io.BufferedReader;importjava.io.IOException;i
此文档下载收益归作者所有