如何通过应用程序获取内存使用率和cpu使用率? [英] how to get memory usage and cpu usage by application?

查看:100
本文介绍了如何通过应用程序获取内存使用率和cpu使用率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了这段代码来获得总体的CPU使用率。这有可能将其转换为按进程告诉CPU使用情况吗?

I found this code to get overall cpu usage. is this possible to convert this to tell cpu usage by process? Is there any API by which we can get CPU or Memory usage of android?

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;
}


推荐答案

Context context = this.getApplicationContext();
ActivityManager mgr = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processes = mgr.getRunningAppProcesses();
Log.e("DEBUG", "Running processes:");
for(Iterator i = processes.iterator(); i.hasNext(); )
{
    RunningAppProcessInfo p = (RunningAppProcessInfo)i.next();
    Log.e("DEBUG", "  process name: "+p.processName);
    Log.e("DEBUG", "     pid: "+p.pid);                    
    int[] pids = new int[1];
    pids[0] = p.pid;
    android.os.Debug.MemoryInfo[] MI = mgr.getProcessMemoryInfo(pids);
    Log.e("memory","     dalvik private: " + MI[0].dalvikPrivateDirty);
    Log.e("memory","     dalvik shared: " + MI[0].dalvikSharedDirty);
    Log.e("memory","     dalvik pss: " + MI[0].dalvikPss);            
    Log.e("memory","     native private: " + MI[0].nativePrivateDirty);
    Log.e("memory","     native shared: " + MI[0].nativeSharedDirty);
    Log.e("memory","     native pss: " + MI[0].nativePss);            
    Log.e("memory","     other private: " + MI[0].otherPrivateDirty);
    Log.e("memory","     other shared: " + MI[0].otherSharedDirty);
    Log.e("memory","     other pss: " + MI[0].otherPss);

    Log.e("memory","     total private dirty memory (KB): " + MI[0].getTotalPrivateDirty());
    Log.e("memory","     total shared (KB): " + MI[0].getTotalSharedDirty());
    Log.e("memory","     total pss: " + MI[0].getTotalPss());            
}




  • 在现代操作系统中,应用程序使用共享库。因此,多个应用程序会使用一些内存
    ,从而使确定应用程序的内存使用情况变得复杂。

    • In modern OS, app use shared libraries. Hence, some memory is used by multiple apps, complicating determining an apps memory usage.

      dalvikPrivateDirty是由Java释放的内存
      如果进程被杀死,则
      虚拟机

      dalvikPrivateDirty is the memory that would be freed by the java
      virtual machine if the process is killed

      otherPrivateDirty dalvikSharedDirty是
      Java虚拟机使用的共享内存,但这不会被释放

      otherPrivateDirty dalvikSharedDirty is the shared memory used by the java virtual machine But this would not be freed

      如果此应用被杀死了dalvikPss –估计该应用使用了多少内存
      。这包括所有专用内存,以及共享内存的

      部分。检查pss> = private原因

      仅使用了一部分共享内存的原因是
      >
      所有负责的应用程序之间共享内存使用的合理性

      if this app is killed dalvikPss – an estimate of how much memory is
      used by the app. This includes all the private memory, and a
      fraction of the shared memory Check that pss >= private The reason
      that only a fraction of shared memory is used is so that
      reasonability of the shared memory usage across all responsible apps

      此值用于估计内存应用程序的负载。

      This value is used to estimate the memory load of the app.

      总数是dalvik,native和其他上的总和。

      The totals are the sum over the dalvik, native, and other.

      这篇关于如何通过应用程序获取内存使用率和cpu使用率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆