用java取得linux系统cpu、内存的实时信息
2008-01-05 08:31:47 来源:WEB开发网
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
* 取得linux系统下的cpu、内存信息
*
* <p>@author javer QQ:84831612</p>
* @date 2005
*/
public final class LinuxSystemTool
{
/**
* get memory by used info
*
* @return int[] result
* result.length==4;int[0]=MemTotal;int[1]=MemFree;int[2]=SwapTotal;int[3]=SwapFree;
* @throws IOException
* @throws InterruptedException
*/
public static int[] getMemInfo() throws IOException, InterruptedException
{
File file = new File("/PRoc/meminfo");
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));
int[] result = new int[4];
String str = null;
StringTokenizer token = null;
while((str = br.readLine()) != null)
{
token = new StringTokenizer(str);
if(!token.hasMoreTokens())
continue;
str = token.nextToken();
if(!token.hasMoreTokens())
continue;
if(str.equalsIgnoreCase("MemTotal:"))
result[0] = Integer.parseInt(token.nextToken());
else if(str.equalsIgnoreCase("MemFree:"))
result[1] = Integer.parseInt(token.nextToken());
else if(str.equalsIgnoreCase("SwapTotal:"))
result[2] = Integer.parseInt(token.nextToken());
else if(str.equalsIgnoreCase("SwapFree:"))
result[3] = Integer.parseInt(token.nextToken());
}
return result;
}
/**
* get memory by used info
*
* @return float efficiency
* @throws IOException
* @throws InterruptedException
*/
public static float getCpuInfo() throws IOException, InterruptedException
{
File file = new File("/proc/stat");
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));
StringTokenizer token = new StringTokenizer(br.readLine());
token.nextToken();
int user1 = Integer.parseInt(token.nextToken());
int nice1 = Integer.parseInt(token.nextToken());
int sys1 = Integer.parseInt(token.nextToken());
int idle1 = Integer.parseInt(token.nextToken());
Thread.sleep(1000);
br = new BufferedReader(
new InputStreamReader(new FileInputStream(file)));
token = new StringTokenizer(br.readLine());
token.nextToken();
int user2 = Integer.parseInt(token.nextToken());
int nice2 = Integer.parseInt(token.nextToken());
int sys2 = Integer.parseInt(token.nextToken());
int idle2 = Integer.parseInt(token.nextToken());
return (float)((user2 + sys2 + nice2) - (user1 + sys1 + nice1)) / (float)((user2 + nice2 + sys2 + idle2) - (user1 + nice1 + sys1 + idle1));
}
}
/**
* 测试类
*
* <p>@author javer QQ:84831612</p>
* @date 2005
*/
public class JaverTest
{
public static void main(String[] args) throws Exception
{
int[] memInfo = LinuxSystemTool.getMemInfo();
System.out.println("MemTotal:" + memInfo[0]);
System.out.println("MemFree:" + memInfo[1]);
System.out.println("SwapTotal:" + memInfo[2]);
System.out.println("SwapFree:" + memInfo[3]);
System.out.println("CPU利用率:" + LinuxSystemTool.getCpuInfo());
}
}
- ››JavaScript项目优化总结
- ››linux下两台服务器文件实时同步方案设计和实现
- ››Linux文件描述符中的close on exec标志位
- ››Linux下管道使用的一些限制
- ››Linux 误删/usr/bin 解决方法
- ››linux 添加新用户并赋予sudo执行权限
- ››Java获取特殊目录或特殊值
- ››linux常用软件安装方法
- ››Linux的分区已经被你从Windows中删除,系统启动后...
- ››linux enable命令大全
- ››Linux实现基于Loopback的NVI(NAT Virtual Interfa...
- ››Linux远程访问windows时,出现"连接被对端重...
更多精彩
赞助商链接