查看App 进程的内存信息

/ 0评 / 0

查看单个 App 进程的内存信息

adb shell dumpsys meminfo <pid> // 输出指定pid的某一进程
adb shell dumpsys meminfo --package <packagename> // 输出指定包名的进程,可能包含多个进程
adb shell dumpsys meminfo com.XXXXXX  //只输出指定进程,不包含子进程

输出如下

Applications Memory Usage (in Kilobytes):
Uptime: 1766002884 Realtime: 1766002884

** MEMINFO in pid 14784 [com.XXXXXX] **
                   Pss  Private  Private  SwapPss      Rss     Heap     Heap     Heap
                 Total    Dirty    Clean    Dirty    Total     Size    Alloc     Free
                ------   ------   ------   ------   ------   ------   ------   ------
  Native Heap   372283   372248        0        7   374496   476160   383856    92303
  Dalvik Heap    66390    66344        0       15    69108    81041    56465    24576
 Dalvik Other    43109    35712        0        0    50940                           
        Stack     7656     7656        0        0     7668                           
       Ashmem     1068      172        0        0     1976                           
    Other dev       82        0       76        0      828                           
     .so mmap    27643     1228    22776       18    70972                           
    .jar mmap     4824        0     2468        0    39412                           
    .apk mmap    46778      480    20756        0   128800                           
    .ttf mmap     1362        0      732        0     2380                           
    .dex mmap   198318   194696     1916        0   200908                           
    .oat mmap       45        0        0        0     3276                           
    .art mmap     9584     9392        0       14    21140                           
   Other mmap     7867       32     7072        0    10596                           
   EGL mtrack    12432    12432        0        0    12432                           
    GL mtrack   212544   212544        0        0   212544                           
      Unknown    12597    12592        0        0    13152                           
        TOTAL  1024636   925528    55796       54  1024636   557201   440321   116879

 App Summary
                       Pss(KB)                        Rss(KB)
                        ------                         ------
           Java Heap:    75736                          90248
         Native Heap:   372248                         374496
                Code:   245200                         460792
               Stack:     7656                           7668
            Graphics:   224976                         224976
       Private Other:    55508
              System:    43312
             Unknown:                                   62448

           TOTAL PSS:  1024636            TOTAL RSS:  1220628       TOTAL SWAP PSS:       54

 Objects
               Views:     6207         ViewRootImpl:        2
         AppContexts:       14           Activities:        1
              Assets:       62        AssetManagers:        0
       Local Binders:       82        Proxy Binders:       61
       Parcel memory:       45         Parcel count:      183
    Death Recipients:        6      OpenSSL Sockets:       21
            WebViews:        1

 SQL
         MEMORY_USED:     1190
  PAGECACHE_OVERFLOW:      235          MALLOC_SIZE:      117

 DATABASES
      pgsz     dbsz   Lookaside(b)          cache  Dbname
         4       96            109       36/63/13  /data/user/0/com/no_backup/androidx.work.workdb
         4        8                         0/0/0    (attached) temp
         4       96             32         2/15/3  /data/user/0/com.XXXXXX/no_backup/androidx.work.workdb (2)
         4       56             32         1/22/2  /data/user/0/com.XXXXXX/databases/com.google.android.datatransport.events
         4       32             96        25/45/8  /data/user/0/com.XXXXXX/databases/pop_db
         4        8                         0/0/0    (attached) temp
         4       12             25         3/17/2  /data/user/0/com.XXXXXX/databases/feature_table_list.db
         4       28             39       487/19/4  /data/user/0/com.XXXXXX/databases/xx.db

部分字段说明

Item 全称 含义 等价
USS Unique Set Size 物理内存 进程独占的内存
PSS Proportional Set Size 物理内存 PSS= USS+ 按比例包含共享库
RSS Resident Set Size 物理内存 RSS= USS+ 包含共享库
VSS Virtual Set Size 虚拟内存 VSS= RSS+ 未分配实际物理内存

内存计数中的类别如下:

来源文档

status文件

//获取pid
adb shell ps -A | grep 应用包名
//获取进程信息
adb shell  cat /proc/[获取pid]/status

输出如下

Name:   com.XXXXX
Umask:  0077
State:  S (sleeping)
Tgid:   14784
Ngid:   0
Pid:    14784
PPid:   785
TracerPid:  0
Uid:    10618   10618   10618   10618
Gid:    10618   10618   10618   10618
FDSize: 1024
Groups: 3001 3002 3003 9997 20618 50618 99909997 
NStgid: 14784
NSpid:  14784
NSpgid: 785
NSsid:  0
VmPeak: 58397952 kB
VmSize: 27277888 kB
VmLck:         0 kB
VmPin:         0 kB
VmHWM:   1407908 kB
VmRSS:   1364556 kB
RssAnon:      702284 kB
RssFile:      643680 kB
RssShmem:      18592 kB
VmData:  2582516 kB
VmStk:      8192 kB
VmExe:        28 kB
VmLib:    341128 kB
VmPTE:      5484 kB
VmPMD:        72 kB
VmSwap:     7132 kB
Threads:    205
SigQ:   2/27433
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000080001204
SigIgn: 0000000000000001
SigCgt: 0000406e400086f8
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: 0000000000000000
CapAmb: 0000000000000000
NoNewPrivs: 0
Seccomp:    2
Speculation_Store_Bypass:   thread vulnerable
Cpus_allowed:   ff
Cpus_allowed_list:  0-7
Mems_allowed:   1
Mems_allowed_list:  0
voluntary_ctxt_switches:    327859
nonvoluntary_ctxt_switches: 8438

直接dump内存

adb shell am dumpheap com.yy.hiyo /data/local/tmp/xxx.hprof

Android Studio导出转换为MAT使用

SDK/platform-tools/hprof-conv memory-20190429T103715.hprof a.hprof 

使用smaps获取内存信息

//获取pid
adb shell ps -A | grep 应用包名
//读取smaps文件到本地
adb shell run-as  应用包名  cat /proc/pid/smaps > out.txt

smaps数据解析

6e8fc00000-6e8fe00000 rw-p 00000000 00:00 0                              [anon:libc_malloc]
Name:           [anon:libc_malloc]
Size:               2048 kB
KernelPageSize:        4 kB
MMUPageSize:           4 kB
Rss:                1020 kB
Pss:                1020 kB
Shared_Clean:          0 kB
Shared_Dirty:          0 kB
Private_Clean:         0 kB
Private_Dirty:      1020 kB
Referenced:         1008 kB
Anonymous:          1020 kB
LazyFree:              0 kB
AnonHugePages:         0 kB
ShmemPmdMapped:        0 kB
Shared_Hugetlb:        0 kB
Private_Hugetlb:       0 kB
Swap:                  0 kB
SwapPss:               0 kB
Locked:                0 kB
VmFlags: rd wr mr mw me nr 
  1. e2600000-e2680000: 虚拟内存的地址空间;
  2. [anon:libc_malloc] :这一段虚拟内存是由libc的malloc所申请;
  3. Size: 这一段的占用虚拟内存大小为512K;
  4. Private_Dirty: 也称"脏页",也就是实际申请的内存大小,也是本进程独占的;
  5. Shared_Dirty:是相对于Private_Dirty而言,表示这一块内存同时被其他进程所引用;
  6. Pss: Private_Dirty + SharedDirty的公摊部分,为195k
  7. Rss: Private_Dirty + SharedDirty , 176+152 = 328 k

可以通过三方库Android-App-Memory-Analysis进行解析

python smaps_parser.py -f out.txt > out_smaps_parser.txt

https://zhuanlan.zhihu.com/p/371527210

https://github.com/Tencent/loli_profiler

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注