Android 应用开发大家都知道可以通过DDMS来查看应用程序进程占用的内存大小;然而Native
内存并不能在虚拟堆上看到;Android系统基于Linux,这样的话其具备Linux的大多数特性;在Linux当中有不少方法可以查看进程占用的内存,可以采用图形界面工具,也可以采用命令,具体如何查看这边就不介绍了。
Android Native 内存查看:
1.首先需要确保你的PC上装了adb工具;不同平台安装相应的版本。
2.adb shell 然后通过ps 命令查看手机上的进程。
[html] view
plain copy
- u0_a101 18271 174 656392 32856 ffffffff 00000000 S com.baidu.map.location
- u0_a101 18286 174 654332 34288 ffffffff 00000000 S com.baidu.map.location:re
- mote
- u0_a58 18325 174 673716 38868 ffffffff 00000000 S com.xiaomi.channel:pushse
- rvice
- u0_a58 18347 174 688288 43628 ffffffff 00000000 S com.xiaomi.channel
- root 18380 2 0 0 ffffffff 00000000 S kworker/0:3
- u0_a11 18440 174 686980 41256 ffffffff 00000000 S com.google.process.gapps
- u0_a11 18470 174 674016 36508 ffffffff 00000000 S com.google.android.gms
- u0_a11 18497 174 676944 38440 ffffffff 00000000 S com.google.android.gsf.lo
- gin
- u0_a11 18517 174 684580 41268 ffffffff 00000000 S com.google.process.locati
- on
- u0_a12 18588 174 654444 32760 ffffffff 00000000 S com.miui.cloudservice
- u0_a74 18623 174 838968 123264 ffffffff 00000000 R com.android.chrome
- u0_i134 18654 174 747068 56468 ffffffff 00000000 S com.android.chrome:sandbo
- xed_process0
- u0_a23 18767 174 654368 32372 ffffffff 00000000 S com.google.android.partne
- rsetup
3.查看进程:下面以com.android.chrome为例
找到com.android.chrome 进程id是18623:在终端上输入:
[plain] view
plain copy
- shell@android:/ $ cat /proc/18623/status
[plain] view
plain copy
- cat /proc/18623/status
- Name: .android.chrome
- State: S (sleeping)
- Tgid: 18623
- Pid: 18623
- PPid: 174
- TracerPid: 0
- Uid: 10074 10074 10074 1007
- Gid: 10074 10074 10074 1007
- FDSize: 256
- Groups: 1006 1015 1028 3003
- VmPeak: 842864 kB
- VmSize: 811216 kB
- VmLck: 0 kB
- VmPin: 0 kB
- VmHWM: 126080 kB
- <strong>VmRSS: 125484 kB</strong>
- VmData: 69156 kB
- VmStk: 136 kB
- VmExe: 8 kB
- VmLib: 68128 kB
- VmPTE: 298 kB
- VmSwap: 0 kB
- Threads: 46
- SigQ: 0/14952
- SigPnd: 0000000000000000
- ShdPnd: 0000000000000000
- SigBlk: 0000000000001204
- SigIgn: 0000000000000000
- SigCgt: 00000002000094e8
- CapInh: 0000000000000000
- CapPrm: 0000000000000000
- CapEff: 0000000000000000
- CapBnd: ffffffffffffffff
- Cpus_allowed: f
- Cpus_allowed_list: 0-3
- voluntary_ctxt_switches: 2759
- nonvoluntary_ctxt_switches: 2591
4.分析内存
熟悉Linux的童鞋知道proc目录下存放的是进程的各种信息;
第三步中看到的VmRSS就是进程当前占用的实际内存。
具体其他参数可以参考:进程信息参数详解
[plain] view
plain copy
- <span style="font-size:18px">rss %ld (24) Resident Set Size: number of pages the
- process has in real memory. This is just the
- pages which count toward text, data, or stack
- space. This does not include pages which have not
- been demand-loaded in, or which are swapped out.</span>
5.ps会列出很多进程我们有时候需要直接找到对应的进程,linux大家都知道可以通过grep、或者find找到直接找到进程;
adb shell;trim版本的linux ,不直接支持这些命令;不过有个方法是通过busybox来解决;下面以查找chrome为例:
[plain] view
plain copy
- <span style="font-size:18px">shell@android:/ $ ps | busybox grep chrome
- ps | busybox grep chrome
- u0_a74 18623 174 837800 125568 ffffffff 00000000 S com.android.chrome
- u0_i134 18654 174 752124 55908 ffffffff 00000000 S com.android.chrome:sandbo
- xed_process0</span>
时间: 2024-10-27 06:04:09