Android性能分析与优化

性能指标

  • 流畅更快 卡顿,启动速度,页面显示速度,响应速度
  • 稳定更稳 Crash, ANR
  • 节省更省 内存,CPU,安装包大小,存储,功耗电量,网络

https://blog.csdn.net/yanbober/article/details/48394201
https://github.com/openthos/openthos/wiki/understand-android
https://www.kancloud.cn/kancloud/android-performance/53238
https://developer.android.com/topic/performance/index.html
https://developer.android.com/studio/profile/index.html

流畅性

启动速度

https://developer.android.com/topic/performance/launch-time.html
https://www.jianshu.com/p/f5514b1a826c
https://juejin.im/post/5874bff0128fe1006b443fa0

启动加速之Avoid Heavy App Initialization

项目中除听云之外其余所有三方组件都抢占先机,在Application主线程初始化。这样的初始化方式肯定是过重的:

考虑异步初始化三方组件,不阻塞主线程;
延迟部分三方组件的初始化;实际上我们粗粒度的把所有三方组件都放到异步任务里,可能会出现WorkThread中尚未初始化完毕但MainThread中已经使用的错误,因此这种情况建议延迟到使用前再去初始化;
而如何开启WorkThread同样也有讲究,这个话题在下文详谈。
项目修改:
将友盟、Bugly、听云、GrowingIO、BlockCanary等组件放在WorkThread中初始化;
延迟地图定位、ImageLoader、自有统计等组件的初始化:地图及自有统计延迟4秒,此时应用已经打开;而ImageLoader
因为调用关系不能异步以及过久延迟,初始化从Application延迟到SplashActivity;而EventBus因为再Activity中使用所以必须在Application中初始化。

Method Tracing或者DDMS


通过对traceview的详细跟踪以及代码的详细比对,我发现卡顿发生在:
部分数据库及IO的操作发生在首屏Activity主线程;
Application中创建了线程池;
首屏Activity网络请求密集;
工作线程使用未设置优先级;
信息未缓存,重复获取同样信息;
流程问题:例如闪屏图每次下载,当次使用;

通用应用启动加速套路?
利用主题快速显示界面;
异步初始化组件;
梳理业务逻辑,延迟初始化组件、操作;
正确使用线程;
去掉无用代码、重复逻辑等。

项目修改:

  1. 数据库及IO操作都移到工作线程,并且设置线程优先级为THREAD_PRIORITY_BACKGROUND,这样工作线程最多能获取到10%的时间片,优先保证主线程执行。

  2. 流程梳理,延后执行;
    实际上,这一步对项目启动加速最有效果。通过流程梳理发现部分流程调用时机偏早、失误等,例如:

更新等操作无需在首屏尚未展示就调用,造成资源竞争;
调用了IOS为了规避审核而做的开关,造成网络请求密集;
自有统计在Application的调用里创建数量固定为5的线程池,造成资源竞争,在上图traceview功能说明图中最后一行可以看到编号12执行5次,耗时排名前列;此处线程池的创建是必要但可以延后的。
修改广告闪屏逻辑为下次生效。
3.其它优化;

去掉无用但被执行的老代码;
去掉开发阶段使用但线上被执行的代码;
去掉重复逻辑执行代码;
去掉调用三方SDK里或者Demo里的多余代码;
信息缓存,常用信息只在第一次获取,之后从缓存中取;
项目是多进程架构,只在主进程执行Application的onCreate()

页面响应速度优化

https://www.zhihu.com/question/47702122
http://www.cnblogs.com/lzl-sml/p/5223704.html

UI渲染显示速度提升

https://blog.csdn.net/yanbober/article/details/48394201

稳定性

  • 稳定性问题 http://gityuan.com/2016/06/19/stability_summary/
  • ANR分析策略 http://rayleeya.iteye.com/blog/1955652
  • anr google https://developer.android.com/topic/performance/vitals/anr.html#fix_the_problems
  • ANR超时源码解析 http://gityuan.com/2016/07/02/android-anr/
  • ANR信息收集 http://gityuan.com/2016/12/02/app-not-response/
  • ANR input http://gityuan.com/2017/01/01/input-anr/
  • app crash http://gityuan.com/2016/06/24/app-crash/
  • native crash http://gityu an.com/2016/06/25/android-native-crash/

稳定性分析及工具

  • Android Profile 工具集 https://developer.android.com/studio/profile/index.html

  • Device Monitor工具集 https://developer.android.com/studio/profile/monitor.html#usage

  • 性能优化 http://rayleeya.iteye.com/blog/1961005

  • traceview
    http://gityuan.com/2017/07/11/android_debug/
    https://bxbxbai.github.io/2014/10/25/use-trace-view/
    https://developer.android.com/studio/profile/traceview.html#overview
    https://blog.csdn.net/innost/article/details/9008691
    https://blog.csdn.net/u011240877/article/details/54347396#2%E4%BD%BF%E7%94%A8-android-studio-%E7%94%9F%E6%88%90-trace-%E6%96%87%E4%BB%B6

  • systrace
    http://gityuan.com/2016/01/17/systrace/
    https://developer.android.com/studio/command-line/systrace.html

  • dumpsys
    http://gityuan.com/2015/08/22/tool-dumpsys/
    https://developer.android.com/studio/command-line/dumpsys.html

  • dmtracedump
    https://developer.android.com/studio/command-line/dmtracedump.html

Android Device Monitor

Android Device Monitor is a standalone tool that provides a UI for several Android app debugging and analysis tools.However, most components of the Android Device Monitor are deprecated in favor of updated tools available in Android Studio 3.0 and higher. The table below helps you decide which developer tools you should use.

Android Device Monitor component What you should use

  • Dalvik Debug Monitor Server (DDMS)
    This tool is deprecated. Instead, use Android Profiler in Android Studio 3.0 and higher to profile your app's CPU, memory, and network usage.
    If you want to perform other debugging tasks, such as sending commands to a connected device to set up port-forwarding, transfer files, or take screenshots, then use the Android Debug Bridge (adb), Android Emulator, Device File Explorer, or Debugger window.

  • Traceview
    If you want to inspect existing .trace files, or ones you've captured by instrumenting your app with the Debug class, keep using Traceview.
    If you want to record new method traces and inspect realtime CPU usage of your app's processes, use Android Studio's CPU profiler.

  • Systrace
    If you need to inspect native system processes and address UI jank caused by dropped frames, use systrace from the command line.
    Otherwise, use Android Studio's CPU profiler to profile your app's processes.

  • Tracer for OpenGL ES Use the Graphics API Debugger.

  • Hierarchy Viewer
    If you want to inspect your app's view hierarchy at runtime, use Layout Inspector.

If you want to profile the rendering speed of your app's layout, use Window.OnFrameMetricsAvailableListener as described in this blog post.

Pixel Perfect Use Layout Inspector.

ANR分析套路
http://maoao530.github.io/2017/02/21/anr-analyse/
https://blog.csdn.net/sinat_34157462/article/details/78651870
https://blog.csdn.net/wei_lei/article/details/70311702
http://www.cnblogs.com/purediy/p/3225060.html

crash - 内存泄露OutOfMemoryError

https://www.jianshu.com/u/b8dad3885e05
http://rayleeya.iteye.com/blog/1956059
http://www.voidcn.com/article/p-txoxuyet-bqt.html
https://juejin.im/entry/59f7ea06f265da43143ffee4
http://blog.csdn.net/qyf2010qyf/article/details/52852000

节省性

减少apk安装包大小

https://developer.android.com/topic/performance/reduce-apk-size.html#apk-structure
避免使用枚举
https://zhuanlan.zhihu.com/p/25865835
http://www.10tiao.com/html/330/201704/2653578978/2.html
https://blog.csdn.net/hp910315/article/details/48975655
https://blog.csdn.net/my_truelove/article/details/70519234

减少过渡后台,android后台优化

https://developer.android.com/training/best-background.html

减少view绘制过度

https://blog.csdn.net/qq_19711823/article/details/65627790
https://www.jianshu.com/p/2cc6d5842986
https://blog.csdn.net/yanbober/article/details/48394201
https://yq.aliyun.com/articles/82572

减少流量消耗

https://developer.android.com/topic/performance/vitals/bg-network-usage.html#detect_the_problem
https://developer.android.com/training/basics/network-ops/data-saver.html#monitor-changes

减少电量消耗

https://developer.android.com/topic/performance/power/index.html

减少内存占用

https://developer.android.com/topic/performance/memory.html#code
https://developer.android.com/topic/performance/memory-overview.html

性能优化的建议

https://blog.csdn.net/carson_ho/article/details/79708444
https://juejin.im/post/5a0d30e151882546d71ee49e
https://www.ctolib.com/docs/sfile/notes-master/Android-Java/AndroidPerformancePatterns.html
https://github.com/Piasy/notes/blob/master/Android-Java/AndroidPerformancePatterns.md
https://www.jianshu.com/u/fdb392adfbed
https://segmentfault.com/a/1190000012413613
https://zhuanlan.zhihu.com/p/26364608

Android优化checklist

你可能感兴趣的:(Android性能分析与优化)