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.
MemTotal — Total amount of physical RAM, in kilobytes.
MemFree — The amount of physical RAM, in kilobytes, left unused by the system.
Buffers — The amount of physical RAM, in kilobytes, used for file buffers.
Cached — The amount of physical RAM, in kilobytes, used as cache memory.
SwapCached — The amount of swap, in kilobytes, used as cache memory.
Active — The total amount of buffer or page cache memory, in kilobytes, that is in active use. This is memory that has been recently used and is usually not reclaimed for other purposes.
Inactive — The total amount of buffer or page cache memory, in kilobytes, that are free and available. This is memory that has not been recently used and can be reclaimed for other purposes.
HighTotal and HighFree — The total and free amount of memory, in kilobytes, that is not directly mapped into kernel space. The HighTotal value can vary based on the type of kernel used.
LowTotal and LowFree — The total and free amount of memory, in kilobytes, that is directly mapped into kernel space. The LowTotal value can vary based on the type of kernel used.
SwapTotal — The total amount of swap available, in kilobytes.
SwapFree — The total amount of swap free, in kilobytes.
Dirty — The total amount of memory, in kilobytes, waiting to be written back to the disk. Writeback — The total amount of memory, in kilobytes, actively being written back to the disk.
Mapped — The total amount of memory, in kilobytes, which have been used to map devices, files, or libraries using the mmap command.
Slab — The total amount of memory, in kilobytes, used by the kernel to cache data structures for its own use.
Committed_AS — The total amount of memory, in kilobytes, estimated to complete the workload. This value represents the worst case scenario value, and also includes swap memory. PageTables — The total amount of memory, in kilobytes, dedicated to the lowest page table level.
VMallocTotal — The total amount of memory, in kilobytes, of total allocated virtual address space.
VMallocUsed — The total amount of memory, in kilobytes, of used virtual address space.
VMallocChunk — The largest contiguous block of memory, in kilobytes, of available virtual address space.
HugePages_Total — The total number of hugepages for the system. The number is derived by dividing Hugepagesize by the megabytes set aside for hugepages specified in /proc/sys/vm/hugetlb_pool. This statistic only appears on the x86, Itanium, and AMD64 architectures.
HugePages_Free — The total number of hugepages available for the system. This statistic only appears on the x86, Itanium, and AMD64 architectures.
Hugepagesize — The size for each hugepages unit in kilobytes. By default, the value is 4096 KB on uniprocessor kernels for 32 bit architectures. For SMP, hugemem kernels, and AMD64, the default is 2048 KB. For Itanium architectures, the default is 262144 KB. This statistic only appears on the x86, Itanium, and AMD64 architectures.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With