Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Memory Usage in Android

Is there any API by which we can get CPU or Memory usage of android?

I have tried one code as below:

package com.infostretch.mainactivity;  import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader;  public class CPULoad  {     long total = 0;     long idle = 0;      float usage = 0;      public CPULoad()     {         readUsage();     }      public float getUsage()     {         readUsage();         return usage;     }      private void readUsage()     {         try         {             BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/stat")), 1000);             String load = reader.readLine();             reader.close();              String[] toks = load.split(" ");              long currTotal = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4]);             long currIdle = Long.parseLong(toks[5]);              this.usage = (currTotal - total) * 100.0f / (currTotal - total + currIdle - idle);             this.total = currTotal;             this.idle = currIdle;         }         catch(IOException ex)         {             ex.printStackTrace();         }     } } 

Is this the correct way to do it?

like image 768
Badal Avatar asked Jun 25 '10 13:06

Badal


People also ask

How do I check memory usage on Android?

Scroll down to “System.” Now select “Developer Options.” Tap “Memory” to see the RAM usage stats. You'll see the “Average Memory Use” at the top of the screen.

How do I check my RAM memory usage?

Press Ctrl + Shift + Esc to launch Task Manager. Or, right-click the Taskbar and select Task Manager. Select the Performance tab and click Memory in the left panel. The Memory window lets you see your current RAM usage, check RAM speed, and view other memory hardware specifications.

How do I check the memory capacity of my phone?

How do I check how much memory my phone has and how much memory is being used? Depending on your phone model, tap System > Memory or tap Memory in Settings to see how much memory is available and how your phone's memory is being used. You can also find out how individual apps are using memory.


1 Answers

I use this function to calculate cpu usage. Hope it can help you.

private float readUsage() {     try {         RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");         String load = reader.readLine();          String[] toks = load.split(" +");  // Split on one or more spaces          long idle1 = Long.parseLong(toks[4]);         long cpu1 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[5])               + 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[4]);         long cpu2 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[5])             + 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; }  
like image 172
szcoder Avatar answered Sep 22 '22 11:09

szcoder