1. 程式人生 > >Linux下使用java獲取cpu、記憶體使用率

Linux下使用java獲取cpu、記憶體使用率

原文地址:http://www.voidcn.com/article/p-yehrvmep-uo.html

思路如下:Linux系統中可以用top命令檢視程序使用CPU和記憶體情況,通過Runtime類的exec()方法執行命令"top”,獲取"top"的輸出,從而得到CPU和記憶體的使用情況。

使用top命令獲取系統資訊: 

top -b -n -1 | sed -n '3p'(使用sed命令將top輸出內容中的第三行打印出來)

%Cpu(s):  6.5 us,  2.2 sy,  0.7 ni, 87.0 id,  3.5 wa,  0.0 hi,  0.1 si,  0.0 st

top -b -n 1 | sed -n '3p' | awk '{print $8}'(將第三行第八列打印出來)

87.0

獲取單個程序CPU,記憶體的佔用率 

cmd指令碼命令:top -b -n 1 -p $pid |  sed -n '$p' 
上面的$pid,就是程序的PID 

Java Runtime類

每個 Java 應用程式都有一個 Runtime 類例項,使應用程式能夠與其執行的環境相連線。可以通過 getRuntime 方法獲取當前執行時。 

應用程式不能建立自己的 Runtime 類例項。

 

示例程式(針對suse平臺,如果是其他Linux,可能需要稍微修改程式)

 

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MySystem {

	public static float getCpuUsage() {
		float cpuUsage = 0;
		float idleUsage = 0;
		Runtime rt = Runtime.getRuntime();
		String[] cmd = { "/bin/sh", "-c",
				"top -b -n 1 | sed -n '3p' | awk '{print $5}'" };<span style="color:#ff0000;"><strong>//如果使用的命令帶有空格、重定向等,必須使用命令串(字串陣列)</strong></span>
		BufferedReader in = null;
		String str = "";
		try{
		Process p = rt.exec(cmd);
		in = new BufferedReader(new InputStreamReader(p.getInputStream()));
		str = in.readLine();
		}catch(Exception e){
			
		}
		str = str.substring(0,3);
		idleUsage = Float.parseFloat(str);
		cpuUsage = 100 - idleUsage;
		cpuUsage = FormatFloat.formatFloat(cpuUsage);
		System.out.println("CpuUsage:");
		System.out.println("	"+cpuUsage);
		return cpuUsage;
	}
	
	public static void getCPUMEMByPID(){
		Runtime rt = Runtime.getRuntime();
		String[] cmd = { "/bin/sh", "-c",
				"top -b -n 1 | sed -n '3p' | awk '{print $5}'" };
		BufferedReader in = null;
		String str = "";
		try{
		Process p = rt.exec(cmd);
		in = new BufferedReader(new InputStreamReader(p.getInputStream()));
		str = in.readLine();
		}catch(Exception e){
			
		}
	}
	
	public static float getMemUsage() {
		long memUsed = 0;
		long memTotal = 0;
		float memUsage = 0;
		Runtime rt = Runtime.getRuntime();
		String[] cmd = { "/bin/sh", "-c",
				"top -b -n 1 | sed -n '4p' | awk '{print $2 \"\t\" $4}'" };
		BufferedReader in = null;
		String str = "";
		try{
			Process p = rt.exec(cmd);
			in = new BufferedReader(new InputStreamReader(p.getInputStream()));
			str = in.readLine();
		}catch(Exception e){
			
		}
		
		String[] mems = str.split("\t");
		mems[0] = mems[0].substring(0,mems[0].length()-2);
		memTotal = Long.parseLong(mems[0]);
		mems[1] = mems[1].substring(0,mems[1].length()-2);
		memUsed = Long.parseLong(mems[1]);
		memUsage = (float) memUsed / memTotal * 100;
		memUsage = FormatFloat.formatFloat(memUsage);
		System.out.println("MemUsage:");
		System.out.println("	"+memUsage);
		return memUsage;
	}

}


獲取cpu、記憶體的使用率還有其他方法

 

proc檔案系統(http://www.cnblogs.com/yoleung/articles/1638922.html,http://blog.csdn.net/blue_jjw/article/details/8741000)

參考文章:http://zengjz88.iteye.com/blog/1595535 http://cumtyjp.blog.163.com/blog/static/7611480820093157512732/ http://blog.csdn.net/hemingwang0902/article/details/4054709

相關文章

相關標籤/搜尋