常用的查看app运行内存消耗的有adb shell procrank
lgy@lgy-PC:~$ adb shell procrank|grep dialer
19164 1646272K 68160K 8043K 6080K 9304K 284K com.android.dialer
和adb shell dumpsys meminfo
lgy@lgy-PC:~$ adb shell dumpsys meminfo com.android.dialer
Applications Memory Usage (kB):
Uptime: 15528616 Realtime: 19602028
** MEMINFO in pid 19164 [com.android.dialer] **
Pss Private Private Swapped Heap Heap Heap
Total Dirty Clean Dirty Size Alloc Free
------ ------ ------ ------ ------ ------ ------
Native Heap 2062 1384 0 3796 28928 27248 1679
Dalvik Heap 885 848 0 20 17848 15944 1904
Dalvik Other 1012 716 0 4572
Stack 140 140 0 0
Other dev 4 0 4 0
.so mmap 286 68 0 796
.apk mmap 645 0 620 0
.dex mmap 2012 4 2008 4
.oat mmap 316 0 24 0
.art mmap 1032 440 24 76
Other mmap 11 8 0 0
Unknown 94 92 0 40
TOTAL 8499 3700 2680 9304 46776 43192 3583
App Summary
Pss(KB)
------
Java Heap: 1312
Native Heap: 1384
Code: 2724
Stack: 140
Graphics: 0
Private Other: 820
System: 2119
TOTAL: 8499 TOTAL SWAP (KB): 9304
Objects
Views: 0 ViewRootImpl: 0
AppContexts: 1 Activities: 0
Assets: 4 AssetManagers: 2
Local Binders: 3 Proxy Binders: 7
Parcel memory: 3 Parcel count: 14
Death Recipients: 0 OpenSSL Sockets: 0
SQL
MEMORY_USED: 101
PAGECACHE_OVERFLOW: 19 MALLOC_SIZE: 62
DATABASES
pgsz dbsz Lookaside(b) cache Dbname
4 28 22 2/22/3 /data/user/0/com.android.dialer/databases/ararat
都可以显示pss大小,但是会发现这两个显示的大小不一样,meminfo是8499,procrank显示的是8043。这两个为啥会不一样呢,哪个比较准确?
dumsys程序不做分析,可以理解为它会调用xx服务的dump方法打印相关信息。meminfo服务在ActivityManagerService中添加
frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
public void setSystemProcess() {
...
ServiceManager.addService("meminfo", new MemBinder(this));
...
}
服务代码实现是MemBinder,一个内部类
static class MemBinder extends Binder {
ActivityManagerService mActivityManagerService;
MemBinder(ActivityManagerService activityManagerService) {
mActivityManagerService = activityManagerService;
}
@Override
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
!= PackageManager.PERMISSION_GRANTED) {
pw.println("Permission Denial: can't dump meminfo from from pid="
+ Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
+ " without permission " + android.Manifest.permission.DUMP);
return;
}
mActivityManagerService.dumpApplicationMemoryUsage(fd, pw, " ", args, false, null);
}
}
dump就是实现打印信息的方法,先检测权限,然后打印相关信息
final void dumpApplicationMemoryUsage(FileDescriptor fd,
PrintWriter pw, String prefix, String[] args, boolean brief, PrintWriter categoryPw) {
...
ArrayList procs = collectProcesses(pw, opti, packages, args);
...
load_maps(pid, stats);
for (int i = procs.size() - 1 ; i >= 0 ; i--) {
...
if (mi == null) {
mi = new Debug.MemoryInfo();
}
if (dumpDetails || (!brief && !oomOnly)) {
Debug.getMemoryInfo(pid, mi);
} else {
mi.dalvikPss = (int)Debug.getPss(pid, tmpLong, null);
mi.dalvikPrivateDirty = (int)tmpLong[0];
}
...
}
...
}
主体是个循环,打印每个进程的信息,信息类是MemoryInfo,这个类定义在Debug中,而且获取信息内容的方法同样是在Debug中
frameworks/base/core/java/android/os/Debug.java
public static native void getMemoryInfo(int pid, MemoryInfo memoryInfo);
从native中看到这个类只是个跳转,真正的实现在C++代码中
frameworks/base/core/jni/android_os_Debug.cpp
{ "getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)V",
(void*) android_os_Debug_getDirtyPagesPid },
从jni相关代码中看出对应方法是android_os_Debug_getDirtyPagesPid
static void android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz,
jint pid, jobject object)
{
stats_t stats[_NUM_HEAP];
memset(&stats, 0, sizeof(stats));
load_maps(pid, stats);
...
}
stats是用于保存返回结果的,获取的代码就在load_maps中
static void load_maps(int pid, stats_t* stats)
{
char tmp[128];
FILE *fp;
sprintf(tmp, "/proc/%d/smaps", pid);
fp = fopen(tmp, "r");
if (fp == 0) return;
read_mapinfo(fp, stats);
fclose(fp);
}
和名字一样,就是读取proc/(进程pid)/smaps文件,该文件片段如下
12c00000-12e0a000 rw-p 00000000 00:04 470401 /dev/ashmem/dalvik-main space (deleted)
Size: 2088 kB
Rss: 1864 kB
Pss: 1864 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 1864 kB
Referenced: 1864 kB
Anonymous: 1864 kB
AnonHugePages: 0 kB
Swap: 0 kB
PSwap: 0 kB
KernelPageSize: 4 kB
MMUPageSize: 4 kB
Locked: 0 kB
VmFlags: rd wr mr mw me ac
12e0a000-133fe000 rw-p 0020a000 00:04 470401 /dev/ashmem/dalvik-main space (deleted)
Size: 6096 kB
Rss: 5900 kB
Pss: 5900 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 5900 kB
Referenced: 5900 kB
Anonymous: 5900 kB
AnonHugePages: 0 kB
Swap: 0 kB
PSwap: 0 kB
KernelPageSize: 4 kB
MMUPageSize: 4 kB
Locked: 0 kB
VmFlags: rd wr mr mw me ac
133fe000-22c00000 ---p 007fe000 00:04 470401 /dev/ashmem/dalvik-main space (deleted)
Size: 253960 kB
Rss: 0 kB
Pss: 0 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 0 kB
Referenced: 0 kB
Anonymous: 0 kB
AnonHugePages: 0 kB
Swap: 0 kB
PSwap: 0 kB
KernelPageSize: 4 kB
MMUPageSize: 4 kB
Locked: 0 kB
VmFlags: mr mw me ac
它详细的描述了内存中各个部分的信息。read_mapinfo方法就是逐行扫描这个文件,然后统计出信息。
/system/extras/procrank/procrank.c
int main(int argc, char *argv[]) {
...
for (i = 0; i < num_procs; i++) {
...
error = pm_process_create(ker, pids[i], &proc);
...
}
...
}
代码的主体还是个循环,通过pm_process_create方法获取相关信息,该方法实现在pm_process.c中
system/extras/libpagemap/pm_process.c
int pm_process_create(pm_kernel_t *ker, pid_t pid, pm_process_t **proc_out) {
...
error = read_maps(proc);
...
}
static int read_maps(pm_process_t *proc) {
...
error = snprintf(filename, MAX_FILENAME, "/proc/%d/maps", proc->pid);
...
}
可以看出最后和meminfo类似,从maps中获取信息,maps的片段如下
12c00000-12e0a000 rw-p 00000000 00:04 470401 /dev/ashmem/dalvik-main space (deleted)
12e0a000-135fe000 rw-p 0020a000 00:04 470401 /dev/ashmem/dalvik-main space (deleted)
135fe000-22c00000 ---p 009fe000 00:04 470401 /dev/ashmem/dalvik-main space (deleted)
23807000-23827000 rw-p 00000000 00:00 0
23827000-23a07000 ---p 00000000 00:00 0
32c00000-32c01000 rw-p 00000000 00:04 470402 /dev/ashmem/dalvik-main space 1 (deleted)
32c01000-42c00000 ---p 00001000 00:04 470402 /dev/ashmem/dalvik-main space 1 (deleted)
43fd4000-44000000 ---p 00000000 00:00 0
44000000-44020000 rw-p 00000000 00:00 0
44020000-44034000 ---p 00000000 00:00 0
45500000-45600000 rw-p 00000000 00:00 0
4d900000-4da00000 rw-p 00000000 00:00 0
52000000-52200000 rw-p 00000000 00:00 0
52200000-53000000 ---p 00000000 00:00 0
53000000-53200000 rw-p 00000000 00:00 0
53200000-54000000 ---p 00000000 00:00 0
56e00000-56f00000 rw-p 00000000 00:00 0
5ba00000-5baca000 rw-p 00000000 00:00 0
708e6000-717ba000 rw-p 00000000 fd:00 1368072 /data/dalvik-cache/arm64/system@[email protected]
717ba000-73d4f000 r--p 00000000 fd:00 1368070 /data/dalvik-cache/arm64/system@[email protected]
73d4f000-765f3000 r-xp 02595000 fd:00 1368070 /data/dalvik-cache/arm64/system@[email protected]
765f3000-765f4000 rw-p 04e39000 fd:00 1368070 /data/dalvik-cache/arm64/system@[email protected]
765f4000-7675d000 rw-p 00000000 00:04 470400 /dev/ashmem/dalvik-zygote space (deleted)
7675d000-7675e000 rw-p 00000000 00:04 468941 /dev/ashmem/dalvik-non moving space (deleted)
可以看出信息内容比smaps简略很多