how to get memory usage and cpu usage by application?

14,859

Solution 1

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 is the memory that would be freed by the java
    virtual machine if the process is killed

  • nativePrivateDirty is the same for native code is the same for some other code (not sure what else there is)
  • otherPrivateDirty dalvikSharedDirty is the shared memory used by the java virtual machine But this would not be freed

  • 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.

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

Solution 2

Read /proc/[pid]/stat - [pid] is your target process's pid

Then look for the following members.

  • utime %lu
  • stime %lu

man /proc @ http://linux.die.net/man/5/proc

Share:
14,859
Ravi Patel
Author by

Ravi Patel

Updated on June 17, 2022

Comments

  • Ravi Patel
    Ravi Patel almost 2 years

    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;
    }
    
  • Pavandroid
    Pavandroid about 12 years
    Check the image for more information at 2.bp.blogspot.com/_9l0GmPwgCzk/SY-EGKmIm4I/AAAAAAAAACE/…
  • Ravi Patel
    Ravi Patel about 12 years
    i want to let user know directly in the application which process is using percentage of cpu.
  • JPM
    JPM almost 12 years
    He wants to do it in code like other apps do not use the DDMS environment
  • user3233280
    user3233280 over 9 years
    Log.e("memory"," total pss: " + MI[0].getTotalPss()); will this line return memory in bytes or in KB ?