First, view the top command
First run:
abd shell
Then execute:
top --help
Note: Different Android system, top command is different.
Android N (level = 25) before and
Invalid argument "-p".
Usage: top [ -m max_procs ] [ -n iterations ] [ -d delay ] [ -s sort_column ] [ -t ] [ -h ]
-m num Maximum number of processes to display.
-n num 显示n次top的结果后命令就会退出,例如:top -n 1
-d num Seconds to wait between updates.
-s col Column to sort by (cpu,vss,rss,thr).
-H Show threads instead of processes.
-h Display this help screen.
Android O (level = 26) and after
usage: top [-Hbq] [-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
-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 选择根据某项(cpu,vss,rss,thr)来排序
-b Batch mode (no tty)
-d 更新的时间间隔,默认是每隔3s更新下状态,top -d 0时会持续地输出,不过很占CPU。
-m 显示的最多进程数,做优化时一般只关心CPU占用率最高的几个进程,那么只需要top -m 3或top -m 5就好了,以免top命令的输出太多
-n 显示n次top的结果后命令就会退出,例如:top -n 1
-p 显示进程信息,例如:top -p [pid]
-u Show these USERs
-q Quiet (no header lines)
Cursor LEFT/RIGHT to change sort, UP/DOWN move list, space to force
update, R to reverse sort, Q to exit.
二、top -n [number]
Android N (level = 25) before and
[hwbeethoven:/ $ top -n 1
// CPU占用率:User 用户进程;System 系统进程;IOW IO等待时间;IRQ 硬中断时间
User 8%, System 9%, IOW 0%, IRQ 0%
// CPU使用情况:
// User 处于用户态的运行时间,不包含优先值为负进程
// Nice 优先值为负的进程所占用的CPU时间
// Sys 处于核心态的运行时间
// Idle 除IO等待时间以外的其它等待时间
// IOW IO等待时间
// IRQ 硬中断时间
// SIRQ 软中断时间
User 5 + Nice 0 + Sys 6 + Idle 50 + IOW 0 + IRQ 0 + SIRQ 0 = 61
// 进程属性:
// PID 进程在系统中的ID
// CPU% 当前瞬时所以使用CPU占用率
// S 进程的状态,其中S表示休眠,R表示正在运行,Z表示僵死状态,N表示该进程优先值是负数。
// #THR 程序当前所用的线程数
// VSS Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)
// RSS Resident Set Size 实际使用物理内存(包含共享库占用的内存)
// PCY Policy系统对这个进程/线程的调度策略,bg 后台;fg 前台
// Name 程序名称
PID USER PR NI CPU% S #THR VSS RSS PCY Name
21548 shell 20 0 9% R 1 9128K 1908K fg top
2 root 20 0 0% S 1 0K 0K fg kthreadd
3 root 20 0 0% S 1 0K 0K fg ksoftirqd/0
5 root 0 -20 0% S 1 0K 0K fg kworker/0:0H
7 root 20 0 0% S 1 0K 0K fg rcu_preempt
8 root 20 0 0% S 1 0K 0K fg rcu_sched
9 root 20 0 0% S 1 0K 0K fg rcu_bh
10 root RT 0 0% S 1 0K 0K fg migration/0
11 root RT 0 0% S 1 0K 0K fg watchdog/0
12 root RT 0 0% S 1 0K 0K fg watchdog/1
13 root RT 0 0% S 1 0K 0K fg migration/1
14 root 20 0 0% S 1 0K 0K fg ksoftirqd/1
Note: The memory footprint size has the following rules:
VSS
>=RSS
>=PSS
>=USS
VSS
Virtual Set Size of virtual memory consumption (contains shared libraries occupy memory)
RSS
Resident actually use the Set Size of physical memory (shared library that contains the memory occupied)
PSS
of physical memory Proportional Set Size actually used (prorated shared libraries occupy memory)
USS
of Unique the Set Size process alone take up physical memory (does not include shared libraries occupy memory)
Android O (level = 26) and after
[HWHRY-HF:/ $ top -n 1
// 进程数(Tasks)
Tasks: 589 total, 1 running, 546 sleeping, 0 stopped, 0 zombie
// 内存使用信息
Mem: 5859784k total, 5741324k used, 118460k free, 5520k buffers
// 交换区使用信息
Swap: 2293756k total, 696260k used, 1597496k free, 2407216k cached
// cpu使用信息
800%cpu 83%user 3%nice 73%sys 630%idle 0%iow 7%irq 3%sirq 0%host
// PID 进程在系统中的ID
// VIRT virtual memory usage 虚拟内存
// 1、进程“需要的”虚拟内存大小,包括进程使用的库、代码、数据等
// 2、假如进程申请100m的内存,但实际只使用了10m,那么它会增长100m,而不是实际的使用量
// RES resident memory usage 常驻内存
// 1、进程当前使用的内存大小,但不包括swap out
// 2、包含其他进程的共享
// 3、如果申请100m的内存,实际使用10m,它只增长10m,与VIRT相反
// 4、关于库占用内存的情况,它只统计加载的库文件所占内存大小
// SHR shared memory 共享内存
// 1、除了自身进程的共享内存,也包括其他进程的共享内存
// 2、虽然进程只使用了几个共享库的函数,但它包含了整个共享库的大小
// 3、计算某个进程所占的物理内存大小公式:RES – SHR
// 4、swap out后,它将会降下来
// S 进程的状态,其中S表示休眠,R表示正在运行,Z表示僵死状态,N表示该进程优先值是负数。
// [%CPU] 当前瞬时所以使用CPU占用率
// %MEM 表示内存使用百分比
// ARGS 程序名称
PID USER PR NI VIRT RES SHR S[%CPU] %MEM TIME+ ARGS
13613 u0_a304 10 -10 4.3G 82M 52M S 46.6 1.4 0:11.43 com.cloudwise.ndk
18873 shell 20 0 12M 2.4M 1.6M R 33.3 0.0 0:00.05 top -n 1
601 system -2 -8 2.4G 18M 8.3M S 23.3 0.3 66:49.50 surfaceflinger
583 system 20 0 22M 2.7M 1.9M S 13.3 0.0 16:35.51 vendor.huawei.hardware.hwdisplay.displayengine@1.2-service
569 system -3 -8 2.1G 3.8M 3.1M S 13.3 0.0 25:59.26 android.hardware.graphics.composer@2.2-service
1370 system 18 -2 5.1G 199M 199M S 10.0 3.4 274:28.21 system_server
662 root RT 0 28M 2.8M 2.1M S 10.0 0.0 16:37.07 aptouch_daemon
18760 shell 20 0 20M 732K 732K S 6.6 0.0 0:00.94 adbd --root_seclabel=u:r:su:s0
18857 shell 20 0 12M 2.4M 1.5M S 3.3 0.0 0:00.34 logcat -v long
18562 root 20 0 0 0 0 S 3.3 0.0 0:00.17 [kworker/u16:3]
If you want to look at information on a process, then run the following command:
top -n 1 -p [pid]
Note: 该命令只适用于Android O(level = 26) 及之后的系统
, Level = 25 the previous system did not -p
command.
Third, code implementation
private void sysInfo(){
new Thread(new Runnable() {
@Override
public void run() {
try {
// 查询某一个进程的信息
List<String> cmds = new ArrayList<String>();
cmds.add("top");
cmds.add("-n");
cmds.add("1");
if(Build.VERSION.SDK_INT >= 26){
cmds.add("-p");
cmds.add(android.os.Process.myPid()+"");
}
ProcessBuilder pb = new ProcessBuilder(cmds);
Process p = pb.start();
//Process p = Runtime.getRuntime().exec("top -n 1 -p " + android.os.Process.myPid());
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null){
sb.append(line).append("\n");
}
Log.e("CLOUDWISE","Sys : " + sb.toString());
} catch (Exception ex){
ex.printStackTrace();
Log.e("CLOUDWISE","Exception : " + ex.toString());
}
}
}).start();
}
Output Log
Android O (level = 26) and after the log:
[?25l[0m[H[J[s[999C[999B[6n[uTasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
Mem: 5859784k total, 5671728k used, 188056k free, 4292k buffers
Swap: 2293756k total, 732988k used, 1560768k free, 2288768k cached
800%cpu 0%user 0%nice 0%sys 800%idle 0%iow 0%irq 0%sirq 0%host
[7m PID USER PR NI VIRT RES SHR S[%CPU] %MEM TIME+ ARGS [0m
23669 u0_a309 10 -10 4.2G 90M 56M S 4.0 1.5 0:02.58 com.tsb
[?25h[0m[1000;1H[K[?25h[0m[1000;1H[K
Android N (level = 25) and before the log:
User 0%, System 0%, IOW 0%, IRQ 0%
User 0 + Nice 0 + Sys 0 + Idle 1 + IOW 0 + IRQ 0 + SIRQ 0 = 1
PID USER PR NI CPU% S #THR VSS RSS PCY Name
7975 u0_a276 20 0 0% R 1 9128K 1656K ta top
7702 u0_a276 20 0 0% S 38 2373700K 109416K ta com.tsb