Android procrank , showmap 内存分析
(一)DDMS 的Heap Dump
1) Data Object:java object.
2) Class Object:object of type Class, e.g. what you'd get from java.lang.String.class or myObject.getClass( ).
3) 1,2,4,8-byte array:Number of bytes per entry.
1-byte array: byte, boolean
2-byte array: char, short
4-byte array: float, int
8-byte array: double, long
4) non-Java object:A non-Java Object is a piece of memory that isn't actually accessible from code written in Java. Essentially it's a blob of stuff that got stuck on the virtual heap but has no meaning to interpreted code. Shouldn't be much of that.----------------------------------------------------
需要root权限同时要支持以下命令的Android工程机
1:root@android:/ # procrank 查看所有进程的内存信息包括:PID Vss Rss Pss Uss cmdline
root@android:/ # procrank warning: could not create process interface for 32099 PID Vss Rss Pss Uss cmdline 17690 75152K 75020K 40491K 38024K com.dianxinos.optimizer.channel 13968 124792K 61864K 35726K 34372K com.dianxinos.optimizer.channel 1516 65052K 64940K 29661K 27076K android.process.acore 1322 88028K 65764K 29581K 26156K system_server 1417 68320K 63892K 28635K 25820K com.android.systemui 1757 55040K 54960K 21176K 19084K com.qihoo360.mobilesafe_mtk6573 12294 45208K 45096K 13238K 11220K android.process.acore 29904 44984K 44896K 12227K 10316K com.dianxinos.optimizer.engine.demo:service 29819 61032K 44624K 11772K 9808K com.dianxinos.optimizer.engine.demo 13284 40556K 40448K 9823K 8472K com.qihoo.appstore 1499 40036K 39940K 9095K 7700K com.android.phone 1478 39988K 39880K 8998K 7588K com.cootek.smartinputv5 12497 36860K 36736K 6975K 5844K android.process.media 12910 36420K 36336K 6754K 5640K com.cnnet.fknjjzw 25083 36772K 36640K 6413K 5180K com.google.process.gapps 26667 36660K 36512K 6323K 5008K com.qihoo.browser 1297 40400K 40240K 5366K 2804K zygote 30870 34424K 34284K 4495K 3264K com.android.contacts 31078 34304K 34164K 4338K 3188K com.android.providers.calendar 11595 33692K 33552K 4064K 2796K net.qihoo.launcher.widget.clockweather:WeatherService 30623 33720K 33576K 4005K 2748K com.android.settings 31103 33980K 33836K 3973K 2768K com.android.calendar
2:root@android:/ # showmap 29904 查看指定Pid的内存具体信息
root@android:/ # showmap 29904 virtual shared shared private private size RSS PSS clean dirty clean dirty # object -------- -------- -------- -------- -------- -------- -------- ---- ------------------------------ 124 124 17 124 0 0 0 3 /data/app/com.dianxinos.optimizer.engine.demo-2.apk 1400 708 351 676 0 32 0 1 /data/dalvik-cache/data@app@com.dianxinos.optimizer.engine.demo-2.apk@classes.dex 328 148 15 148 0 0 0 1 /data/dalvik-cache/system@framework@android.policy.jar@classes.dex 1348 32 1 32 0 0 0 1 /data/dalvik-cache/system@framework@apache-xml.jar@classes.dex 956 344 54 344 0 0 0 1 /data/dalvik-cache/system@framework@bouncycastle.jar@classes.dex 4 4 0 4 0 0 0 1 /data/dalvik-cache/system@framework@com.qrd.plugin.feature_query.jar@classes.dex 8 8 0 8 0 0 0 1 /data/dalvik-cache/system@framework@com.qrdinside.impl.jar@classes.dex 24 8 0 8 0 0 0 1 /data/dalvik-cache/system@framework@core-junit.jar@classes.dex 3288 1372 68 1372 0 0 0 1 /data/dalvik-cache/system@framework@core.jar@classes.dex 1460 96 5 96 0 0 0 1 /data/dalvik-cache/system@framework@ext.jar@classes.dex 316 16 0 16 0 0 0 1 /data/dalvik-cache/system@framework@filterfw.jar@classes.dex 11476 4616 316 4588 20 8 0 11 /data/dalvik-cache/system@framework@framework.jar@classes.dex 2208 48 2 48 0 0 0 1 /data/dalvik-cache/system@framework@services.jar@classes.dex 64 40 0 0 40 0 0 1 /dev/__properties__ (deleted) 24 4 2 0 4 0 0 1 /dev/ashmem/InputChannel 41a89fb0 com.dianxinos.optimizer.engine.demo/com.dianxinos.optimizer.engine.demo.antispam.AntiSpamActi 4 4 0 4 0 0 0 1 /dev/ashmem/SurfaceFlinger read-only heap (deleted) 8192 2864 249 0 2712 0 152 4 /dev/ashmem/dalvik-LinearAlloc (deleted) 2048 0 0 0 0 0 0 1 /dev/ashmem/dalvik-bitmap-1 (deleted) 2048 220 220 0 0 0 220 1 /dev/ashmem/dalvik-bitmap-2 (deleted) 1028 128 128 0 0 0 128 1 /dev/ashmem/dalvik-card-table (deleted) 131072 16136 7117 0 9356 0 6780 3 /dev/ashmem/dalvik-heap (deleted) 1024 96 96 0 0 0 96 1 /dev/ashmem/dalvik-jit-code-cache (deleted) 43692 0 0 0 0 0 0 1 /dev/ashmem/dalvik-mark-stack (deleted) 1016 4 4 0 0 4 0 1 /dev/binder 12 8 0 4 4 0 0 2 /system/bin/app_process 36 24 4 20 0 0 4 2 /system/bin/linker 360 24 3 24 0 0 0 1 /system/fonts/DroidNaskh-Regular.ttf 16 12 1 12 0 0 0 1 /system/fonts/DroidSansArmenian.ttf 224 16 2 16 0 0 0 1 /system/fonts/DroidSansEthiopic-Regular.ttf 5176 764 147 748 0 16 0 1 /system/fonts/DroidSansFallback.ttf 24 12 1 12 0 0 0 1 /system/fonts/DroidSansGeorgian.ttf 32 16 4 16 0 0 0 1 /system/fonts/DroidSansHebrew-Bold.ttf 32 16 2 16 0 0 0 1 /system/fonts/DroidSansHebrew-Regular.ttf 36 16 2 16 0 0 0 1 /system/fonts/DroidSansThai.ttf 140 8 1 8 0 0 0 1 /system/fonts/Lohit-Bengali.ttf 64 16 2 16 0 0 0 1 /system/fonts/Lohit-Devanagari.ttf 68 8 1 8 0 0 0 1 /system/fonts/Lohit-Tamil.ttf 80 12 3 12 0 0 0 1 /system/fonts/Roboto-Bold.ttf 80 16 2 16 0 0 0 1 /system/fonts/Roboto-Regular.ttf 4 0 0 0 0 0 0 1 /system/framework/android.policy.jar 8 0 0 0 0 0 0 1 /system/framework/apache-xml.jar 4 0 0 0 0 0 0 1 /system/framework/bouncycastle.jar 4 0 0 0 0 0 0 1 /system/framework/com.qrd.plugin.feature_query.jar 4 0 0 0 0 0 0 1 /system/framework/com.qrdinside.impl.jar 4 0 0 0 0 0 0 1 /system/framework/core-junit.jar 4 0 0 0 0 0 0 1 /system/framework/core.jar 64 0 0 0 0 0 0 1 /system/framework/ext.jar 4 0 0 0 0 0 0 1 /system/framework/filterfw.jar 944 212 25 212 0 0 0 2 /system/framework/framework-res.apk 4 0 0 0 0 0 0 1 /system/framework/framework.jar 4 0 0 0 0 0 0 1 /system/framework/services.jar 20 20 9 12 0 0 8 2 /system/lib/hw/gralloc.msm7627a.so 152 80 5 52 24 0 4 3 /system/lib/libEGL.so 12 8 0 0 8 0 0 2 /system/lib/libETC1.so 24 16 0 8 8 0 0 2 /system/lib/libGLESv1_CM.so 20 16 0 8 8 0 0 2 /system/lib/libGLESv2.so 128 84 2 72 12 0 0 2 /system/lib/libGLESv2_dbg.so 24 20 0 12 8 0 0 2 /system/lib/libQcomUI.so 640 272 132 124 20 0 128 3 /system/lib/libRS.so 56 52 1 40 12 0 0 2 /system/lib/libandroid.so 704 616 24 568 44 0 4 2 /system/lib/libandroid_runtime.so 4276 796 27 576 220 0 0 3 /system/lib/libbcc.so 844 220 7 204 16 0 0 2 /system/lib/libbcinfo.so 164 156 7 128 24 0 4 2 /system/lib/libbinder.so 12 8 0 0 8 0 0 2 /system/lib/libbluedroid.so 320 228 19 188 24 0 16 3 /system/lib/libc.so 124 120 3 96 24 0 0 2 /system/lib/libcamera_client.so 2472 1040 35 964 76 0 0 3 /system/lib/libchromium_net.so 8 8 0 0 8 0 0 2 /system/lib/libcnefeatureconfig.so 816 476 14 400 76 0 0 3 /system/lib/libcrypto.so 120 44 5 36 4 0 4 3 /system/lib/libcutils.so 300 56 1 48 8 0 0 2 /system/lib/libdbus.so 92 84 2 68 16 0 0 2 /system/lib/libdrmframework.so 748 640 50 580 44 0 16 3 /system/lib/libdvm.so 28 20 8 4 8 0 8 3 /system/lib/libemoji.so 52 12 0 4 8 0 0 3 /system/lib/libexif.so 80 8 0 0 8 0 0 2 /system/lib/libexpat.so 176 28 0 16 12 0 0 2 /system/lib/libflex.so 16 16 0 8 8 0 0 2 /system/lib/libgabi++.so 8 8 8 0 0 0 8 2 /system/lib/libgenlock.so 60 56 1 40 16 0 0 2 /system/lib/libgesture_client.so 216 208 10 168 36 0 4 2 /system/lib/libgui.so 8 8 0 0 8 0 0 2 /system/lib/libhardware.so 24 12 0 4 8 0 0 2 /system/lib/libhardware_legacy.so 256 52 1 44 8 0 0 2 /system/lib/libharfbuzz.so 140 60 2 48 12 0 0 2 /system/lib/libhwui.so 1168 700 28 672 24 0 4 2 /system/lib/libicui18n.so 992 692 36 644 36 0 12 3 /system/lib/libicuuc.so 216 20 0 12 8 0 0 2 /system/lib/libjpeg.so 16 16 0 8 8 0 0 2 /system/lib/liblog.so 92 52 5 40 8 0 4 3 /system/lib/libm.so 436 376 19 308 64 0 4 2 /system/lib/libmedia.so 88 84 2 72 12 0 0 2 /system/lib/libmedia_jni.so 24 24 4 16 4 0 4 2 /system/lib/libmemalloc.so 76 40 1 28 12 0 0 2 /system/lib/libmtp.so 216 176 5 160 16 0 0 2 /system/lib/libnativehelper.so 24 12 0 4 8 0 0 2 /system/lib/libnetutils.so 8 8 0 0 8 0 0 2 /system/lib/libnfc_ndef.so 972 112 3 92 20 0 0 3 /system/lib/libnode.so 112 60 1 48 12 0 0 2 /system/lib/libpixelflinger.so 16 16 0 8 8 0 0 2 /system/lib/libpowermanager.so 12 12 0 4 8 0 0 2 /system/lib/libqc-opt.so 8 8 0 0 8 0 0 2 /system/lib/libqrdinside.so 24 24 0 16 8 0 0 2 /system/lib/librs_jni.so 1228 836 49 804 24 0 8 3 /system/lib/libskia.so 336 20 0 12 8 0 0 3 /system/lib/libsonivox.so 328 304 15 292 8 0 4 2 /system/lib/libsqlite.so 184 116 3 96 20 0 0 2 /system/lib/libssl.so 1496 416 14 368 48 0 0 2 /system/lib/libstagefright.so 52 12 0 4 8 0 0 2 /system/lib/libstagefright_amrnb_common.so 40 16 4 4 8 0 4 3 /system/lib/libstagefright_avc_common.so 8 8 0 0 8 0 0 2 /system/lib/libstagefright_enc_common.so 44 36 1 28 8 0 0 2 /system/lib/libstagefright_foundation.so 12 8 0 0 8 0 0 2 /system/lib/libstagefright_yuv.so 8 8 0 0 8 0 0 2 /system/lib/libstdc++.so 208 16 4 8 4 0 4 2 /system/lib/libstlport.so 16 16 0 8 8 0 0 2 /system/lib/libtilerenderer.so 92 80 7 68 8 0 4 2 /system/lib/libui.so 12 8 0 0 8 0 0 2 /system/lib/libusbhost.so 188 172 8 160 8 0 4 2 /system/lib/libutils.so 2680 512 17 428 84 0 0 3 /system/lib/libv8.so 100 20 0 8 12 0 0 2 /system/lib/libvorbisidec.so 6364 1724 59 1200 524 0 0 3 /system/lib/libwebcore.so 12 12 0 4 8 0 0 2 /system/lib/libwpa_client.so 96 52 1 44 8 0 0 2 /system/lib/libz.so 36 8 0 0 8 0 0 2 /system/lib/libzipfile.so 14304 144 10 144 0 0 0 2 /system/usr/icu/icudt46l.dat 476 0 0 0 0 0 0 1 /system/usr/share/zoneinfo/zoneinfo.dat 9896 1392 978 0 432 0 960 29 [anon] 2756 2752 1675 0 1120 0 1632 2 [heap] 1160 28 24 0 4 0 24 3 [stack] 4 4 4 0 0 0 4 1 [vectors] -------- -------- -------- -------- -------- -------- -------- ---- ------------------------------ virtual shared shared private private size RSS PSS clean dirty clean dirty # object -------- -------- -------- -------- -------- -------- -------- ---- ------------------------------ 280360 44900 12195 18956 15624 60 10260 277 TOTAL
3: dumpsys meminfo 包名
4: VSS/RSS/PSS/USS 的区别
- VSS - Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)
- RSS - Resident Set Size 实际使用物理内存(包含共享库占用的内存)
- PSS - Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存)
- USS - Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存)
一般来说内存占用大小有如下规律:VSS >= RSS >= PSS >= 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.
5:通过Android系统提供的Runtime类,执行adb 命令(top,procrank,ps...等命令)查询
通过对执行结果的标准控制台输出进行解析。这样大大的扩展了Android查询功能.例如:
final Process m_process = Runtime.getRuntime().exec("/system/bin/top -n 1");
final StringBuilder sbread = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(m_process.getInputStream()), 8192);
# procrank
Runtime.getRuntime().exec("/system/xbin/procrank");
6:android.os.Debug查询PSS,VSS,USS等单个进程使用内存信息
MemoryInfo[] memoryInfoArray = am.getProcessMemoryInfo(pids); MemoryInfo pidMemoryInfo=memoryInfoArray[0]; pidMemoryInfo.getTotalPrivateDirty(); //Return total private dirty memory usage in kB. USS
7:root@android:/ # cat /proc/meminfo
使用adb shell cat /proc/meminfo 命令,查看剩余的空间还有多少,通常的系统剩余空间是: MemFree + cached
MemTotal: 483724 kB
MemFree: 5616 kB
Buffers: 2732 kB
Cached: 158632 kB
SwapCached: 0 kB
Active: 277336 kB
Inactive: 83232 kB
Active(anon): 197452 kB
root@android:/ # cat /proc/meminfo MemTotal: 835616 kB MemFree: 251068 kB Buffers: 42128 kB Cached: 218088 kB SwapCached: 0 kB Active: 402312 kB Inactive: 116296 kB Active(anon): 261152 kB Inactive(anon): 300 kB Active(file): 141160 kB Inactive(file): 115996 kB Unevictable: 2728 kB Mlocked: 0 kB HighTotal: 121856 kB HighFree: 416 kB LowTotal: 713760 kB LowFree: 250652 kB SwapTotal: 0 kB SwapFree: 0 kB Dirty: 40 kB Writeback: 0 kB AnonPages: 261132 kB Mapped: 66860 kB Shmem: 348 kB Slab: 23940 kB SReclaimable: 13876 kB SUnreclaim: 10064 kB KernelStack: 5296 kB PageTables: 8652 kB NFS_Unstable: 0 kB Bounce: 0 kB WritebackTmp: 0 kB CommitLimit: 417808 kB Committed_AS: 3873120 kB VmallocTotal: 196608 kB VmallocUsed: 163088 kB VmallocChunk: 11204 kB
8:
[root@localhost ~]# vmstat -n 3 (每个3秒刷新一次)
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
1 0 144 186164 105252 2386848 0 0 18 166 83 2 48 21 31 0
2 0 144 189620 105252 2386848 0 0 0 177 1039 1210 34 10 56 0
0 0 144 214324 105252 2386848 0 0 0 10 1071 670 32 5 63 0
0 0 144 202212 105252 2386848 0 0 0 189 1035 558 20 3 77 0
2 0 144 158772 105252 2386848 0 0 0 203 1065 2832 70 14 15 0
vmstat所带参数请用man vmstat查看
Procs
r: 等待运行的进程数 b: 处在非中断睡眠状态的进程数 w: 被交换出去的可运行的进程数。此数由 linux 计算得出,但 linux 并不耗尽交换空间
Memory
swpd: 虚拟内存使用情况,单位:KB
free: 空闲的内存,单位KB
buff: 被用来做为缓存的内存数,单位:KB
Swap
si: 从磁盘交换到内存的交换页数量,单位:KB/秒
so: 从内存交换到磁盘的交换页数量,单位:KB/秒
IO
bi: 发送到块设备的块数,单位:块/秒
bo: 从块设备接收到的块数,单位:块/秒
System
in: 每秒的中断数,包括时钟中断
cs: 每秒的环境(上下文)切换次数
CPU
按 CPU 的总使用百分比来显示
us: CPU 使用时间
sy: CPU 系统使用时间
id: 闲置时间
准测
r,b≈0,
如果fre,将会出现连续不断的页面调度,将导致系统性能问题。
对于page列,re,pi,po,cy维持于比较稳定的状态,PI率不超过5,如果有pagin发生,那么关联页面必须先进行pageout
在内存相对紧张的环境下pagein会强制对不同的页面进行steal操作。如果系统正在读一个大批的永久页面,你也许可以看到po和pi列
会出现不一致的增长,这种情景并不一定表明系统负载过重,但是有必要对应用程序的数据访问模式进行见检查。在稳定的情况下,扫描率和重置率几乎相等,在
多个进程处理使用不同的页面的情况下,页面会更加不稳定和杂乱,这时扫描率可能会比重置率高出。
faults列,in,sy,cs会不断跳跃,这里没有明确的限制,唯一的就是这些值最少大于100
cpu列,us,sys,id和wa也是不确定的,最理想的状态是使cpu处于100%工作状态,单这只适合单用户的情况下。
如果在多用户环境中us+sys》80,进程就会在运行队列中花费等待时间,响应时间和吞吐量就会下降。wa>40表明磁盘io没有也许存在不合理的平衡,或者对磁盘操作比较频繁,
vmstat各项:
procs:
r-->在运行队列中等待的进程数
b-->在等待io的进程数
w-->可以进入运行队列但被替换的进程
memoy
swap-->现时可用的交换内存(k表示)
free-->空闲的内存(k表示)
pages
re--》回收的页面
mf--》非严重错误的页面
pi--》进入页面数(k表示)
po--》出页面数(k表示)
fr--》空余的页面数(k表示)
de--》提前读入的页面中的未命中数
sr--》通过时钟算法扫描的页面
disk 显示每秒的磁盘操作。 s表示scsi盘,0表示盘号
fault 显示每秒的中断数
in--》设备中断
sy--》系统中断
cy--》cpu交换
cpu 表示cpu的使用状态
cs--》用户进程使用的时间
sy--》系统进程使用的时间
id--》cpu空闲的时间
如果 r经常大于 4 ,且id经常少于40,表示cpu的负荷很重。
如果pi,po 长期不等于0,表示内存不足。
如果disk 经常不等于0, 且在 b中的队列 大于3, 表示 io性能不好。
Linux在具有高稳定性、可靠性的同时,具有很好的可伸缩性和扩展性,能够针对不同的应用和硬件环境调整,优化出满足当前应用需要的最佳性能。因此企业在维护Linux系统、进行系统调优时,了解系统性能分析工具是至关重要的。
在Linux下有很多系统性能分析工具,比较常见的有top、free、ps、time、timex、uptime等。下文将介绍几个较为重要的性能分析工具vmstat、iostat和sar及其使用。
用vmstat监视内存使用情况
vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存、进程、CPU活动进行监视。它是对系统的整体情况进行统计,不足之处是无法对某个进程进行深入分析。
vmstat的语法如下:
vmstat [-V] [-n] [delay [count]]
其中,-V表示打印出版本信息;-n表示在周期性循环输出时,输出的头部信息仅显示一次;delay是两次输出之间的延迟时间;count是指按照这个时间间隔统计的次数。对于vmstat输出各字段的含义,可运行man vmstat查看
root@android:/ # procrank