Android 性能优化 冷启动速度优化

来源:互联网 发布:数控螺纹编程 编辑:程序博客网 时间:2024/06/02 04:29

前言

本篇文章对app启动速度进行优化。先了解 Android 性能优化 基本概念应用是如何启动的,会对此有帮助。

1. 应用的启动模式

  • 冷启动 Cold start
    当启动应用时,后台没有该应用的进程,这时系统会重新创建一个新的进程分配给该应用,这个启动方式就是冷启动。
    冷启动因为系统会重新创建一个新的进程分配给它,所以会先创建和初始化Application类,再创建和初始化MainActivity类(包括一系列的测量、布局、绘制),最后显示在界面上。
    此时会初始化Application,Application没有初始化完不会向下走。

  • 热启动Warm start
    当启动应用时,后台已有该应用的进程(例:按back键、home键,应用虽然会退出,但是该应用的进程是依然会保留在后台,可进入任务列表查看),所以在已有进程的情况下,这种启动会从已有的进程中来启动应用,这个方式叫热启动。
    热启动因为会从已有的进程中来启动,所以热启动就不会走Application这步了,而是直接走LauncherActivity(包括一系列的测量、布局、绘制),所以热启动的过程只需要创建和初始化一个MainActivity就行了,而不必创建和初始化Application,因为一个应用从新进程的创建到进程的销毁,Application只会初始化一次。

  • 温启动Lukewarm start
    A lukewarm start encompasses some subset of the operations that take place during a cold start; at the same time, it represents less overhead than a warm start. 
    温启动包括在冷启动期间发生的操作的一些子集;同时,它表示比热启动少的开销。有许多潜在的状态可以被认为是温暖的开始。

  • Although, application launching is blazingly fast, Android still require some time to load some data from your application (classes, resources, etc). In order to avoid hiccups and visually respond as soon as possible to the user interaction, the system displays a temporary window: the “starting window” also known as the “preview window”.
    *A common way to implement a themed launch screen is to use the windowDisablePreview
     theme attribute to turn off the initial blank screen that the system process draws when launching the app. However, this approach can result in a longer startup time than apps that don’t suppress the preview window. Also, it forces the user to wait with no feedback while the activity launches, making them wonder if the app is functioning properly.*
    在Launcher上点击应用启动时会展示一个preview window,一般根据Theme为黑色或白色,也可以在Theme中设置属性windowDisablePreview为true来禁止显示该preview window,但这样会让用户困惑,到底我点了没有??

在使用中大多数应用都有一个SplashActivity来形成视觉连贯性。这里就不赘述了。可查看 带你重新认识:Android Splash页秒开 Activity白屏 Activity黑屏。所以下面的优化也都是建立在已经使用了SplashActivity后依然停留过长时间。

2. 应用启动时间测量

2.1 测量启动时间

使用命令adb shell am start -W [packageName]/[packageName.MainActivity]可以查看冷、热启动时间。

  • WaitTime 就是总的耗时,包括前一个应用 Activity pause 的时间和新应用启动的时间;
  • ThisTime 表示一连串启动 Activity 的最后一个 Activity 的启动耗时;
  • TotalTime 表示新应用启动的耗时,包括新进程的启动和 Activity 的启动,但不包括前

做启动优化时主要优化冷启动,尽量使冷启动占用的时间变少,其中冷启动包括如下几个步骤:
1. Creating the app object.
2. Launching the main thread.
3. Creating the main activity.
4. Inflating views.
5. Laying out the screen.
6. Performing the initial draw.

所以要尽量保证在Application和LauncherActivity中尽量少操作,尽量不耗时。

2.2 测量Activity启动时间

官方文档:Profiling Launch Performance 中有介绍,使用Activity.reportFullyDrawn在Logcat中打印出来。

No Filters模式下搜索关键词Displayed便可以查看,如下所示:
ActivityManager: Displayed com.android.myexample/.StartupTiming: +3s534ms

2.3 使用TraceView精确测量

在Application代码中,添加如下代码进行精确测量:

// start tracing to "/sdcard/calc.trace"Debug.startMethodTracing("calc");// ...// stop tracingDebug.stopMethodTracing();

在DDMS中打开文件后,可以精确查看每一个方法的消耗时间,按照Real Time/Call排序,然后进行优化。

2.4 可以优化之处

知道了启动流程,我们也主要优化这个流程中我们能优化的地方:Application.attachBaseContext()Application.onCreate()LauncherActivity.onCreate()中进行的操作和布局复杂度。

知道了如何测量时间,便提供了一个标准,我们就在优化时有个标准,当然时间越少越好。

3. 如何优化

  • 在Application和LauncherActivity中打印出.trace文件,分析耗时操作。
  • 业务允许时将耗时操作放入子线程中初始化。

可查看Android App优化之提升你的App启动速度之实例挑战该示例,提供了一个很少的思路。

结语

启动速度的优化有可依循的步骤,方法很多,参考资料中有可以用多进程去优化,慢慢来吧,不断学习。




官方文档:Launch-Time Performance

  • 测量时间
    测量Activity 的启动时间
     Android性能优化之加快应用启动速度
    Android 中如何计算 App 的启动时间?

  • splash
     带你重新认识:Android Splash页秒开 Activity白屏 Activity黑屏

  • 优化:
    Android性能优化典范 - 第6季
    Android App优化之提升你的App启动速度之实例挑战
    Android 启动速度优化-总会遇到的不痛不痒的坎~
    提高NYTimes的启动速度
    Android应用快速启动设计
    Google Play榜单APP冷启动速度分析报告
0 0
原创粉丝点击