文章目录
一、进程线程相关命令
1.1 通过进程名字查看进程号
在 Android 开发调试中,查找进程 PID 是最基础的操作之一。
pidof 命令(最直接的方式):
adb shell pidof com.example.app
输出示例:
adb shell pidof com.example.virtualscreendemo
4179
该命令直接返回进程 ID,适合脚本化使用。
ps 命令结合 grep 过滤:
adb shell ps -A | grep com.example.app
输出示例:
adb shell ps -A | grep exam
u0_a186 4179 946 14523504 167736 SyS_epoll_wait 0 S com.example.virtualscreendemo
pgrep 命令:
adb shell pgrep -f com.xxxx
示例:
$ adb shell pgrep -f com.example.virtualscreendemo
4179
$ adb shell pgrep -f com.example.vir
4179
$ adb shell pgrep -f com.ex
4179
$ adb shell pgrep -f com
680
705
945
1016
1069
1.2 通过进程号查看进程执行文件
Android 系统调试时候,经常需要查看某个进程名字对应的可执行文件具体在哪个目录的需求,/proc 文件系统提供了完整的进程信息。
查看可执行文件路径:
adb shell ls -l /proc/进程号/exe
输出示例:
adb shell ps -A | grep audioserver
audioserver 1004 1 75680 23264 binder_ioctl_write_read 0 S android.hardware.audio.service
audioserver 1091 1 11564132 35220 binder_ioctl_write_read 0 S audioserver
获取进程号1091
adb shell ls -l /proc/1091/exe
lrwxrwxrwx 1 audioserver audio 0 1970-02-01 11:29 /proc/1091/exe -> /system/bin/audioserver
注意对于普通的apk启动输出是有点特殊的:
adb shell ps -A | grep exam
u0_a186 4179 946 14523504 167736 SyS_epoll_wait 0 S com.example.virtualscreendemo
得出virtualscreendemo的进程号4179
adb shell ls -l /proc/4179/exe
lrwxrwxrwx 1 u0_a186 u0_a186 0 2026-06-23 18:46 /proc/4179/exe -> /system/bin/app_process64
可以看到这里本质apk是由
/system/bin/app_process64
这个native程序拉起的。
查看进程启动命令及参数:
adb shell cat /proc/1091/cmdline
/system/bin/audioserver
1.3 查看进程下的所有线程
Android 应用的每个进程默认包含主线程(UI 线程)和多个工作线程。
使用 top 命令查看线程:
adb shell top -H -p 1091
输出结果
Threads: 21 total, 0 running, 21 sleeping, 0 stopped, 0 zombie
Mem: 5748944K total, 3641604K used, 2107340K free, 12076K buffers
Swap: 3145724K total, 0K used, 3145724K free, 1810672K cached
800%cpu 17%user 0%nice 10%sys 772%idle 0%iow 0%irq 0%sirq 0%host
TID USER PR NI VIRT RES SHR S[%CPU] %MEM TIME+ THREAD
4717 audioserver 20 0 11G 34M 27M S 0.0 0.6 0:00.00 binder:1091_6
4715 audioserver 20 0 11G 34M 27M S 0.0 0.6 0:00.00 binder:1091_5
2675 audioserver 20 0 11G 34M 27M S 0.0 0.6 0:00.29 binder:1091_4
2147 audioserver 20 0 11G 34M 27M S 0.0 0.6 0:00.44 binder:1091_3
1754 audioserver 1 -19 11G 34M 27M S 0.0 0.6 0:00.12 AudioOut_3D
1753 audioserver -4 -19 11G 34M 27M S 0.0 0.6 0:00.00 FastMixer
1747 audioserver 1 -19 11G 34M 27M S 0.0 0.6 0:00.12 AudioOut_2D
1746 audioserver -4 -19 11G 34M 27M S 0.0 0.6 0:00.00 FastMixer
1745 audioserver 20 0 11G 34M 27M S 0.0 0.6 0:00.00 HwBinder:1091_3
1737 audioserver 1 -19 11G 34M 27M S 0.0 0.6 0:00.07 AudioOut_15
1701 audioserver 1 -19 11G 34M 27M S 0.0 0.6 0:00.69 AudioOut_D
1699 audioserver -4 -19 11G 34M 27M S 0.0 0.6 0:02.14 FastMixer
1674 audioserver 20 0 11G 34M 27M S 0.0 0.6 0:00.03 TimerThread
1425 audioserver 4 -16 11G 34M 27M S 0.0 0.6 0:00.18 ApmOutput
1424 audioserver 4 -16 11G 34M 27M S 0.0 0.6 0:00.00 ApmAudio
1208 audioserver 4 -16 11G 34M 27M S 0.0 0.6 0:00.00 AudioFlinger_Pa
1201 audioserver 20 0 11G 34M 27M S 0.0 0.6 0:00.00 HwBinder:1091_1
1202 audioserver 20 0 11G 34M 27M S 0.0 0.6 0:00.00 HwBinder:1091_2
1148 audioserver 20 0 11G 34M 27M S 0.0 0.6 0:00.02 binder:1091_1
^[[73;274R^Cerver 20 0 11G 34M 27M S 0.0 0.6 0:00.05 binder:1091_2
顶部 Threads 行的 total 值即为线程总数。
使用 ps 命令:
adb shell ps -T -p 1091
输出结果如下
USER PID TID PPID VSZ RSS WCHAN ADDR S CMD
audioserver 1091 1091 1 11564132 35220 binder_ioctl_write_read 0 S audioserver
audioserver 1091 1148 1 11564132 35220 binder_ioctl_write_read 0 S binder:1091_1
audioserver 1091 1149 1 11564132 35220 binder_ioctl_write_read 0 S binder:1091_2
audioserver 1091 1201 1 11564132 35220 binder_ioctl_write_read 0 S HwBinder:1091_1
audioserver 1091 1202 1 11564132 35220 binder_ioctl_write_read 0 S HwBinder:1091_2
audioserver 1091 1208 1 11564132 35220 futex_wait_queue_me 0 S AudioFlinger_Pa
audioserver 1091 1424 1 11564132 35220 futex_wait_queue_me 0 S ApmAudio
audioserver 1091 1425 1 11564132 35220 futex_wait_queue_me 0 S ApmOutput
audioserver 1091 1674 1 11564132 35220 futex_wait_queue_me 0 S TimerThread
audioserver 1091 1699 1 11564132 35220 futex_wait_queue_me 0 S FastMixer
audioserver 1091 1701 1 11564132 35220 futex_wait_queue_me 0 S AudioOut_D
audioserver 1091 1737 1 11564132 35220 futex_wait_queue_me 0 S AudioOut_15
audioserver 1091 1745 1 11564132 35220 binder_ioctl_write_read 0 S HwBinder:1091_3
audioserver 1091 1746 1 11564132 35220 futex_wait_queue_me 0 S FastMixer
audioserver 1091 1747 1 11564132 35220 futex_wait_queue_me 0 S AudioOut_2D
audioserver 1091 1753 1 11564132 35220 futex_wait_queue_me 0 S FastMixer
audioserver 1091 1754 1 11564132 35220 futex_wait_queue_me 0 S AudioOut_3D
audioserver 1091 2147 1 11564132 35220 binder_ioctl_write_read 0 S binder:1091_3
audioserver 1091 2675 1 11564132 35220 binder_ioctl_write_read 0 S binder:1091_4
audioserver 1091 4715 1 11564132 35220 binder_ioctl_write_read 0 S binder:1091_5
audioserver 1091 4717 1 11564132 35220 binder_ioctl_write_read 0 S binder:1091_6
单独读取进的线程数量:
adb shell grep Threads /proc/1541/status
Threads: 36
二、/proc 相关的数据
/proc 是 Android 系统中最重要的虚拟文件系统,它不占用实际磁盘空间,所有内容由内核在访问时动态生成。通过 /proc,开发者可以获取系统内核、硬件资源以及运行中进程的实时数据。
2.1 系统级 /proc 文件
表格
路径 作用
/proc/cpuinfo 显示 CPU 详细信息(型号、核心数、频率等)
/proc/meminfo 显示物理及虚拟内存使用情况
/proc/loadavg 系统平均负载(1分钟、5分钟、15分钟)
/proc/version 系统内核版本信息
/proc/uptime 系统启动时间和空闲时间
/proc/stat 系统简要统计信息
/proc/net/ 网络相关统计信息
/proc/diskstats 磁盘和分区统计信息
读取 CPU 信息示例:
adb shell cat /proc/cpuinfo

读取内存信息示例:
adb shell cat /proc/meminfo
MemTotal: 11550280 kB
MemFree: 1106328 kB
MemAvailable: 4676816 kB
Buffers: 2340 kB
Cached: 3859768 kB
SwapCached: 1141944 kB
Active: 2100504 kB
Inactive: 4091368 kB
Active(anon): 914044 kB
Inactive(anon): 2285092 kB
Active(file): 1186460 kB
Inactive(file): 1806276 kB
Unevictable: 860824 kB
Mlocked: 231604 kB
SwapTotal: 12582908 kB
SwapFree: 8028868 kB
Dirty: 156 kB
Writeback: 0 kB
AnonPages: 2592532 kB
Mapped: 1506168 kB
Shmem: 651404 kB
KReclaimable: 821784 kB
Slab: 885216 kB
SReclaimable: 280632 kB
SUnreclaim: 604584 kB
KernelStack: 128800 kB
ShadowCallStack: 32224 kB
PageTables: 221976 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 18358048 kB
Committed_AS: 148925136 kB
VmallocTotal: 263061440 kB
VmallocUsed: 376316 kB
VmallocChunk: 0 kB
Percpu: 16896 kB
AnonHugePages: 0 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
FileHugePages: 0 kB
FilePmdMapped: 0 kB
CmaTotal: 266240 kB
CmaFree: 0 kB
关键字段说明:
MemTotal:所有可用 RAM 大小
MemFree:未被使用的内存
Cached:被高速缓冲存储器使用的内存
SwapTotal:交换空间总大小
2.2 进程级 /proc/[PID] 目录
每个运行中的进程在 /proc 下都有一个以 PID 命名的子目录,包含该进程的详细信息。
proc的子目录数据详细列表对应
路径 作用
/proc/PID/exe 可执行文件的符号链接
/proc/PID/cwd 当前工作目录
/proc/PID/cmdline 启动命令及参数
/proc/PID/environ 进程环境变量
/proc/PID/status 进程状态(含 Threads 线程数)
/proc/PID/fd/ 打开的文件描述符目录
/proc/PID/maps 内存映射信息
三、env 相关的命令
3.1 查看进程环境变量
通过 /proc 查看指定进程的环境变量:
adb shell cat /proc/1091/environ
输出如下

3.2 查看当前 shell 环境变量(注意不是具体某个进程)
adb shell env
adb shell printenv
adb shell echo $PATH

这里需要注意每个进程都会有自己的env环境变量,切勿把adb shell env当成所有的进程env
四、top 查看 CPU 相关的命令
4.1 基本用法
查看整体 CPU 使用率:
adb shell top
常用参数:
adb shell top --help
Toybox 0.8.11-android multicall binary (see toybox --help)
usage: top [-Hhbq] [-k FIELD,] [-o FIELD,] [-s SORT] [-n NUMBER] [-m LINES] [-d SECONDS] [-p PID,] [-u USER,]
Show process activity in real time.
-H Show threads
-h Usage graphs instead of text
-k Fallback sort FIELDS (default -S,-%CPU,-ETIME,-PID)
-o Show FIELDS (def PID,USER,PR,NI,VIRT,RES,SHR,S,%CPU,%MEM,TIME+,CMDLINE)
-O Add FIELDS (replacing PR,NI,VIRT,RES,SHR,S from default)
-s Sort by field number (1-X, default 9)
-b Batch mode (no tty)
-d Delay SECONDS between each cycle (default 3)
-m Maximum number of tasks to show
-n Exit after NUMBER iterations
-p Show these PIDs
-u Show these USERs
-q Quiet (no header lines)
Cursor UP/DOWN or LEFT/RIGHT to move list, SHIFT LEFT/RIGHT to change sort,
space to force update, R to reverse sort, Q to exit.
示例:
adb shell top -m 10
4.2 top 输出解读
系统信息栏:
Tasks: 2 total, 0 running, 2 sleeping, 0 stopped, 0 zombie
total:当前系统进程总数
running:当前运行中的进程数
sleeping:处于等待状态的进程数
stopped:被停止的进程数
zombie:僵尸进程数
CPU 状态行:
text
%Cpu(s): 12.5 us, 3.2 sy, 0.0 ni, 82.1 id, 1.8 wa, 0.2 hi, 0.2 si, 0.0 st
字段 含义
us 用户空间 CPU 占用率
sy 内核空间 CPU 占用率
ni 改变过优先级的进程 CPU 占用率
id 空闲 CPU 百分比
wa I/O 等待占用 CPU 百分比
hi 硬件中断占用
si 软件中断占用
st 虚拟机被宿主机抢占的时间
进程列表栏解答:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ ARGS
PID:进程 ID
USER:进程所属用户
PR:进程优先级
NI:Nice 值(调度优先级)
VIRT:虚拟内存使用量
RES:常驻物理内存使用量
SHR:共享内存使用量
S:进程状态(R=运行中,S=睡眠中,D=不可中断睡眠,Z=僵尸进程)
%CPU:CPU 使用率
%MEM:内存使用率
TIME+:进程运行总时间
ARGS:进程启动命令和参数
注意:在多核处理器上,多个进程的 CPU 使用率累加可能超过 100%,这是因为系统将各核心上的 CPU 使用率相加计算总使用率,属于正常现象。
原文参考:
47

被折叠的 条评论
为什么被折叠?



