版權(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)頁內(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年度云南省高校教師資格證之高等教育法規(guī)能力檢測(cè)試卷B卷附答案
- 贛南師范大學(xué)《教育統(tǒng)計(jì)學(xué)》2021-2022學(xué)年第一學(xué)期期末試卷
- 阜陽師范大學(xué)《大學(xué)體育一》2021-2022學(xué)年第一學(xué)期期末試卷
- 蘇州市2024-2025學(xué)年六年級(jí)上學(xué)期11月期中調(diào)研數(shù)學(xué)試卷二(有答案)
- 福建師范大學(xué)協(xié)和學(xué)院《幼兒歌曲彈唱》2022-2023學(xué)年第一學(xué)期期末試卷
- 福建師范大學(xué)《專業(yè)色彩訓(xùn)練》2021-2022學(xué)年第一學(xué)期期末試卷
- 福建師范大學(xué)《學(xué)校團(tuán)體心理輔導(dǎo)》2022-2023學(xué)年第一學(xué)期期末試卷
- 2024二建管理點(diǎn)睛三小時(shí)講義(可打印版)
- 福建師范大學(xué)《體育保健學(xué)》2021-2022學(xué)年第一學(xué)期期末試卷
- 福建師范大學(xué)《環(huán)境工程實(shí)驗(yàn)》2023-2024學(xué)年第一學(xué)期期末試卷
- 電網(wǎng)運(yùn)行安全校核技術(shù)規(guī)范
- 汽車坡道玻璃雨棚施工方案
- 二輪復(fù)習(xí)微專題湖泊專題
- 2024年德陽發(fā)展控股集團(tuán)有限公司招聘筆試參考題庫附帶答案詳解
- 餐前檢查表(標(biāo)準(zhǔn)模版)
- 2022-2023學(xué)年廣東深圳福田區(qū)七年級(jí)上冊(cè)期中地理試卷及答案
- 關(guān)于小學(xué)數(shù)學(xué)課堂中數(shù)形結(jié)合教學(xué)的調(diào)查研究的開題報(bào)告
- 傳統(tǒng)文化的傳承和創(chuàng)新
- 2024春國開會(huì)計(jì)實(shí)務(wù)專題形考任務(wù)題庫及答案匯總
- 2024年科技部事業(yè)單位招聘95人歷年高頻考題難、易錯(cuò)點(diǎn)模擬試題(共500題)附帶答案詳解
- 2024年深圳市公務(wù)員考試申論真題A卷綜覽
評(píng)論
0/150
提交評(píng)論