linux下用java程序获取cpu和内存的使用率1(转)  2009-04-15 19:51

字号:    

http://hi.baidu.com/qmiao128/blog/item/fa91b41f1b6c1f61f724e424.html

项目组张布置说是要在linux实验平台上获取cpu和内存的使用率,刚开始想的是用jni调用本地方法,但是感觉这个方法太麻烦,主要也是对jni不怎么熟悉。后来在网上查到这个方法,经过改装,运行成功。

在这个程序中,我将getCpuUsage和getMemUsage两个方法封装到MySystem这个类中,声明为静态方法,这样可以直接通过类来调用方法,而不必创建新的对象。

这个程序主要还是针对我们实验平台linux系统来做的,如果要运行到其他的linux需要一定的修改。

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


public class MySystem {



public static double getCpuUsage() throws Exception {
   double cpuUsed = 0;
   double idleUsed = 0.0;
   Runtime rt = Runtime.getRuntime();
   Process p = rt.exec("top -b -n 1");// call "top" command in linux
   BufferedReader in = null;
   // try
   {
    in = new BufferedReader(new InputStreamReader(p.getInputStream()));
//    in = new BufferedReader(new InputStreamReader(System.in));
    String str = null;
    int linecount = 0;

    while ((str = in.readLine()) != null) {
     linecount++;
     if (linecount == 3) {
      String[] s = str.split("%");
      String idlestr = s[3];
      String idlestr1[] = idlestr.split(" ");
      idleUsed = Double.parseDouble(idlestr1[idlestr1.length-1]);
    //   System.out.println("IdleUsed:XXXXXXXXXXXX"+idleUsed);
      cpuUsed = 100-idleUsed;
    //   System.out.println(cpuUsed);
      break;
     }

    }
    // System.out.println(str);
   }
   //   }
   /*
    }
    catch (Exception e) {
    e.printStackTrace();
    } finally {
    in.close();
    }*/
   return cpuUsed;
}

public static double getMemUsage() throws Exception {
   long memUsed = 0;
   long memTotal = 0;
   double memUsage = 0.0;
   Runtime rt = Runtime.getRuntime();
   Process p = rt.exec("top -b -n 1");// call "top" command in linux
   BufferedReader in = null;
  
   // try
   {
    in = new BufferedReader(new InputStreamReader(p.getInputStream()));
//    in = new BufferedReader(new InputStreamReader(System.in));
    String str = null;
    int linecount = 0;

    while ((str = in.readLine()) != null) {
     linecount++;
     if (linecount == 4) {
      String[] s = str.split("k ");
      String memUsedstr = s[1];
      String memTotalstr = s[0];
      String memUsedstr1[] = memUsedstr.split(" ");
      memUsed = Long.parseLong(memUsedstr1[memUsedstr1.length-1]);
      String memTotalstr1[] = memTotalstr.split(" ");
      memTotal = Long.parseLong(memTotalstr1[memTotalstr1.length-1]);
    //   System.out.println("IdleUsed:XXXXXXXXXXXX"+idleUsed);
      memUsage = memUsed*100/memTotal;
    //   System.out.println(memUsed+"\n"+memTotal);
    //   System.out.println(memUsage);
    //   System.out.println(cpuUsed);
      break;
     }

    }
    // System.out.println(str);
   }
   //   }
   /*
    }
    catch (Exception e) {
    e.printStackTrace();
    } finally {
    in.close();
    }*/
   return memUsage;
}

public static void main(String []args)
{
   try
   {
    System.out.println(MySystem.getCpuUsage());
    System.out.println(MySystem.getMemUsage());
   }
   catch(Exception e)
   {
   
   }
}
 }