Android Question Android CPU Usage How to get?

ronovar

Active Member
Licensed User
Longtime User
If is possible to get CPU Usage from android box? So that i can see how many cpu percent and how much ram memory is my apk using so that i know in witch way i need it to optimize it.

So idea is like this:

lblCPUsage.Text = Application.GetCPUsage
lblRAMUsage.Text = Application.GetRAMUsage

Thanks.
 

somed3v3loper

Well-Known Member
Licensed User
Longtime User
I got this from here http://stackoverflow.com/a/10269661

B4X:
       Dim java As JavaObject
    java.InitializeContext
    Dim stastics() As Int =java.RunMethod("getCpuUsageStatistic",Null)
    For Each v In stastics
        Log(v)
    Next
   

#If JAVA


//cpu
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import android.util.Log;

   
    /**
*
* @return integer Array with 4 elements: user, system, idle and other cpu
*         usage in percentage.
*/
public int[] getCpuUsageStatistic() {

    String tempString = executeTop();

    tempString = tempString.replaceAll(",", "");
    tempString = tempString.replaceAll("User", "");
    tempString = tempString.replaceAll("System", "");
    tempString = tempString.replaceAll("IOW", "");
    tempString = tempString.replaceAll("IRQ", "");
    tempString = tempString.replaceAll("%", "");
    for (int i = 0; i < 10; i++) {
        tempString = tempString.replaceAll("  ", " ");
    }
    tempString = tempString.trim();
    String[] myString = tempString.split(" ");
    int[] cpuUsageAsInt = new int[myString.length];
    for (int i = 0; i < myString.length; i++) {
        myString[i] = myString[i].trim();
        cpuUsageAsInt[i] = Integer.parseInt(myString[i]);
    }
    return cpuUsageAsInt;
}

private String executeTop() {
    java.lang.Process p = null;
    BufferedReader in = null;
    String returnString = null;
    try {
        p = Runtime.getRuntime().exec("top -n 1");
        in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        while (returnString == null || returnString.contentEquals("")) {
            returnString = in.readLine();
        }
    } catch (IOException e) {
        Log.e("executeTop", "error in getting first line of top");
        e.printStackTrace();
    } finally {
        try {
            in.close();
            p.destroy();
        } catch (IOException e) {
            Log.e("executeTop",
                    "error in closing and destroying top process");
            e.printStackTrace();
        }
    }
    return returnString;
}
#End if
I am not sure whether it is what you want
And it takes a time to retrieve info so you might want to run it on a thread .
 
Upvote 0

ronovar

Active Member
Licensed User
Longtime User
Thanks this is what i need...my apk gets cpu usage from 5 to 25% and middle usage is around 18%..cpu is dual core...so i think that is OK usage for speed....i im using SQL database, scrollers, 5 timers.

Thanks this code works excellent.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
This code, seems, halts the UI for 1-3 seconds.
What is the means of these 4 returned digits ?
Sum of all 4 digits are under 100% always ?
 
Upvote 0
Top