




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
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.getR
9、untime(.totalMemory( / 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("
10、"/ 總的物理內(nèi)存long totalMemorySize = osmxb.getTotalPhysicalMemorySize( / kb;/ 剩余的物理內(nèi)存long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize( / kb; / 已使用的物理內(nèi)存long usedMemory = (osmxb.getTotalPhysicalMemorySize( - osmxb.getFreePhysicalMemorySize(/ kb;/ 獲得線程總數(shù)ThreadGroup parentThread;for (parent
11、Thread = Thread.currentThread(.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.
12、getCpuRateForLinux(;/ 構(gòu)造返回對(duì)象MonitorInfoBean infoBean = new MonitorInfoBean(;infoBean.setFreeMemory(freeMemory;infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize;infoBean.setMaxMemory(maxMemory;infoBean.setOsName(osName;infoBean.setTotalMemory(totalMemory;infoBean.setTotalMemorySize(totalMemor
13、ySize;infoBean.setTotalThread(totalThread;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(&q
14、uot;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.
15、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+" ,
16、"+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
17、 = new Float(nice.floatValue(;return (userUsage+systemUsage+niceUsage/100;elsebrStat.readLine(;brStat.readLine(;tokenStat = new StringTokenizer(brStat.readLine(;tokenStat.nextToken(;tokenStat.nextToken(;tokenStat.nextToken(;tokenStat.nextToken(;tokenStat.nextToken(;tokenStat.nextToken(;tokenStat.nex
18、tToken(;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 ioeSystem.out.println(ioe.getMessage(;freeResource(is, isr, brStat;return 1
19、; finallyfreeResource(is, isr, brStat;private static void freeResource(InputStream is, InputStreamReader isr, BufferedReader brtryif(is!=nullis.close(;if(isr!=nullisr.close(;if(br!=nullbr.close(;catch(IOException ioeSystem.out.println(ioe.getMessage(;/* 獲得CPU使用率.* return 返回cpu使用率* author GuoHuang*/p
20、rivate double getCpuRatioForWindows( try String procCmd = System.getenv("windir"+ "system32wbemwmic.exeprocess get Caption,CommandLine,"+"KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationC ount"/ 取進(jìn)程信息long c0 = readCpu(Runtime.getRuntime(.exec(pr
21、ocCmd;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(PERCENT * (busytime / (busytime + idletime.doubleValue(; else return 0.0; catch (Exception ex ex.printStackTrac
22、e(;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 InputStreamReader(proc.getInputStream(; LineNumberReader input = new LineNumberReader(ir;String line = input.readL
23、ine(;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 = line.indexOf("UserModeTime"int kmtidx = line.indexOf("Ker
24、nelModeTime"int wocidx = line.indexOf("WriteOperationCount"long idletime = 0;long kneltime = 0;long usertime = 0;while (line = input.readLine( != null if (line.length( < wocidx continue;/ 字段出現(xiàn)順序:Caption,CommandLine,KernelModeTime,ReadOperationCount,/ ThreadCount,UserModeTime,WriteO
25、perationString caption = Bytes.substring(line, capidx, cmdidx - 1.trim(;String cmd = Bytes.substring(line, cmdidx, kmtidx - 1.trim(;if (cmd.indexOf("wmic.exe" >= 0 continue;/ ("line="+line;if (caption.equals("System Idle Process"| caption.equals("System&
26、quot; 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(Bytes.subst
27、ring(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;/* 測(cè)試方法.* param args* throws Exception* author GuoHuang*/public static void main(String args throws Exception IMonitorService service = new MonitorServiceImpl(;MonitorInfoBean monitorInfo = serv
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 黑龍江高三三模數(shù)學(xué)試卷
- 2025屆亳州市重點(diǎn)中學(xué)高二物理第二學(xué)期期末聯(lián)考試題含解析
- 2025年中國(guó)電池級(jí)金屬鋰行業(yè)市場(chǎng)發(fā)展前景及發(fā)展趨勢(shì)與投資戰(zhàn)略研究報(bào)告
- 中國(guó)橡膠自粘帶市場(chǎng)運(yùn)行調(diào)查報(bào)告
- 2025年中國(guó)軸用直爪卡簧鉗行業(yè)市場(chǎng)發(fā)展前景及發(fā)展趨勢(shì)與投資戰(zhàn)略研究報(bào)告
- 2024年免燒磚機(jī)項(xiàng)目資金申請(qǐng)報(bào)告代可行性研究報(bào)告
- 藥品網(wǎng)絡(luò)銷售監(jiān)督管理辦法
- 葫蘆島木材加工管理辦法
- 虹口區(qū)進(jìn)口水泵管理辦法
- 行政事業(yè)類收費(fèi)管理辦法
- 他汀不耐受的臨床診斷與處理中國(guó)專家共識(shí)(2024)解讀課件
- 2024年7月國(guó)家開放大學(xué)法學(xué)本科《知識(shí)產(chǎn)權(quán)法》期末考試試題及答案
- 2024移動(dòng)金融客戶端應(yīng)用軟件安全管理規(guī)范標(biāo)準(zhǔn)
- DB43T 876.8-2015 高標(biāo)準(zhǔn)農(nóng)田建設(shè) 第8部分:科技服務(wù)
- 2025版《新亮劍》高中物理:第九章 靜電場(chǎng)及其應(yīng)用 靜電場(chǎng)中的能量含答案
- 普通洗車操作流程及操作指導(dǎo)書
- 40000平方米人民醫(yī)院項(xiàng)目監(jiān)理招標(biāo)文件
- 2024年遼寧電工(高級(jí)技師)高頻核心題庫(kù)300題(含解析)
- 北師大版二年級(jí)下冊(cè)豎式計(jì)算題練習(xí)200道及答案
- JC-T 902-2002 建筑表面用有機(jī)硅防水劑
- DL∕T 5539-2018 采動(dòng)影響區(qū)架空輸電線路設(shè)計(jì)規(guī)范
評(píng)論
0/150
提交評(píng)論