欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android系统检测程序内存占用各种方法

程序员文章站 2022-03-23 14:12:15
1.检查系统总内存 复制代码 代码如下: liuhx@uc ~ $ adb shell cat /proc/meminfo memtotal:  ...

1.检查系统总内存

复制代码 代码如下:

liuhx@uc ~ $ adb shell cat /proc/meminfo
memtotal:         840868 kb
memfree:          457344 kb
buffers:            1744 kb
cached:           203064 kb
swapcached:            0 kb
active:           234932 kb
inactive:         129644 kb
active(anon):     170292 kb
inactive(anon):        0 kb
active(file):      64640 kb
inactive(file):   129644 kb
unevictable:           0 kb
mlocked:               0 kb
swaptotal:             0 kb
swapfree:              0 kb
dirty:                 0 kb
writeback:             0 kb
anonpages:        159820 kb
mapped:            97916 kb
slab:               7808 kb
sreclaimable:       2920 kb
sunreclaim:         4888 kb
pagetables:         3516 kb
nfs_unstable:          0 kb
bounce:                0 kb
writebacktmp:          0 kb
commitlimit:      420432 kb
committed_as:    1295272 kb
vmalloctotal:     122880 kb
vmallocused:       16508 kb
vmallocchunk:      76804 kb

第一行memtotal就是总内存。一般并不会等于手机标榜的数值。说是2g内存的手机,会在1.8g左右

2.检查某个程序的各类型内存占用

可用dumpsys meminfo 包名。不知道包名的话,可以用ps命令得到所有运行的程序,也包括得到pid。以自带的浏览器为例:

复制代码 代码如下:

liuhx@uc ~ $ adb shell dumpsys meminfo com.android.browser
applications memory usage (kb):
uptime: 23140065 realtime: 23140065

** meminfo in pid 939 [com.android.browser] **
                         shared  private     heap     heap     heap
                   pss    dirty    dirty     size    alloc     free
                ------   ------   ------   ------   ------   ------
       native     7111      748     7048     8964     8634      329
       dalvik     7338     5096     6892     7568     7041      527
       cursor        0        0        0                          
       ashmem      276        0      276                          
    other dev        4        0        0                          
     .so mmap     5050     1728      252                          
    .jar mmap        0        0        0                          
    .apk mmap      418        0        0                          
    .ttf mmap      439        0        0                          
    .dex mmap        0        0        0                          
   other mmap     2320       16       24                          
      unknown     5173       12     5172                          
        total    28129     7600    19664    16532    15675      856
 
 objects
               views:      104         viewrootimpl:        1
         appcontexts:        4           activities:        1
              assets:        2        assetmanagers:        2
       local binders:       15        proxy binders:       19
    death recipients:        0
     openssl sockets:        0
 
 sql
         memory_used:     1468
  pagecache_overflow:      899          malloc_size:      168
 
 databases
      pgsz     dbsz   lookaside(b)          cache  dbname
         4     1180             68        54/55/9  /data/data/com.android.browser/databases/browser2.db
         4     1180             25         1/60/2  /data/data/com.android.browser/databases/browser2.db (2)
         4     1180             65        90/23/7  /data/data/com.android.browser/databases/browser2.db (1)
         4       40             18         0/15/1  /data/data/com.android.browser/databases/webview.db
 
 asset allocations
    zip:/system/app/browser.apk:/resources.arsc: 881k

dumpsys的输出结果在不同版本的android系统可能不同。

其中pss total是程序占用的pss。

3.检查程序状态

复制代码 代码如下:

liuhx@uc ~ $ adb shell cat /proc/939/status
name: android.browser
state: s (sleeping)
tgid: 939
pid: 939
ppid: 38
tracerpid: 0
uid: 10013 10013 10013 10013
gid: 10013 10013 10013 10013
fdsize: 256
groups: 1015 1028 3003 50013
vmpeak:   215504 kb
vmsize:   206780 kb
vmlck:        0 kb
vmhwm:    52108 kb
vmrss:    47748 kb
vmdata:    53616 kb
vmstk:       84 kb
vmexe:        8 kb
vmlib:    29300 kb
vmpte:      156 kb
threads: 29
sigq: 0/6656
sigpnd: 0000000000000000
shdpnd: 0000000000000000
sigblk: 0000000000001204
sigign: 0000000000000000
sigcgt: 00000002000094e8
capinh: 0000000000000000
capprm: 0000000000000000
capeff: 0000000000000000
capbnd: fffffffffffffeff
voluntary_ctxt_switches: 451
nonvoluntary_ctxt_switches: 3563

vmrss是程序占用的rss

4.检查程序各部分的内存占用。

复制代码 代码如下:

cat /proc/939/smaps

这个命令的输出结果很长,下面是一部分结果:
复制代码 代码如下:

4b399000-4b39b000 r--p 0001d000 1f:00 560        /system/lib/libglesv1_enc.so
size:                  8 kb
rss:                   8 kb
pss:                   8 kb
shared_clean:          0 kb
shared_dirty:          0 kb
private_clean:         0 kb
private_dirty:         8 kb
referenced:            8 kb
swap:                  0 kb
kernelpagesize:        4 kb
mmupagesize:           4 kb
4b39b000-4b39c000 rw-p 0001f000 1f:00 560        /system/lib/libglesv1_enc.so
size:                  4 kb
rss:                   4 kb
pss:                   4 kb
shared_clean:          0 kb
shared_dirty:          0 kb
private_clean:         0 kb
private_dirty:         4 kb
referenced:            4 kb
swap:                  0 kb
kernelpagesize:        4 kb
mmupagesize:           4 kb