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;
}
share|improve this question

feedback

2 Answers

up vote -1 down vote accepted

While Running your application, Run the DDMS tool which is present in Android-sdk\tools folder. Where you will get the complete details about all the running process. If you have debug enabled Android Device, then you can debug even from the device by connecting it to the system.

share|improve this answer
Check the image for more information at 2.bp.blogspot.com/_9l0GmPwgCzk/SY-EGKmIm4I/AAAAAAAAACE/… – Pavandroid Mar 18 at 7:05
i want to let user know directly in the application which process is using percentage of cpu. – Ravi Patel Mar 19 at 5:27
He wants to do it in code like other apps do not use the DDMS environment – JPM Jul 16 at 22:47
feedback

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|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.