0

I am working on an app, in which I need to display the total and available memory. I am using the following snippet; however, it works only on API 16 or above. I need something which works on all Android versions.

my question is Different from the "possible duplicates" as my question is to get memory usage on all api levels.

private void getAvailableRam() {
    MemoryInfo mi = new MemoryInfo();
    ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    activityManager.getMemoryInfo(mi);
    long availableMegs = mi.availMem / 1048576L;
    ttvv.setText(availableMegs + " MB");
}

private void getTotalRamSize() {
    MemoryInfo mi = new MemoryInfo();
    ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    activityManager.getMemoryInfo(mi);
    long totalMemory = mi.totalMem / 1048576L;
    ttvv2.setText(totalMemory + " MB");
}
  • Possible duplicate of [Is there a way to get total device ram?I need it for an optimization](http://stackoverflow.com/questions/12551547/is-there-a-way-to-get-total-device-rami-need-it-for-an-optimization) – PearsonArtPhoto Jan 12 '16 at 03:35

2 Answers2

1

File "/proc/meminfo" is real-time memory info. you can read it in your code

adb shell cat /proc/meminfo

MemTotal:        1900556 kB
MemFree:          343468 kB
Buffers:           97708 kB
Cached:           659364 kB
SwapCached:            0 kB
Active:           784552 kB
Inactive:         358336 kB
Active(anon):     394596 kB
Inactive(anon):      364 kB
Active(file):     389956 kB
Inactive(file):   357972 kB
Unevictable:        1396 kB
Mlocked:               0 kB
HighTotal:       1158144 kB
HighFree:          52088 kB
LowTotal:         742412 kB
LowFree:          291380 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Dirty:                 0 kB
Writeback:             0 kB
AnonPages:        394552 kB
Mapped:           176752 kB
Shmem:               452 kB
Slab:              48296 kB
SReclaimable:      21808 kB
SUnreclaim:        26488 kB
KernelStack:        9632 kB
PageTables:        19276 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:      950276 kB
Committed_AS:   47222076 kB
VmallocTotal:     245760 kB
VmallocUsed:       79036 kB
VmallocChunk:      75648 kB
PearsonArtPhoto
  • 35,989
  • 16
  • 107
  • 136
user3680200
  • 987
  • 8
  • 8
1

For getting available memory,

You can use the same mi.availMem since min API level is 1

For getting total memory less than API level 16

Try this

public class MainActivity extends AppCompatActivity {

TextView ttvv2;

protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      ttvv2 = (TextView) findViewById(R.id.ttvv2);
      ttvv2.setText(getTotalRAM());
 }

private  String getTotalRAM() {
      RandomAccessFile reader = null;
      String load = null;
      long total = 0;
      try {
           reader = new RandomAccessFile("/proc/meminfo", "r");
           load = reader.readLine().replaceAll("\\D+","");;
           total = Integer.parseInt(load)/1024;
      }catch (IOException ex) {
           ex.printStackTrace();
      }
      return total+" MB";
}
}

Hope this helps. Regards.

Source:

1) http://developer.android.com/reference/android/app/ActivityManager.MemoryInfo.html

2) Android How do you get total memory RAM in the device?

Community
  • 1
  • 1
sayor2015
  • 46
  • 5
  • pass the method to textview like this **ttvv2.setText(getTotalRAM());** – sayor2015 Jan 12 '16 at 04:15
  • You can either assign TotalRAM() to a string variable and use it in setText() or directly call the method in setText(). I also updated the code to fetch total RAM in MB. Hope this helps. Regards. – sayor2015 Jan 12 '16 at 04:30
  • I updated the code for full implementation. Hope this helps. Regards. – sayor2015 Jan 12 '16 at 04:41
  • use your old getAvailableRam() method Regards. – sayor2015 Jan 12 '16 at 05:05
  • i want to implement this simple antivirus in my app, can you test it and find out why it force closes? ill ask a new question! https://github.com/azzzey5/androidFileScanner – MaggotSauceYumYum Jan 12 '16 at 06:51