Android使用procrank和dumpsys meminfo分析内存占用情况

来源:互联网 发布:小礼服 知乎 编辑:程序博客网 时间:2024/05/17 09:36

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

先来看下procrank

 

view sourceprint?
01.sh-4.2# procrank
02.PID      Vss      Rss      Pss      Uss  cmdline
03.1078   59840K   59708K   42125K   39344K  com.csr.BTApp
04.2683   59124K   59040K   37960K   33032K  com.android.launcher
05.1042   51572K   51488K   35686K   33604K  android.process.acore
06.782   32808K   32748K   16775K   14716K  system_server
07.667   20560K   17560K   12739K    8940K  /system/bin/surfaceflinger
08.851   30124K   30036K   12085K    7996K  com.android.systemui
09.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 是针对某个进程开始有可疑内存泄露的情况,进行检测的最佳数字。怀疑某个程序有内存泄露可以查看这个值是否一直有增加

Overview
The aim of this post is to provide information that will assist in interpreting memory reports from various tools so the true memory usage for Linux processes and the system can be determined.
Android has a tool called procrank (/system/xbin/procrank), which lists out the memory usage of Linux processes in order from highest to lowest usage. The sizes reported per process are VSS, RSS, PSS, and USS.For the sake of simplicity in this description, memory will be expressed in terms of pages, rather than bytes. Linux systems like ours manage memory in 4096 byte pages at the lowest level.

VSS (reported as VSZ from ps)

is the total accessible address space of a process. This size also includes memory that may not be resident in RAM like mallocs that have been allocated but not written to. VSS is of very little use for determing real memory usage of a process.

RSS

is the total memory actually held in RAM for a process. RSS can be misleading, because it reports the total all of the shared libraries that the process uses, even though a shared library is only loaded into memory once regardless of how many processes use it. RSS is not an accurate representation of the memory usage for a single process.

PSS

differs from RSS in that it reports the proportional size of its shared libraries, i.e. if three processes all use a shared library that has 30 pages, that library will only contribute 10 pages to the PSS that is reported for each of the three processes. PSS is a very useful number because when the PSS for all processes in the system are summed together, that is a good representation for the total memory usage in the system. When a process is killed, the shared libraries that contributed to its PSS will be proportionally distributed to the PSS totals for the remaining processes still using that library. In this way PSS can be slightly misleading, because when a process is killed, PSS does not accurately represent the memory returned to the overall system.

USS 

is the total private memory for a process, i.e. that memory that is completely unique to that process. USS is an extremely useful number because it indicates the true incremental cost of running a particular process. When a process is killed, the USS is the total memory that is actually returned to the system. USS is the best number to watch when initially suspicious of memory leaks in a process.

For systems that have Python available, there is also a nice tool called smem that will report memory statistics including all of these categories.

使用dumpsys meminfo查看内存信息

 

view sourceprint?
01.sh-4.2# dumpsys meminfo
02.Applications Memory Usage (kB):
03.Uptime: 3353863 Realtime: 3353850
04. 
05.Total PSS by process:
06.41743 kB: com.csr.BTApp (pid 1078)
07.36924 kB: com.android.launcher (pid 2683)
08.35452 kB: android.process.acore (pid 1042)
09.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

 

 

view sourceprint?
01.sh-4.2# dumpsys meminfo com.android.launcher
02.Applications Memory Usage (kB):
03.Uptime: 4497753 Realtime: 4497741
04. 
05.** MEMINFO in pid 2683 [com.android.launcher] **
06.Shared  Private     Heap     Heap     Heap
07.Pss    Dirty    Dirty     Size    Alloc     Free
08.------   ------   ------   ------   ------   ------
09.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能做的事还有很多

 

 

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