Skip to content

How Android computes available memory?

mingyuan-xia edited this page Dec 18, 2014 · 1 revision
  • If you look at stackoverflow thread about how to get current available RAM in system, they will probably refer you to use MemoryInfo.availMem. However, this is not the number shown in the setting.
  • We want to know how the available RAM shown in Setting->Apps->RUNNING is computed.
  • This is the source code for the setting->app UI. You find this line interesting:
long availMem = mMemInfoReader.getFreeSize() + mMemInfoReader.getCachedSize()
                - SECONDARY_SERVER_MEM;

mMemInfoReader reads /proc/meminfo and retrieve the "MemFree:" and "Cached:". The SECONDARY_SERVER_MEM is a constant to the system, which I did not dig into.

  • Further more, this "availMem" is not the final answer. Scroll down a little more, you will see:
           float totalMem = mMemInfoReader.getTotalSize();
            float totalShownMem = availMem + mLastBackgroundProcessMemory
                    + mLastServiceProcessMemory;
            mColorBar.setRatios((totalMem-totalShownMem)/totalMem,
                    mLastServiceProcessMemory/totalMem,
                    mLastBackgroundProcessMemory/totalMem);

So basically, the real free RAM shown is:

.getFreeSize()+.getCachedSize()- SECONDARY_SERVER_MEM
+ mLastBackgroundProcessMemory+ mLastServiceProcessMemory