Android执行shell命令
一、方法

1. /**
2. * 执行一个shell命令,并返回字符串值
3. *
4. * @param cmd
5. * 命令名称&参数组成的数组(例如:{"/system/bin/cat", "/proc/version"})
6. * @param workdirectory
7. * 命令执行路径(例如:"system/bin/")
8. * @return 执行结果组成的字符串
9. * @throws IOException
10. */
11. public static synchronized String run(String[] cmd, String workdirectory)
12. throws IOException {
13. StringBuffer result = new StringBuffer();
14. try {
15. // 创建操作系统进程(也可以由Runtime.exec()启动)
16. // Runtime runtime = Runtime.getRuntime();
17. // Process proc = runtime.exec(cmd);
18. // InputStream inputstream = proc.getInputStream();
19. ProcessBuilder builder = new ProcessBuilder(cmd);
20.
21. InputStream in = null;
22. // 设置一个路径(绝对路径了就不一定需要)
23. if (workdirectory != null) {
24. // 设置工作目录(同上)
25. builder.directory(new File(workdirectory));
26. // 合并标准错误和标准输出
27. builder.redirectErrorStream(true);
28. // 启动一个新进程
29. Process process = builder.start();
30.
31. // 读取进程标准输出流
32. in = process.getInputStream();
33. byte[] re = new byte[1024];
34. while (in.read(re) != -1) {
35. result = result.append(new String(re));
36. }
37. }
38. // 关闭输入流
39. if (in != null) {
40. in.close();
41. }
42. } catch (Exception ex) {
43. ex.printStackTrace();
44. }
45. return result.toString();
46. }


二、用途
执行Linux下的top、ps等命令,这些命令你也通过adb可以执行查看效果。
1)top命令如下:

1. adb shell
2. $ top -h
3. top -h
4. Usage: top [-m max_procs] [-n iterations] [-d delay] [-s sort_column] [-t] [-h]
5. -m num Maximum number of processes to display. // 最多显示多少个进程
6. -n num Updates to show before exiting. // 刷新次数
7. -d num Seconds to wait between updates. // 刷新间隔时间(默认5秒)
8. -s col Column to sort by <cpu,vss,rss,thr> // 按哪列排序
9. -t Show threads instead of processes. // 显示线程信息而不是进程
10. -h Display this help screen. // 显示帮助文档
11. $ top -n 1
12. top -n 1


就不把执行效果放上来了,总之结果表述如下:

1. User 35%, System 13%, IOW 0%, IRQ 0% // CPU占用率
2. User 109 + Nice 0 + Sys 40 + Idle 156 + IOW 0 + IRQ 0 + SIRQ 1 = 306 // CPU使用情况
3.
4. PID CPU% S #THR VSS RSS PCY UID Name // 进程属性
5. xx xx% x xx xx xx xx xx xx
6.
7. CPU占用率:
8. User 用户进程
9. System 系统进程
10. IOW IO等待时间
11. IRQ 硬中断时间
12.
13. CPU使用情况(指一个最小时间片内所占时间,单位jiffies。或者指所占进程数):
14. User 处于用户态的运行时间,不包含优先值为负进程
15. Nice 优先值为负的进程所占用的CPU时间
16. Sys 处于核心态的运行时间
17. Idle 除IO等待时间以外的其它等待时间
18. IOW IO等待时间
19. IRQ 硬中断时间
20. SIRQ 软中断时间
21.
22. 进程属性:
23. PID 进程在系统中的ID
24. CPU% 当前瞬时所以使用CPU占用率
25. S 进程的状态,其中S表示休眠,R表示正在运行,Z表示僵死状态,N表示该进程优先值是负数。
26. #THR 程序当前所用的线程数
27. VSS Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)
28. RSS Resident Set Size 实际使用物理内存(包含共享库占用的内存)
29. PCY OOXX,不知道什么东东
30. UID 运行当前进程的用户id
31. Name 程序名称android.process.media
32.
33. // ps:内存占用大小有如下规律:VSS >= RSS >= PSS >= USS
34. // PSS Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存)
35. // USS Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存)

在附件Android系统->android top.txt文件内,自个总结的。
2)执行代码

1. // top命令
2. public static final String[] TOP = { "/system/bin/top", "-n", "1" };
3.
4. // 现在执行top -n 1,我们只需要第二行(用第二行求得CPU占用率,精确数据)
5. // 第一行:User 35%, System 13%, IOW 0%, IRQ 0% // CPU占用率
6. // 第二行:User 109 + Nice 0 + Sys 40 + Idle 156 + IOW 0 + IRQ 0 + SIRQ 1 = 306
7. // // CPU使用情况
8. public static synchronized String run(String[] cmd) {
9. String line = "";
10. InputStream is = null;
11. try {
12. Runtime runtime = Runtime.getRuntime();
13. Process proc = runtime.exec(cmd);
14. is = proc.getInputStream();
15.
16. // 换成BufferedReader
17. BufferedReader buf = new BufferedReader(new InputStreamReader(is));
18. do {
19. line = buf.readLine();
20. // 前面有几个空行
21. if (line.startsWith("User")) {
22. // 读到第一行时,我们再读取下一行
23. line = buf.readLine();
24. break;
25. }
26. } while (true);
27.
28. if (is != null) {
29. buf.close();
30. is.close();
31. }
32. } catch (IOException e) {
33. e.printStackTrace();
34. }
35. return line;
36. }
37.
38. // 获取指定应用的top命令获取的信息
39. // PID CPU% S #THR VSS RSS PCY UID Name // 进程属性
40. // 如果当前应用不在运行则返回null
41. public static synchronized String run(String[] cmd, String pkgName) {
42. String line = null;
43. InputStream is = null;
44. try {
45. Runtime runtime = Runtime.getRuntime();
46. Process proc = runtime.exec(cmd);
47. is = proc.getInputStream();
48.
49. // 换成BufferedReader
50. BufferedReader buf = new BufferedReader(new InputStreamReader(is));
51. do {
52. line = buf.readLine();
53. // 读取到相应pkgName跳出循环(或者未找到)
54. if (null == line || line.endsWith(pkgName)) {
55. break;
56. }
57. } while (true);
58.
59. if (is != null) {
60. buf.close();
61. is.close();
62. }
63. } catch (IOException e) {
64. e.printStackTrace();
65. }
66. return line;
67. }

--------------------------------------------
PID:进程在系统中的ID

CPU% - 当前瞬时所以使用CPU占用率

#THR - 程序当前所用的线程数

UID - 运行当前进程的用户id

Name - 程序名称org.xmpp.app

VSS - Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)
RSS - Resident Set Size 实际使用物理内存(包含共享库占用的内存)
PSS - Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存)
USS - Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存)

一般来说内存占用大小有如下规律:VSS >= RSS >= PSS >= USS

更多相关文章

  1. 使用valgrind检测Android native程序的内存
  2. Android最佳实践之Notification、下拉刷新、内存及性能建议等
  3. Android bitmap内存优化测试
  4. Android通过共享用户ID来实现多Activity进程共享
  5. Android仿iPhone的日期时间选择器
  6. Android 自定义日期和时间和星期的弹窗
  7. [置顶] android 内存泄露那些事情之Handler(一)
  8. Android之进程查看,关闭(可操作所有进程)
  9. Android InputMethodManager 导致的内存泄露及解决方案

随机推荐

  1. Android版本与Linux内核版本的关系
  2. android全平台编译ffmpeg合并为单个库实
  3. 区分android:gravity与android:layout_gr
  4. Android开发规范
  5. Android(安卓)安全测试书单
  6. Android加密之文件级加密
  7. Android图像篇
  8. You are attempting to build with the i
  9. 各种类型Android(安卓)Market了解
  10. Android(安卓)Button应用法则