您的位置:首页 > 移动开发 > Android开发

Android性能相关常用命令收集

2017-08-02 15:03 399 查看

Android性能相关常用命令收集

1.CUP

(1)adb shell (进入linux的底层)

(2)echo 3>/proc/sys/vm/drop_caches (清除一下系统cache)

(3)top -d 1 | grep com.baidu.BaiduMap (以百度地图为例,每一秒打印一次资源利用情况)
  top -d 1  //查看所有的

2.Mem

  (1) 查看属性

adb shellgetprop|grep heapgrowthlimit//查看单个应用程序最大内存限制 [dalvik.vm.heapgrowthlimit]
adb shell getprop|grep dalvik.vm.heapstartsize//应用启动后分配的初始内存 [dalvik.vm.heapstartsize]
adb shell getprop|grep dalvik.vm.heapsize//单个java虚拟机最大的内存限制 [dalvik.vm.heapsize]

  (2) 查看程序的内存使用情况://仅仅PSS

adb shelldumpsys meminfo//全部,但是没有下面的命令详细
adb shell dumpsys meminfo $package_name or $pid   //使用程序的包名或者进程id

  (3) 查看USS和PSS//手机需要root

adb shellprocrank//...|grep
YOUR_APP
adb shell dumpsys meminfo//只是查看PSS

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

【解释】
--CPU占用率: 
User       用户进程 
System  系统进程 
IOW       IO等待时间 
IRQ        硬中断时间 

--CPU使用情况(指一个最小时间片内所占时间,单位jiffies。或者指所占进程数): 
User    处于用户态的运行时间,不包含优先值为负进程 
Nice    优先值为负的进程所占用的CPU时间 
Sys     处于核心态的运行时间 
Idle      除IO等待时间以外的其它等待时间 
IOW    IO等待时间 
IRQ     硬中断时间 
SIRQ   软中断时间 

--进程属性: 
PID        进程在系统中的ID 
CPU%   当前瞬时所以使用CPU占用率 
S           进程的状态,其中S表示休眠,R表示正在运行,Z表示僵死状态,N表示该进程优先值是负数。 
#THR    程序当前所用的线程数 
VSS      Virtual Set Size 虚拟耗用内存(包含共享库占用的内存) 
RSS      Resident Set Size 实际使用物理内存(包含共享库占用的内存) 
PCY      OOXX,不知道什么东东 
UID       运行当前进程的用户id 
Name   程序名称android.process.media 

--ps:内存占用大小有如下规律:VSS >= RSS >= PSS >= USS  

VSS      Virtual Set Size 虚拟耗用内存(包含共享库占用的内存) 
RSS      Resident Set Size 实际使用物理内存(包含共享库占用的内存)

PSS     Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存)

USS     Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存)

【温馨提示】
(1)我们一般观察 Uss 来反映一个Process的内存使用情况,Uss 的大小代表了只属于本进程正在使用的内存大小,这些内存在此Process被杀掉之后,会被完整的回收掉,Vss和Rss对查看某一Process自身内存状况没有什么价值,因为他们包含了共享库的内存使用,而往往共享库的资源占用比重是很大的,这样就稀释了对Process自身创建内存波动。
而Pss是按照比例将共享内存分割,某一Process对共享内存的占用情况。
(2)在取内存数据前可以前判断一下手机是否root, 如果root了取USS比较好一些,如果没有root取PSS 也是可以的。

//********************************************************************************************************

3.Profile GPU rendering (GPU呈现模式分析)

  adb shell
dumpsys gfxinfo <PACKAGE_NAME>

  【结果中部分概念解释】

     Applications Graphics Acceleration Info:
    Uptime: 16264702 Realtime: 28169900

     1. Uptime  VS  Realtime :

      From ActivityManagerService.java:

[plain]
view plain
copy

long uptime = SystemClock.uptimeMillis();  
long realtime = SystemClock.elapsedRealtime();  
pw.println("Applications Graphics Acceleration Info:");  
pw.println("Uptime: " + uptime + " Realtime: " + realtime);  


       Also from SystemClock description:

Three different clocks are available, and they should not be confused:

(1) currentTimeMillis() 

is the standard "wall" clock (time and date) expressing milliseconds since the epoch. The wall clock can be set by the user or the phone network (see setCurrentTimeMillis(long)), so the time may jump backwards
or forwards unpredictably. This clock should only be used when correspondence with real-world dates and times is important, such as in a calendar or alarm clock application. Interval or elapsed time measurements should use a different clock. If you are using
System.currentTimeMillis(), consider listening to the ACTION_TIME_TICK, ACTION_TIME_CHANGED and ACTION_TIMEZONE_CHANGED Intent broadcasts to find out when the time changes.

(2) uptimeMillis() 

is counted in milliseconds since the system was booted. This clock stops when the system enters deep sleep (CPU off, display dark, device waiting for external input), but
is not affected by clock scaling, idle, or other power saving mechanisms. This is the basis for most interval timing such as Thread.sleep(millls), Object.wait(millis), and System.nanoTime(). This clock is guaranteed to be monotonic, and is suitable for interval
timing when the interval does not span device sleep. Most methods that accept a timestamp value currently expect the uptimeMillis() clock.

(3) elapsedRealtime() and elapsedRealtimeNanos() 

return the time since the system was booted, and include deep sleep. This clock is guaranteed to be monotonic, and continues to tick even when the CPU is in power saving modes, so is the recommend basis for
general purpose interval timing.

4.其他 dumpsys 命令

Android提供的dumpsys工具可以用于查看感兴趣的系统服务信息与状态,手机连接电脑后可以直接命令行执行adb shell dumpsys 查看所有支持的Service但是这样输出的太多,可以通过dumpsys
| grep "DUMP OF SERVICE" 仅显示主要的Service的信息


1、列出dumpsys所有支持命令

?

[plain]
view plain
copy

> adb shell   
$ dumpsys | grep "DUMP OF SERVICE"  

以下是在三星S3手机上的输出信息

[plain]
view plain
copy

C:\Windows\System32>adb shell   
shell@m:/ $ dumpsys | grep"DUMP OF SERVICE"   
dumpsys | grep"DUMP OF SERVICE"   
DUMP OF SERVICE AtCmdFwd:   
DUMP OF SERVICE CustomFrequencyManagerService:   
DUMP OF SERVICE DirEncryptService:   
DUMP OF SERVICE Exynos.HWCService:   
DUMP OF SERVICE Exynos.IPService:   
DUMP OF SERVICE FMPlayer:   
DUMP OF SERVICE LEDService:   
DUMP OF SERVICE SecTVOutService:   
DUMP OF SERVICE SurfaceFlinger:   
DUMP OF SERVICE TvoutService_C:   
DUMP OF SERVICE accessibility:   
DUMP OF SERVICE account:   
DUMP OF SERVICE activity:   
DUMP OF SERVICE alarm:   
DUMP OF SERVICE android.security.keystore:   
DUMP OF SERVICE application_policy:   
DUMP OF SERVICE appops:   
DUMP OF SERVICE appwidget:   
DUMP OF SERVICE audio:   
DUMP OF SERVICE backup:   
DUMP OF SERVICE battery:   
DUMP OF SERVICE batteryinfo:   
DUMP OF SERVICE bluetooth_manager:   
DUMP OF SERVICE bluetooth_secure_mode_manager:   
DUMP OF SERVICE clipboard:   
DUMP OF SERVICE clipboardEx:   
DUMP OF SERVICE commontime_management:   
DUMP OF SERVICE connectivity:   
DUMP OF SERVICE container_service:   
DUMP OF SERVICE content:   
DUMP OF SERVICE country_detector:   
DUMP OF SERVICE cpuinfo:   
DUMP OF SERVICE dbinfo:   
DUMP OF SERVICE device_policy:   
DUMP OF SERVICE devicestoragemonitor:   
DUMP OF SERVICE diskstats:   
DUMP OF SERVICE display:   
DUMP OF SERVICE dreams:   
DUMP OF SERVICE drm.drmManager:   
DUMP OF SERVICE dropbox:   
DUMP OF SERVICE edmnativehelper:   
DUMP OF SERVICE enterprise_license_policy:   
DUMP OF SERVICE enterprise_policy:   
DUMP OF SERVICE entropy:   
DUMP OF SERVICE gfxinfo:   
DUMP OF SERVICE hardware:   
DUMP OF SERVICE harmony_eas_service:   
DUMP OF SERVICE input:   
DUMP OF SERVICE input_method:   
DUMP OF SERVICE iphonesubinfo:   
DUMP OF SERVICE isms:   
DUMP OF SERVICE license_log_service:   
DUMP OF SERVICE location:   
DUMP OF SERVICE lock_settings:   
DUMP OF SERVICE log_manager_service:   
DUMP OF SERVICE mdm.remotedesktop:   
DUMP OF SERVICE media.audio_flinger:   
DUMP OF SERVICE media.audio_policy:   
DUMP OF SERVICE media.camera:   
DUMP OF SERVICE media.player:   
DUMP OF SERVICE meminfo:   
DUMP OF SERVICE motion_recognition:   
DUMP OF SERVICE mount:   
DUMP OF SERVICE multiwindow:   
DUMP OF SERVICE netpolicy:   
DUMP OF SERVICE netstats:   
DUMP OF SERVICE network_management:   
DUMP OF SERVICE nfc:   
DUMP OF SERVICE nfccontroller:   
DUMP OF SERVICE notification:   
DUMP OF SERVICE package:   
DUMP OF SERVICE permission:   
DUMP OF SERVICE phone:   
DUMP OF SERVICE phone_restriction_policy:   
DUMP OF SERVICE phoneext:   
DUMP OF SERVICE power:   
DUMP OF SERVICE remoteinjection:   
DUMP OF SERVICE samplingprofiler:   
DUMP OF SERVICE samsung.smartfaceservice:   
DUMP OF SERVICE scheduling_policy:   
DUMP OF SERVICE search:   
DUMP OF SERVICE sec_analytics:   
DUMP OF SERVICE secontroller:   
DUMP OF SERVICE sensorservice:   
DUMP OF SERVICE serial:   
DUMP OF SERVICE servicediscovery:   
DUMP OF SERVICE simphonebook:   
DUMP OF SERVICE sip:   
DUMP OF SERVICE statusbar:   
DUMP OF SERVICE telephony.registry:   
DUMP OF SERVICE textservices:   
DUMP OF SERVICE tvoutservice:   
DUMP OF SERVICE uimode:   
DUMP OF SERVICE updatelock:   
DUMP OF SERVICE usagestats:   
DUMP OF SERVICE usb:   
DUMP OF SERVICE user:   
DUMP OF SERVICE vibrator:   
DUMP OF SERVICE voip:   
DUMP OF SERVICE wallpaper:   
DUMP OF SERVICE wfd:   
DUMP OF SERVICE wifi:   
DUMP OF SERVICE wifi_policy:   
DUMP OF SERVICE wifip2p:   
DUMP OF SERVICE window:  

?

2、具体命令如何查看帮助

从上面可以看出Service非常多,“DUMP OF SERVICE”关键字后面的单词都可以直接通过 dumpsys + 单词 查看相关信息,具体每一个如何使用有一种通用的查看帮助的办法。

查看每一个命令的使用帮助,以下以meminfo 为例演示:

?

[plain]
view plain
copy

shell@m:/ $ dumpsys meminfo -h   
dumpsys meminfo -h   
meminfo dump options: [-a] [--oom] [process]   
  -a: include all available information foreach process.   
  --oom: only show processes organized by oom adj.   
If [process] is specified it can be the name or   
pid of a specific process to dump.  


3、一些关键命令解释

名字功能
account 显示accounts信息
activity 显示所有的activities的信息
cpuinfo显示CPU信息
window显示键盘,窗口和它们的关系
wifi 显示wifi信息
batteryinfo $package_name电量信息及CPU 使用时长
package packagename获取安装包信息
usagestats每个界面启动的时间
statusbar显示状态栏相关的信息
meminfo内存信息(meminfo $package_name or $pid 
使用程序的包名或者进程id显示内存信息)
diskstats 磁盘相关信息
battery 电池信息
alarm显示Alarm信息
  
activity  - 使用adb
shell dumpsys检测Android的Activity任务栈
window - 通过adb
shell dumpsys命令获取当前应用的component

statusbar - 找出广告通知属于哪个应用
    dumpsys statusbar | grep notification=Notification 



5.参考资料

Dumpsys (官方文档)
How
to discover memory usage of my application in Android
android中dumpsys函数介绍与使用 (代码分析)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: