android app patterns,App Launch time 101 (Android Performance Patterns Season 6 Ep. 1)

You know what this timer means? Every quarter second a user spends staring at a blank screen instead of interfacing with your app is a quarter second more. they're willing to close your app and give their attention to something else. misunderstanding all the complex things that happen during your app startup can lead to some serious performance problems.

Now,see, Android is pretty smart when it comes to understanding human performance perception. As soon as the user launches your application,Android will immediately display a start window,which will stay around untill your application is fully loaded,initialized,and can draw its first frame.

--你知道这个计时器是什么意思吗?【这个不知道怎么翻译,大概意思就是说如果启动时的白屏界面时间过长】。他们会希望把你的app关了,然后去做别的事情。在app启动过程中的那些复杂错误的操作会导致一些严重的性能问题。

现在我们了解了,android其实是很聪明的,它能够明白人类对性能的感知。一旦用户点击桌面图标,android会立即展示启动窗口,这个窗口将持续,直到你的应用完全加载,初始化,绘制首帧。

eaafef34e50e5a0b3e749ba48aaa0646.png

This behavior is most often seen when your app is booted for the first time,but it can easily happen other times as well,like when the activity is brought to the foreground,or after the user backs out of your app,or after some portion of your app has been purged by the system to save memory.Basically,any time the user moves from something else to your application, there's a chance you can see this type of behavior.

--这种情况在在app冷启动时候很常见,但是也很容易发生在其他时候,比如活动被置于前台,或者用于退出你的app,或者你的app的某些部分被系统清楚用于节省内存。基本上,任何时候,用户向你的应用转移一些东西(应该是指调用你的app),你也有机会看到这种情况。****套用知乎:cold start:应用第一次启动。   warm start:

18e7571124ed08a2464f80c257b924c5.png

The important point here is this.Letting the user spend too much time looking at the start window gives then ample opportunity to get bored and move on to other things.And taking too long in general could even cause the Application Not Responding dialog to pop

up.Neither of these are very good for user retention.

--真正重要的点是这个。让用户花太长时间在启动界面,会让他们感到很枯燥,然后转去做别的事情。并且花太长的时间在这上面,甚至会导致应用弹出无响应弹窗(NAR)。这些对于挽留用户都是没有好处的。

b9a80f881bcad318be9ec20755c97b2f.png

So from the technical side,the whole process works something like this.when the user launches your app, the system does a bit of work to load your application information and create a unique process for your app.

--所以,从技术方面,全部的进程工作情况,就如图下图。当用户启动你的app,系统会做一点小工作,加载你的应用信息,给你的app创建一个独立的进程。

13d79802a851ac5b2e01d38ab528316b.png

From there,the system will display the strating window and basically hang out until the application is up and running.

--从那以后,系统会显示启动界面并且会持续到应用启动和运行。

463fc6a96e371cbbcd337487bfa8dd91.png

Meanwhile,the application process will create the application object and launch the main thread.

--同时,应用进程会创建应用对象,启动主线程。

717d5f81a4b98de71cb801cf81945656.png

This is where your startup activity will be initalized,created,inflated,and finally drawn.

--主线程完成activity的初始化,创建,膨胀,绘制。

61bb8e3da8c60f64e1950c3582b2ac32.png

it's only at this point,after the application has drawn its first frame,that the system process then goes and swaps out the start window for the application.

--在这点上,当应用绘制了首帧之后,系统进程会把启动窗口替换成app的显示窗口。

b4f99139139d0fe8a4c82b049e51b192.png

Now,to be clear,the majority of that entire process happens pretty cleanly.There's not really much chance that performance can go off the rails.However,there are three big areas where things could become problematic that you should keep an eye on.

--现在很清楚了,大多数的进程产生的很干净(大概这个意思吧)。性能出差错的可能性也很小。但是还是有三个大的方面会造成问题,需要你自己关注。

4578a1e024a025440401124de55da589.png

The first thing you should really take a look at is all the work that goes into creating your activity class.Most often,there's lots of heavy lifting that occurs during this process,but heaviest has to be the inflation of layouts and loading of resources that goes along with it.This is not a cheap process,and if your layouts are too complex or you've got some blocking logic in there,this can cause some really big problems.

--第一件你需要真正关注的事情就是,所有的工作都会先创建你的activity 类。最经常的,大量繁重任务发生在这个进程期间。但是最繁重的还是inflation布局和加载资源。这不是一个廉价的进程,如果你的布局太复杂,或者是你在这里产生了一些闭锁逻辑,那就会造成×××烦。

0aecd590313ac9cfc139f2f0a3e1e13d.png

On a similar note,make sure you take a look at application initialization.For really complex apps,the initialization of the app object often becomes a junk drawer for lots of global classes that might be used between activities.So there tends to be lots of work here that could be deferred to later times or perhaps loded in a lazy-load fashion.

--同样的,确定你关注了应用的初始化,对于复杂的app,app对象的初始化经常会成为一个大量全局类的一个垃圾抽屉,可能会被用在活动中(翻译的越来越不行了)。所以这里会有很多工作可能会被推迟,或者是在lazy-load模式下加载。

8e28146b44662877f92cd2169c6c50ab.png

Now there's lots of applications out there which provide custom start windows.This is either done to help brand the application or to make a slow load look like a custom-branded application.Now if you're doing this to hide bad load times,obcviously,you should fix that first.But if you're doing it for branding,then you need to be aware that there's a right and a wrong way to set this up,so that it does'nt influence the user perception too negatively.

--现在,很多的应用都会提供启动窗口。或者帮助展示应用品牌,或者降慢应用加载。既然你做这个就是为了隐藏不好的加载时间,那么你就应该修复他。但是如果你设置品牌,那你就应该注意到设置这个的正确和错误的方式。以便不会让用户感觉到太消极。

5295fa1c74dfcf8ba15073d714a83328.png

But before you run off into the weeds and try to fix these types of common patterns,you need to sit down and figure out if you have a problem in the first place.Thankfully,Android has a few tools to help.

--在解决问题之前,如果你在第一个位置有问题,你需要坐下来,找出它。感谢android有这些工具来帮助我们。

3d066adfaf27c8b392c5c447f9d2d4a5.png

Firstly is display time.For releases after KitKat,Logcat will include an output line which displays the amount of time between when the process was launched and the activity finally drawn to the screen.This can be helpful,because it gives you a general idea how long it

takes to occur for your application.

--首先是显示时间。在KitKat(Android 4.4)版本之后,日志将会包含输出行,它将显示在从程序启动到activity最后绘制在屏幕这段总的时间。这是很有帮助的,对于你的应用启动的时间,他会给出一个大体的建议。

6aa9b7a6ea1e8516314977ddf567bbd4.png

oh,by the way,not if you want to see this value inisde of Android Studio,you need to turn off filters for the Logcat output. So keep that in mind.

--噢,记住,如果你不想在Android Studio看到这些值,你需要在日志输出上关掉过滤功能。

8a891199135893979d9a39c7bd0a0196.png

Secondly is reportFullyDrawn function.The displayed metric that's repoted in Logcat is useful for most situations where you'd like to track down the time it takes to go from application start to first viseble.Howere,in modern application development,there's often a great deal of lazy loading-that is,rather than blocking the initial drawing of the window,asynchronously loading resources and views in the background and updating the view hierachy accordingly.The result is that while the initial activity may be visible,it may not yet be fully loaded with respect to resources,which could be considered a separate metric to use when evaluating launch time performance.

--其次是reportFullyDrawn方法:我们通常来说会使用异步懒加载的方式来提升程序画面的显示速度,这通常会导致的一个问题是,程序画面已经显示,可是内容却还在加载中。为了衡量这些异步加载资源所耗费的时间,我们可以在异步加载完毕之后调用activity.reportFullyDrawn()方法来告诉系统此时的状态,以便获取整个加载的耗时。

4a119fcf35de8bb69a016a2dd04964ff.png

To address this concern,you can manually call the activity.reportFullyDrawn function to let the system know that your activity is finished with its lazy loading.

ca21c1e7fef754659deaf1079f5d1260.png

Third is method tracing.While display time and reportFullyDrawn give a good understanding of the overall load time of your application,they do not provide details into what may be causing particular parts of that pipeline to go longer than expected.To gain more insight in this area,you can use the start method tracing tool inside of Android Studio.

然后是Method Tracing:前面两个方法提供了启动耗时的总时间,可是却无法提供具体的耗时细节。为了获取具体的耗时分布情况,我们可以使用Method Tracing工具来进行详细的测量。

a2ea38633aa041886aca4397e908dc61.png

And finally is the big one,Systrace.when you add trace functions inside of your onCreate methods,it will augment your logging,such that the Systrace tool can properly discover all the subsections and display them in this graphing process.

--最后的重头戏是Systrace。最后是Systrace:我们可以在onCreate方法里面添加trace方法来声明需要跟踪的起止位置,系统会帮忙统计中间经历过的函数调用耗时,并输出报表。

d305e6c2af69352d9abfc0ca8d062c6a.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值