Android-获取内存,当前/总cpu信息

内存信息中最有用的就是PSS了,还有各种零碎的,在android.os.Debug中可以直接拿到MemeryInfo

 

MemoryInfo utInfo = new MemoryInfo();
android.os.Debug.getMemoryInfo(utInfo);

info.dalvikPrivateDirty = utInfo.dalvikPrivateDirty;
info.dalvikPss = utInfo.dalvikPss;
info.dalvikSharedDirty = utInfo.dalvikSharedDirty;
info.nativePrivateDirty = utInfo.nativePrivateDirty;
info.nativePss = utInfo.nativePss;
info.nativeSharedDirty = utInfo.nativeSharedDirty;
info.otherPss = utInfo.otherPss;
info.otherPrivateDirty = utInfo.otherPrivateDirty;
info.otherSharedDirty = utInfo.otherSharedDirty;
info.totalPss = utInfo.getTotalPss();

cpu信息分两类,总的cpu使用率,取proc/stat的数据,这个样子

cpu  192369 7119 480152 122044337 14142 9937 26747 0 0

取到之后解析,拿到使用时间和空闲时间

隔一段时间后,再取一次数据,即可得到cpu使用时间

private float readUsage() {
    try {
        RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
        String load = reader.readLine();

        String[] toks = load.split(" ");

        long idle1 = Long.parseLong(toks[5]);
        long cpu1 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4])
              + Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);

        try {
            Thread.sleep(360);
        } catch (Exception e) {}

        reader.seek(0);
        load = reader.readLine();
        reader.close();

        toks = load.split(" ");

        long idle2 = Long.parseLong(toks[5]);
        long cpu2 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4])
            + Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);

        return (float)(cpu2 - cpu1) / ((cpu2 + idle2) - (cpu1 + idle1));

    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return 0;
}

当前进程的cpu使用率计算方法和总的相似,只不过路径变成了proc/<pid>/stat

Table 1-3: Contents of the stat files (as of 2.6.22-rc3)
..............................................................................
 Field          Content
  pid           process id
  tcomm         filename of the executable
  state         state (R is running, S is sleeping, D is sleeping in an
                uninterruptible wait, Z is zombie, T is traced or stopped)
  ppid          process id of the parent process
  pgrp          pgrp of the process
  sid           session id
  tty_nr        tty the process uses
  tty_pgrp      pgrp of the tty
  flags         task flags
  min_flt       number of minor faults
  cmin_flt      number of minor faults with child's
  maj_flt       number of major faults
  cmaj_flt      number of major faults with child's
  utime         user mode jiffies
  stime         kernel mode jiffies
  cutime        user mode jiffies with child's
  cstime        kernel mode jiffies with child's

解析数据代码如下,如果不计算子进程时间则只用toks[13]和toks[14]

reader = new RandomAccessFile("/proc/" + android.os.Process.myPid() + "/stat", "r");
String currentStat = reader.readLine();
toks = currentStat.split(" ");
// include child  or (13 + 14)
current_process_usetime = Long.parseLong(toks[13]) + Long.parseLong(toks[14]) + Long.parseLong(toks[15])
        + Long.parseLong(toks[16]);
reader.close();

取到之后除以总的cpu时间,即

long totalCPUTime = (cpu2 + idle2) - (cpu1 + idle1);
currentCPUUsage = (float)(current_process_usetime - last_process_usetime) / totalCPUTime;

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注