Sahil Mahajan Mj
Sahil Mahajan Mj

Reputation: 11141

How to get CPU usage by Running Apps

I am working on an App, where I need to show the CPU usage as per running applications(just like a Task manager).

I have followed this post, Get CPU usage, but it demonstrates the total CPU usage. Here it has used the following code to read the CPU stats,

RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");

I want to get cpu usage of all individually running apps. So far, I've been able to fetch the list of all running applications,

      ActivityManager activityManager = (ActivityManager)
                      getSystemService(ACTIVITY_SERVICE);

    List<RunningAppProcessInfo> listOfRunningProcess = activityManager
                    .getRunningAppProcesses();

Now I want to get the CPU usage for these applications.

Upvotes: 2

Views: 7371

Answers (2)

Sunny
Sunny

Reputation: 14808

Use top command and Execute it by using Runtime.execute("your top command") This will give you a String and you have to parse it to get each app CPU usages.

look at here for more on Top command.

Upvotes: 1

Supyus
Supyus

Reputation: 24

Well you can use the linux top command to get the memory usage for each process (PID).

You can find the complete implementation here.

Upvotes: 0

Related Questions