Android启动速度(cold warm hot)

官方文档详细介绍了cold start, warm start 和 hot start 3种情况下app启动优化 下边是文档地址

https://developer.android.com/topic/performance/vitals/launch-time

一:首先解释下这几个启动状态的场景

1,cold start :应用完全从头启动

2,warm start:

1)用户在主actiivty点击了back键退出了应用(没有调用Precess。kill(myPid))这时的app进程还活着,用户此时又点击了桌面的启动图标,app进程不需要重新启动 但是actiivty需要重新创建

2)由于系统内存紧张 app进程被杀了 但是系统的back stack还保留着活动 此时用户启动app 

3,hot start:进程存在activity也没有销毁(按下home键)此时再回到app 还有情况就是系统内存紧张调用了activity的onTrimMemory(), 然后activity需要重新创建view 并实现渲染绘制同时还需要实例化业务对象

二:为了达到优化app启动速度的目的,我们需要理解一下app启动的过程 

但是我现在描述的过程不像网上大家看到的深入到内核和framework的源码角度来叙述二是基本简述一下启动的流程顺序

Cold start 发生在手机开机后第一次启动app或者是杀掉app进程后再次启动,这个期间如果处理不好会极大的影响app的启动速度和体验,因为这个阶段系统和app加载初始化的资源比其他任何状态下都更多也就意味着消耗更多时间。

在cold start最开始阶段 系统有3个任务 1:加载和启动app。2:启动app后立即显示一个空白的window窗口。3:创建app进程

创建app进程阶段 app进程会有以下几个步骤 1:创建app对象(application)。2:启动主线程。3:创建主activity。4:inflate Views。5:布局屏幕(status bar navigation bar 我们的contentView)。6:开始绘制。一旦app进程完成了第一帧的绘制 系统进程会把绘制好的actiivty把之前空白的window替换掉,这时用户就可以使用app进行交互了。

Figure 1 shows how the system and app processes hand off work between each other.

Figure 1. A visual representation of the important parts of a cold application launch.

Performance issues can arise during creation of the app and creation of the activity.

性能问题会发生在app的create和actiivty的create阶段 也就是application的onCreate()(如果我们重写了这个方法的话)和activity的onCreate()

Activity creation activity的创建

app进程创建了我们的activity,activity做了以下的一些操作

Initializes values.

Calls constructors.

Calls the callback method, such as Activity.onCreate(), appropriate to the current lifecycle state of the activity.

Typically, the onCreate() method has the greatest impact on load time, because it performs the work with the highest overhead: loading and inflating views, and initializing the objects needed for the activity to run.最关键的是这句话 activity的onCreate()方法执行了非常大的一些操作例如inflate Views 初始化activity里的对象,所以我们一定不要把业务逻辑放在这个生命周期进行。

怎么检测app启动各个阶段的加载时间呢? 谷歌搞出了google concole

Android vitals 这是一个工具

Android vitals can help improve your app's performance by alerting you, via the Play Console, when your app's startup times are excessive. Android vitals considers your app's startup times excessive when the app's:

Cold startup takes 5 seconds or longer.

Warm startup takes 2 seconds or longer.

Hot startup takes 1.5 seconds or longer.

daily session refers to a day in which your app was used.

Android vitals doesn't report data for hot startups. For information on how Google Play collects Android vitals data, see the Play Console documentation.

但是我们可以在logcat中查看log来分析app启动所消耗的时间  只需要连接上设备,logcat上输入过滤字符串Displayed 然后启动应用就可以观察到了 下边是我的截图


Displayed

你可能感兴趣的:(Android启动速度(cold warm hot))