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

时间:2021-11-01 06:19:05

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