还未发布过话题
  • @lihuazhang 好的,那我重新编辑下~

  • 楼楼工具挺好用,我也借楼分享下自己写的一个监测 Cpu、内存的 app 小 demo。

    1.内存计算方式
        public static void getMemoryForPid(Context context,int pid,String fileName) {
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            int[] pids = {pid,};
            MemoryInfo[] memoryInfos = am.getProcessMemoryInfo(pids);
            ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
            am.getMemoryInfo(outInfo);
            long availMem = outInfo.availMem;
            long  totalMem = outInfo.totalMem;
            Utils.writeLog(fileName,"TotalPss:" + Utils.convertFileSize(memoryInfos[0].getTotalPss()) , true);
            Utils.writeLog(fileName,"TotalPrivateDirty:" + Utils.convertFileSize(memoryInfos[0].getTotalPrivateDirty()), true);
            Utils.writeLog(fileName,"TotalSharedDirty:" + Utils.convertFileSize(memoryInfos[0].getTotalSharedDirty()), true);   
    }
    

    2.Cpu 使用率,计划调用 com.android.internal.os.ProcessCpuTracker.getCpuTimeForPid(int pid) 获取线程 CpuTime,没有反射出来,就把这个方法摘出来了;系统 CpuTime 参照 com.android.internal.os.ProcessCpuTracker.update().

        public static long getCpuTimeForPid(int pid) {
            final String statFile = "/proc/" + pid + "/stat";
            final long[] statsData = new long[4];
            try {
                Class process = Class.forName("android.os.Process");
                Method read = process.getMethod("readProcFile", String.class,int[].class,String[].class,long[].class,float[].class);                  
                if ((Boolean)read.invoke(process.newInstance(), statFile, PROCESS_STATS_FORMAT,null, statsData, null)) {
                    long time = statsData[PROCESS_STAT_UTIME] + statsData[PROCESS_STAT_STIME];
                    //Log.e("CpuTimeForPid", String.valueOf(time));
                    return time;
                  }
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }      
            return 0;
        }
    
        public static long getCpuTotalTime() {
            final long[] sysCpu = new long[7];
            try {
                Class process = Class.forName("android.os.Process");
                Method[] methods = process.getMethods();
                Method read = process.getMethod("readProcFile", String.class,int[].class,String[].class,long[].class,float[].class);                  
                if ((Boolean)read.invoke(process.newInstance(), "/proc/stat", SYSTEM_CPU_FORMAT,null, sysCpu, null)) {
                    // Total user time is user + nice time.
                    final long usertime = sysCpu[0]+sysCpu[1];
                    // Total system time is simply system time.
                    final long systemtime = sysCpu[2];
                    // Total idle time is simply idle time.
                    final long idletime = sysCpu[3];
                    // Total irq time is iowait + irq + softirq time.
                    final long iowaittime = sysCpu[4];
                    final long irqtime = sysCpu[5];
                    final long softirqtime = sysCpu[6];
                    long totalTime = usertime + systemtime + idletime + irqtime + softirqtime;
                    //Log.e("CpuTime", String.valueOf(totalTime));
                    return totalTime;
                }
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InstantiationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return 0;   
        }
    
    }
    

    Cpu 使用率计算:按分钟 (60000) 采样 CpuTime,Cpu 使用率=(secPidCpuTime-firstPidCpuTime)/(secCpuTime-firstCpuTime)。
    3.开启监测后按分钟写日志信息到 Sdcard;内存、Cpu 使用率采用 achartengine-1.1.0.jar 以折线图的形式显示。
    手机型号 华为 P6 4.2.2
    源码:https://github.com/wsjunjin/AndroidTest/tree/master
    ps:第一次发长贴,有问题一起交流~

  • Android 常用 adb 命令总结 at 2015年08月18日

    赞,写的很系统

  • 好文

  • Android 性能测试实践 (一) at 2015年08月18日

    好贴