如果你想查看所有进程的内存使用情况,可以使用命令procrank、dumpsys meminfo查看,当然也只可以过滤出某个进程如:dumpsys meminfo | grep -i phone

先来看下procrank



1 sh-4.2# procrank  
2 PID Vss Rss Pss Uss cmdline
3 1078 59840K 59708K 42125K 39344K com.csr.BTApp
4 2683 59124K 59040K 37960K 33032K com.android.launcher
5 1042 51572K 51488K 35686K 33604K android.process.acore
6 782 32808K 32748K 16775K 14716K system_server
7 667 20560K 17560K 12739K 8940K /system/bin/surfaceflinger
8 851 30124K 30036K 12085K 7996K com.android.systemui
9 2999 27680K 27596K 9929K 7040K com.baidu.input
10 959 20764K 20676K 5522K 3788K com.android.phone
11 3468 21892K 21800K 4591K 1920K com.apical.dreamthemetime
12 982 19880K 19792K 4438K 2644K com.csr.csrservices
13 668 19592K 19480K 3525K 1360K zygote
14 670 2960K 2960K 2407K 2356K /system/bin/mediaserver
15 663 1784K 1784K 1209K 1116K /system/bin/synergy_service
16 756 3404K 1348K 1133K 1124K /usr/bin/gpsexe
17 669 1468K 1468K 959K 928K /system/bin/drmserver
18 675 692K 692K 692K 692K /bin/sh
19 758 1060K 1060K 630K 604K /system/bin/audiotransfer
20 3482 656K 652K 456K 444K procrank
21 664 664K 664K 403K 392K /system/bin/netd
22 658 584K 584K 331K 320K /system/bin/vold
23 666 548K 548K 270K 256K /system/bin/rild
24 671 416K 412K 212K 204K /system/bin/dbus-daemon
25 673 336K 332K 170K 164K /system/bin/keystore
26 1 164K 164K 144K 144K /init
27 674 152K 152K 136K 136K /sbin/adbd
28 662 312K 312K 112K 104K /system/bin/dvdd
29 672 328K 324K 109K 100K /system/bin/installd
30 657 268K 264K 102K 96K /system/bin/servicemanager
31 649 84K 84K 84K 84K /sbin/ueventd
32 665 260K 256K 83K 76K /system/bin/debuggerd
33 ------ ------ ------
34 195031K 163724K TOTAL
35
36 RAM: 480380K total, 3624K free, 732K buffers, 299788K cached, 264844K shmem, 7632K slab


 

从以上打印可以看出,一般来说内存占用大小有如下规律:VSS >= RSS >= PSS >= USS

VSS - Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)是单个进程全部可访问的地址空间

RSS - Resident Set Size 实际使用物理内存(包含共享库占用的内存)是单个进程实际占用的内存大小,对于单个共享库, 尽管无论多少个进程使用,实际该共享库只会被装入内存一次。

PSS - Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存)

USS - Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存)USS 是一个非常非常有用的数字, 因为它揭示了运行一个特定进程的真实的内存增量大小。如果进程被终止, USS 就是实际被返还给系统的内存大小。

USS 是针对某个进程开始有可疑内存泄露的情况,进行检测的最佳数字。怀疑某个程序有内存泄露可以查看这个值是否一直有增加

 

使用dumpsys meminfo查看内存信息


 


 



1 sh-4.2# dumpsys meminfo  
2 Applications Memory Usage (kB):
3 Uptime: 3353863 Realtime: 3353850
4
5 Total PSS by process:
6 41743 kB: com.csr.BTApp (pid 1078)
7 36924 kB: com.android.launcher (pid 2683)
8 35452 kB: android.process.acore (pid 1042)
9 16094 kB: system (pid 782)
10 11609 kB: com.android.systemui (pid 851)
11 8564 kB: com.baidu.input (pid 2999)
12 5298 kB: com.android.phone (pid 959)
13 4443 kB: com.apical.dreamthemetime (pid 4448)
14 4203 kB: com.csr.csrservices (pid 982)
15 4130 kB: com.apical.apicalradio (pid 4518)
16
17 Total PSS by OOM adjustment:
18 16094 kB: System
19 16094 kB: system (pid 782)
20 21110 kB: Persistent
21 11609 kB: com.android.systemui (pid 851)
22 5298 kB: com.android.phone (pid 959)
23 4203 kB: com.csr.csrservices (pid 982)
24 36924 kB: Foreground
25 36924 kB: com.android.launcher (pid 2683)
26 85759 kB: Perceptible
27 41743 kB: com.csr.BTApp (pid 1078)
28 35452 kB: android.process.acore (pid 1042)
29 8564 kB: com.baidu.input (pid 2999)
30 4443 kB: A Services
31 4443 kB: com.apical.dreamthemetime (pid 4448)
32 4130 kB: Background
33 4130 kB: com.apical.apicalradio (pid 4518)
34
35 Total PSS by category:
36 56020 kB: Dalvik
37 30214 kB: Other dev
38 27716 kB: Native
39 24504 kB: Cursor
40 13198 kB: Unknown
41 7723 kB: Other mmap
42 6895 kB: .so mmap
43 1232 kB: .apk mmap
44 888 kB: .dex mmap
45 36 kB: .ttf mmap
46 34 kB: Ashmem
47 0 kB: .jar mmap
48
49 Total PSS: 168460 kB


 

打印某个程序内存信息,把包名写上,如:com.android.launcher


 


 



1 sh-4.2# dumpsys meminfo com.android.launcher  
2 Applications Memory Usage (kB):
3 Uptime: 4497753 Realtime: 4497741
4
5 ** MEMINFO in pid 2683 [com.android.launcher] **
6 Shared Private Heap Heap Heap
7 Pss Dirty Dirty Size Alloc Free
8 ------ ------ ------ ------ ------ ------
9 Native 2144 988 2040 8636 5124 1699
10 Dalvik 9481 8292 8644 13639 13335 304
11 Cursor 0 0 0
12 Ashmem 2 4 0
13 Other dev 4 20 0
14 .so mmap 922 1892 292
15 .jar mmap 0 0 0
16 .apk mmap 90 0 0
17 .ttf mmap 0 0 0
18 .dex mmap 300 0 0
19 Other mmap 1634 16 112
20 Unknown 1830 580 1772
21 TOTAL 16407 11792 12860 22275 18459 2003
22
23 Objects
24 Views: 146 ViewRootImpl: 1
25 AppContexts: 374 Activities: 1
26 Assets: 4 AssetManagers: 4
27 Local Binders: 13 Proxy Binders: 23
28 Death Recipients: 1
29 OpenSSL Sockets: 0
30
31 SQL
32 heap: 59 MEMORY_USED: 59
33 PAGECACHE_OVERFLOW: 1 MALLOC_SIZE: 46
34
35 DATABASES
36 pgsz dbsz Lookaside(b) cache Dbname
37 1 179 55 2/11/2 launcher.db
38
39 Asset Allocations
40 zip:/system/app/Launcher2.apk:/resources.arsc: 340K
41 zip:/system/app/MediaCenter.apk:/resources.arsc: 604K


 

Android程序内存被分为2部分:native和dalvik,dalvik就是我们平常说的java堆,我们创建的对象是在这里面分配的,而bitmap是直接在native上分配的,对于内存的限制是 native+dalvik 不能超过最大限制。Android程序内存一般限制在16M,当然也有24M的。

从上信息对于分析内存泄露,内存溢出都有极大的作用,从以上信息可以看到该应用程序占用的native和dalvik,当TOTAL 16407 11792 12860 22275 18459 2003超过内存最大限制时会出现OOM错误。

dumpsys能做的事还有很多

 



1 dumpsys [options]  
2 meminfo 显示内存信息
3 cpuinfo 显示CPU信息
4 account 显示accounts信息
5 activity 显示所有的activities的信息
6 window 显示键盘,窗口和它们的关系
7 wifi 显示wifi信息


 

 

adb shell top

top命令提供了实时的对系统处理器的状态监视.它将显示系统中CPU最“敏感”的任务列表.该命令可以按CPU使用.内存使用和执行时间对任务进行排序.

top 用法

>adb shell top -h
Usage: top [ -m max_procs ] [ -n iterations ] [ -d delay ] [ -s sort_column ] [-t ] [ -h ]
-m num Maximum number of processes to display. 最多显示多少个进程
-n num Updates to show before exiting. 刷新次数
-d num Seconds to wait between updates. 刷新间隔时间(默认5秒)
-s col Column to sort by (cpu,vss,rss,thr). 按哪列排序
-t Show threads instead of processes. 显示线程信息而不是进程
-h Display this help screen. 显示帮助文档


adb shell top

> adb shell top

User 13%, System 5%, IOW 0%, IRQ 0%
User 85 + Nice 0 + Sys 37 + Idle 509 + IOW 0 + IRQ 0 + SIRQ 0 = 631

PID PR CPU% S #THR VSS RSS PCY UID Name
22205 0 13% S 56 423416K 88160K fg u0_a92 com.tmall.wireless
24310 1 2% R 1 1232K 536K root top
22600 0 1% S 46 341712K 40872K fg u0_a90 com.wandoujia.phoenix2.usbproxy
31125 1 1% S 31 319976K 33284K fg u0_a74 com.android.Chinpower
1533 0 1% S 32 67320K 20552K fg system /system/bin/surfaceflinger
1852 0 1% S 112 445876K 80304K fg system system_server
...
10 0 0% S 1 0K 0K root watchdog/0
16 1 0% S 1 0K 0K root khelper
22 1 0% S 1 0K 0K root suspend_sys_syn
23 1 0% S 1 0K 0K root suspend


第一组数据的含义:

User  处于用户态的运行时间,不包含优先值为负进程 
Nice 优先值为负的进程所占用的CPU时间
Sys 处于核心态的运行时间
Idle 除IO等待时间以外的其它等待时间
IOW IO等待时间
IRQ 硬中断时间
SIRQ 软中断时间


第二组数据的含义:

PID   进程id
PR 优先级
CPU% 当前瞬时CPU占用率
S 进程状态:D=不可中断的睡眠状态, R=运行, S=睡眠, T=跟踪/停止, Z=僵尸进程
#THR 程序当前所用的线程数
VSS Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)
RSS Resident Set Size 实际使用物理内存(包含共享库占用的内存)
PCY 调度策略优先级,SP_BACKGROUND/SP_FOREGROUND
UID 进程所有者的用户id
Name 进程的名称


具体信息可以查看源代码中: xx\system\core\toolbox\top.c

监控CPU的命令使用实战

>adb shell top  | findstr tmall > d:\cpu.txt   


打印 top命令,并查找 包含 tmall的命令行,输出至 d:\cpu.txt

result:

22137  1   0% S    10 304728K  28900K  bg u0_a92   com.tmall.wireless:push

22205 0 0% S 15 322944K 38236K bg u0_a92 com.tmall.wireless

22205 1 27% R 34 345304K 49396K fg u0_a92 com.tmall.wireless

22112 1 0% S 9 303704K 29380K bg u0_a92 com.tmall.wireless:containerprocess1

22137 1 0% S 10 304728K 28900K bg u0_a92 com.tmall.wireless:push

22252 1 0% S 9 302600K 28596K fg u0_a92 com.tmall.wireless:notify

22205 0 45% R 40 365500K 56708K fg u0_a92 com.tmall.wireless

22252 0 22% S 19 325476K 40868K bg u0_a92 com.tmall.wireless:notify

22137 0 2% S 14 308944K 31320K fg u0_a92 com.tmall.wireless:push

22112 0 0% S 9 305892K 29888K bg u0_a92 com.tmall.wireless:containerprocess1

22137 1 14% S 17 323080K 39696K bg u0_a92 com.tmall.wireless:push

22252 0 3% S 21 327656K 41556K bg u0_a92 com.tmall.wireless:notify

22205 1 2% S 38 366420K 59700K fg u0_a92 com.tmall.wireless

22112 0 0% S 9 305892K 29888K bg u0_a92 com.tmall.wireless:containerprocess1

22408 0 0% R 9 278424K 21936K fg u0_a92 com.tmall.wireless:utremote

22408 1 7% S 9 304180K 30964K bg u0_a92 com.tmall.wireless:utremote

22205 0 2% R 39 367456K 59612K fg u0_a92 com.tmall.wireless