17

I can get total available memory by:

ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
memoryInfo.availMem;

However, how do you get Total memory (RAM) of the device?

I did read: How do I discover memory usage of my application in Android?

It doesn't seem like adding pidMemoryInfo.getTotalPss() gives me the total memory either.

Community
  • 1
  • 1
jclova
  • 5,466
  • 16
  • 52
  • 78

3 Answers3

15

Try this works for me.

public static String getTotalRAM() {
    RandomAccessFile reader = null;
    String load = null;
    try {
        reader = new RandomAccessFile("/proc/meminfo", "r");
        load = reader.readLine();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        // Streams.close(reader);
    }
    return load;
}
sandy
  • 3,311
  • 4
  • 36
  • 47
3

MemoryInfo class has added totalMem in API 16. Please give a try.

public long totalMem Added in API level 16 The total memory accessible by the kernel. This is basically the RAM size of the device, not including below-kernel fixed allocations like DMA buffers, RAM for the baseband CPU, etc.

user802467
  • 951
  • 2
  • 14
  • 22
3

If you don't find something else, you could bypass android and read /proc/meminfo which is what the 'free' command on a more ordinary linux distribution does

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117