I am writing an Android app that needs to measure current free/used RAM.
Searching this site showed multiple threads with similar topics that generally suggest two approaches:
Approach 1:
MemoryInfo mi = new MemoryInfo();
ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
activityManager.getMemoryInfo(mi);
long availableMegs = mi.availMem / 1048576L;
totalMegs = mi.totalMem / 1048576L; //available since API 16 only
Approach 2: parsing /proc/meminfo.
I have tried both approaches and they seem to work ok. I get the same values from both methods.
My issue is that when I go to Settings -> Apps -> Running
at the bottom of the screen is information about free/used RAM and this differs from what I get with above mentioned methods.
My suspicion is that Google does report cached apps as free memory, because when I kill some cached processes, Google's reported RAM usage barely changes, but my apps does in an amount that is close to size of the running process I just killed.
Example:
Approach 1 output:
meminfo: avail: 660, total: 821, used: 161
Approach 2 output:
root@android:/proc # cat meminfo
MemTotal: 840868 kB
MemFree: 548080 kB
Buffers: 0 kB
Cached: 128300 kB
SwapCached: 0 kB
Active: 192052 kB
Inactive: 79816 kB
Active(anon): 157792 kB
Inactive(anon): 0 kB
Active(file): 34260 kB
Inactive(file): 79816 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 0 kB
SwapFree: 0 kB
Dirty: 0 kB
Writeback: 0 kB
AnonPages: 143580 kB
Mapped: 67584 kB
Slab: 6736 kB
SReclaimable: 2104 kB
SUnreclaim: 4632 kB
PageTables: 4008 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 420432 kB
Committed_AS: 1606616 kB
VmallocTotal: 122880 kB
VmallocUsed: 20588 kB
VmallocChunk: 77828 kB
MemFree + Cached = 660MB
Android reported RAM usage: 679MB Free, 142MB used.
How can I get the same numbers as Android reports?
I assume I have to get it from /proc/meminfo
as the official API seems not to exist.