在 Android Settings中,“应用” - “正在运行”,能够看到内存相关的信息。以下图java
这三个内存指标都来自 /proc/meminfo 这支文件中。post
root@generic_x86:/proc # cat meminfo MemTotal: 1551556 kB MemFree: 1176792 kB Buffers: 4620 kB Cached: 157696 kB
总内存 = Memtotalthis
剩余内存 = MemFree + Cached - SECONDARY_SERVER_MEMspa
SECONDARY_SERVER_MEM 为 ActivityManger.Meminfo.secondaryServerThresholdcode
如上,Clock 的内存为 4.1 M。orm
这个内存值取自 Pss ,从 ActivityManagerService.getProcessesPss()中取得。进程
long[] pss = ActivityManagerNative.getDefault().getProcessPss(pids); int bgIndex = 0; for (int i=0; i<pids.length; i++) { ProcessItem proc = mAllProcessItems.get(i); changed |= proc.updateSize(context, pss[i], mSequence);
boolean updateSize(Context context, long pss, int curSeq) { mSize = pss * 1024; if (mCurSeq == curSeq) { String sizeStr = Formatter.formatShortFileSize( context, mSize); if (!sizeStr.equals(mSizeStr)){ mSizeStr = sizeStr; // We update this on the second tick where we update just // the text in the current items, so no need to say we // changed here. return false; } } return false; }
这个Pss 与 procrank 展现出来的 pss 是同样的。因此说他们应该取的同一个值。继续看看 ActivityManagerService 如何拿的吧。内存
public long[] getProcessPss(int[] pids) { 4294 enforceNotIsolatedCaller("getProcessPss"); 4295 long[] pss = new long[pids.length]; 4296 for (int i=pids.length-1; i>=0; i--) { 4297 ProcessRecord proc; 4298 int oomAdj; 4299 synchronized (this) { 4300 synchronized (mPidsSelfLocked) { 4301 proc = mPidsSelfLocked.get(pids[i]); 4302 oomAdj = proc != null ? proc.setAdj : 0; 4303 } 4304 } 4305 long[] tmpUss = new long[1]; 4306 pss[i] = Debug.getPss(pids[i], tmpUss); 4307 if (proc != null) { 4308 synchronized (this) { 4309 if (proc.thread != null && proc.setAdj == oomAdj) { 4310 // Record this for posterity if the process has been stable. 4311 proc.baseProcessTracker.addPss(pss[i], tmpUss[0], false, proc.pkgList); 4312 } 4313 } 4314 } 4315 } 4316 return pss; 4317 }
pss[i] = Debug.getPss(pids[i], tmpUss);get
Debug.getPss() 有两个实现,带参与不带参。带参的是获取进程列表的内存。不带参对应用可见,在应中,咱们就能够用 Debug.getPss() 来获取进程对标系统设置中的内存了。it
对了,这个单位是 kb,实际计算的时候要注意了。