1

I am looking into reading the android device's total ammount of physical RAM.

I understand that these informations are stored in /proc/meminfo.

how can i read it?

opc0de
  • 11,557
  • 14
  • 94
  • 187

3 Answers3

3

try this :

   public void getTotalMemory() {  
    {
    String str1 = "/proc/meminfo";
    String str2;        
    String[] arrayOfString;
    long initial_memory = 0;
    try {
    FileReader localFileReader = new FileReader(str1);
    BufferedReader localBufferedReader = new BufferedReader(    localFileReader, 8192);
    str2 = localBufferedReader.readLine();//meminfo
    arrayOfString = str2.split("\\s+");
    for (String num : arrayOfString) {
    Log.i(str2, num + "\t");
    }
    //total Memory
    initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;   
    localBufferedReader.close();
    } 
    catch (IOException e) 
    {       
    }
  }  

when you read this file you will get Total Memory in First Line like:

MemTotal:          94096 kB
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1
public long getTotalMemory() {
    String str1 = "/proc/meminfo";
    String str2="";
    String[] arrayOfString;
    long initial_memory = 0, free_memory = 0;
    try {
        FileReader localFileReader = new FileReader(str1);
        BufferedReader localBufferedReader = new BufferedReader(
            localFileReader, 8192);
        for (int i = 0; i < 2; i++) {
            str2 =str2+" "+ localBufferedReader.readLine();// meminfo  //THIS WILL READ meminfo AND GET BOTH TOT MEMORY AND FREE MEMORY eg-: Totalmemory 12345 KB //FREEMEMRY: 1234 KB  
        }
        arrayOfString = str2.split("\\s+");
        for (String num : arrayOfString) {
            Log.i(str2, num + "\t");
        }
        // total Memory
        initial_memory = Integer.valueOf(arrayOfString[2]).intValue();
        free_memory = Integer.valueOf(arrayOfString[5]).intValue();

        localBufferedReader.close();
    } catch (IOException e) {
    }
    return ((initial_memory-free_memory)/1024);
}
Jason Sturges
  • 15,855
  • 14
  • 59
  • 80
0

availMem should tell you the total available memory on the system.

You can use it like that:

ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
activityManager.getMemoryInfo(mi);
Log.d("FreeRam: ", "" + mi.availMem);

Hope it helps!

Chris
  • 4,403
  • 4
  • 42
  • 54