java程序?qū)崿F(xiàn)獲取計(jì)算機(jī)cpu利用率和內(nèi)存使用信息的代碼_第1頁
java程序?qū)崿F(xiàn)獲取計(jì)算機(jī)cpu利用率和內(nèi)存使用信息的代碼_第2頁
java程序?qū)崿F(xiàn)獲取計(jì)算機(jī)cpu利用率和內(nèi)存使用信息的代碼_第3頁
java程序?qū)崿F(xiàn)獲取計(jì)算機(jī)cpu利用率和內(nèi)存使用信息的代碼_第4頁
java程序?qū)崿F(xiàn)獲取計(jì)算機(jī)cpu利用率和內(nèi)存使用信息的代碼_第5頁
已閱讀5頁,還剩14頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、利用java程序?qū)崿F(xiàn)獲取計(jì)算機(jī)cpu利用率和內(nèi)存使用信息。創(chuàng)建一個(gè)Bean用來存貯要得到的信public class MonitorInfoBean /* 可使用內(nèi)存. */private long totalMemory;/* 剩余內(nèi)存. */private long freeMemory;/* 最大可使用內(nèi)存. */private long maxMemory;/* 操作系統(tǒng). */private String osName;/* 總的物理內(nèi)存. */private long totalMemorySize;/* 剩余的物理內(nèi)存. */private long freePhysicalMe

2、morySize;/* 已使用的物理內(nèi)存. */private long usedMemory;/* 線程總數(shù). */private int totalThread;/* cpu使用率. */private double cpuRatio;public long getFreeMemory( return freeMemory;public void setFreeMemory(long freeMemory this.freeMemory = freeMemory;public long getFreePhysicalMemorySize( return freePhysicalMemory

3、Size;public void setFreePhysicalMemorySize(long freePhysicalMemorySize this.freePhysicalMemorySize = freePhysicalMemorySize;public long getMaxMemory( return maxMemory;public void setMaxMemory(long maxMemory this.maxMemory = maxMemory;public String getOsName( return osName;public void setOsName(Strin

4、g osName this.osName = osName;public long getTotalMemory( return totalMemory;public void setTotalMemory(long totalMemory this.totalMemory = totalMemory;public long getTotalMemorySize( return totalMemorySize;public void setTotalMemorySize(long totalMemorySize this.totalMemorySize = totalMemorySize;pu

5、blic int getTotalThread( return totalThread;public void setTotalThread(int totalThread this.totalThread = totalThread;public long getUsedMemory( return usedMemory;public void setUsedMemory(long usedMemory this.usedMemory = usedMemory;public double getCpuRatio( return cpuRatio;public void setCpuRatio

6、(double cpuRatio this.cpuRatio = cpuRatio;之后,建立bean的接口public interface IMonitorService public MonitorInfoBean getMonitorInfoBean( throws Exception;然后,就是最關(guān)鍵的,得到cpu的利用率,已用內(nèi)存,可用內(nèi)存,最大內(nèi)存等信息。import java.io.InputStreamReader;import java.io.LineNumberReader;import sun.management.ManagementFactory;import com

7、.sun.management.OperatingSystemMXBean;import java.io.*;import java.util.StringTokenizer;/* 獲取系統(tǒng)信息的業(yè)務(wù)邏輯實(shí)現(xiàn)類.* author GuoHuang*/public class MonitorServiceImpl implements IMonitorService private static final int CPUTIME = 30;private static final int PERCENT = 100;private static final int FAULTLENGTH =

8、10;private static final File versionFile = new File(/proc/version;private static String linuxVersion = null;/* 獲得當(dāng)前的監(jiān)控對(duì)象.* return 返回構(gòu)造好的監(jiān)控對(duì)象* throws Exception* author GuoHuang*/public MonitorInfoBean getMonitorInfoBean( throws Exception int kb = 1024;/ 可使用內(nèi)存long totalMemory = Runtime.getRuntime(.tot

9、alMemory( / kb;/ 剩余內(nèi)存long freeMemory = Runtime.getRuntime(.freeMemory( / kb;/ 最大可使用內(nèi)存long maxMemory = Runtime.getRuntime(.maxMemory( / kb;OperatingSystemMXBean osmxb = (OperatingSystemMXBean ManagementFactory.getOperatingSystemMXBean(;/ 操作系統(tǒng)String osName = System.getProperty(;/ 總的物理內(nèi)存long tot

10、alMemorySize = osmxb.getTotalPhysicalMemorySize( / kb;/ 剩余的物理內(nèi)存long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize( / kb; / 已使用的物理內(nèi)存long usedMemory = (osmxb.getTotalPhysicalMemorySize( - osmxb.getFreePhysicalMemorySize(/ kb;/ 獲得線程總數(shù)ThreadGroup parentThread;for (parentThread = Thread.curren

11、tThread(.getThreadGroup(; parentThread .getParent( != null; parentThread = parentThread.getParent(;int totalThread = parentThread.activeCount(;double cpuRatio = 0;if (osName.toLowerCase(.startsWith(windows cpuRatio = this.getCpuRatioForWindows(;else cpuRatio = this.getCpuRateForLinux(;/ 構(gòu)造返回對(duì)象Monito

12、rInfoBean infoBean = new MonitorInfoBean(;infoBean.setFreeMemory(freeMemory;infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize;infoBean.setMaxMemory(maxMemory;infoBean.setOsName(osName;infoBean.setTotalMemory(totalMemory;infoBean.setTotalMemorySize(totalMemorySize;infoBean.setTotalThread(tota

13、lThread;infoBean.setUsedMemory(usedMemory;infoBean.setCpuRatio(cpuRatio;return infoBean;private static double getCpuRateForLinux(InputStream is = null;InputStreamReader isr = null;BufferedReader brStat = null;StringTokenizer tokenStat = null;trySystem.out.println(Get usage rate of CUP , linux versio

14、n: +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.4brStat.readLine(;brStat.readLine(;brStat.readLine(;brStat.readLine(;tokenStat = new StringTokenizer(brStat.rea

15、dLine(;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.sub

16、string(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;elsebrStat.readLine(;brStat.readLine(;tokenStat = ne

17、w 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.substr

18、ing(0,cpuUsage.indexOf(%;return (1-usage.floatValue(/100; catch(IOException ioeSystem.out.println(ioe.getMessage(;freeResource(is, isr, brStat;return 1; finallyfreeResource(is, isr, brStat;private static void freeResource(InputStream is, InputStreamReader isr, BufferedReader brtryif(is!=nullis.close

19、(;if(isr!=nullisr.close(;if(br!=nullbr.close(;catch(IOException ioeSystem.out.println(ioe.getMessage(;/* 獲得CPU使用率.* return 返回cpu使用率* author GuoHuang*/private double getCpuRatioForWindows( try String procCmd = System.getenv(windir+ system32wbemwmic.exeprocess get Caption,CommandLine,+KernelModeTime,R

20、eadOperationCount,ThreadCount,UserModeTime,WriteOperationC ount;/ 取進(jìn)程信息long c0 = readCpu(Runtime.getRuntime(.exec(procCmd;Thread.sleep(CPUTIME;long c1 = readCpu(Runtime.getRuntime(.exec(procCmd;if (c0 != null & c1 != null long idletime = c10 - c00;long busytime = c11 - c01;return Double.valueOf(PERC

21、ENT * (busytime / (busytime + idletime.doubleValue(; else return 0.0; catch (Exception ex ex.printStackTrace(;return 0.0;/* 讀取CPU信息.* param proc* return* author GuoHuang*/private long readCpu(final Process proc long retn = new long2;try proc.getOutputStream(.close(;InputStreamReader ir = new InputSt

22、reamReader(proc.getInputStream(; LineNumberReader input = new LineNumberReader(ir;String line = input.readLine(;if (line = null | line.length( FAULTLENGTH return null;int capidx = line.indexOf(Caption;int cmdidx = line.indexOf(CommandLine;int rocidx = line.indexOf(ReadOperationCount;int umtidx = lin

23、e.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( = 0 continue;/ (line=+line;if (caption.equals(System Idle Process| captio

24、n.equals(System idletime += Long.valueOf(Bytes.substring(line, kmtidx, rocidx - 1.trim(.longValue(;idletime += Long.valueOf(Bytes.substring(line, umtidx, wocidx - 1.trim(.longValue(;continue;kneltime += Long.valueOf(Bytes.substring(line, kmtidx, rocidx - 1.trim(.longValue(;usertime += Long.valueOf(B

25、ytes.substring(line, umtidx, wocidx - 1.trim(.longValue(;retn0 = idletime;retn1 = kneltime + usertime;return retn; catch (Exception ex ex.printStackTrace(; finally try proc.getInputStream(.close(; catch (Exception e e.printStackTrace(;return null;/* 測試方法.* param args* throws Exception* author GuoHuang*/public static void main(String args throws Exc

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論