diff --git a/docs/html/topic/performance/images/cold-launch.png b/docs/html/topic/performance/images/cold-launch.png new file mode 100644 index 0000000000000..2935ece786651 Binary files /dev/null and b/docs/html/topic/performance/images/cold-launch.png differ diff --git a/docs/html/topic/performance/images/displayed-logcat.png b/docs/html/topic/performance/images/displayed-logcat.png new file mode 100644 index 0000000000000..7dee884d5a0c7 Binary files /dev/null and b/docs/html/topic/performance/images/displayed-logcat.png differ diff --git a/docs/html/topic/performance/launch-time.jd b/docs/html/topic/performance/launch-time.jd new file mode 100644 index 0000000000000..c9ce1d56185fb --- /dev/null +++ b/docs/html/topic/performance/launch-time.jd @@ -0,0 +1,565 @@ +page.title=Launch-Time Performance +@jd:body + +
+
+ +

In this document

+
    +
  1. Launch Internals +
      +
    1. Cold start
    2. +
    3. Warm start
    4. +
    5. Lukewarm start
    6. +
    +
  2. +
  3. Profiling Launch Performance +
      +
    1. Time to initial display
    2. +
    3. Time to full display
    4. +
    +
  4. +
  5. Common Issues +
      +
    1. Heavy app initialization
    2. +
    3. Heavy activity initialization
    4. +
    5. Themed launch screens
    6. +
    +
  6. +
+
+
+ +

+Users expect apps to be responsive and fast to load. An app with a slow startup +time doesn’t meet this expectation, and can be disappointing to users. This +sort of poor experience may cause a user to rate your app poorly on the Play +store, or even abandon your app altogether. +

+ +

+This document provides information to help you optimize your app’s launch time. +It begins by explaining the internals of the launch process. Next, it discusses +how to profile startup performance. Last, it describes some common startup-time +issues, and gives some hints on how to address them. +

+ +

Launch Internals

+ +

+App launch can take place in one of three states, each affecting how +long it takes for your app to become visible to the user: cold start, +warm start, and lukewarm start. In a cold start, your app starts from scratch. +In the other states, the system needs to bring the app from the background to +the foreground. We recommend that you always optimize based on an assumption of +a cold start. Doing so can improve the performance of warm and lukewarm starts, +as well. +

+ +

+To optimize your app for fast startup, it’s useful to understand what’s +happening at the system and app levels, and how they interact, in each of +these states. +

+ +

Cold start

+ +

+A cold start refers to an app’s starting from scratch: the system’s process +has not, until this start, created the app’s process. Cold starts happen in +cases such as your app’s being launched for the first time since the device +booted, or since the system killed the app. This type of start presents the +greatest challenge in terms of minimizing startup time, because the system +and app have more work to do than in the other launch states. +

+ +

+At the beginning of a cold start, the system has three tasks. These tasks are: +

+ +
    +
  1. Loading and launching the app.
  2. +
  3. Displaying a blank starting window for the app immediately after launch. +
  4. +
  5. Creating the app + + process.
  6. +
+
+

+As soon as the system creates the app process, the app process is responsible +for the next stages. These stages are: +

+ +
    +
  1. Creating the app object.
  2. +
  3. Launching the main thread.
  4. +
  5. Creating the main activity.
  6. +
  7. Inflating views.
  8. +
  9. Laying out the screen.
  10. +
  11. Performing the initial draw.
  12. +
+ +

+Once the app process has completed the first draw, the system process swaps +out the currently displayed background window, replacing it with the main +activity. At this point, the user can start using the 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. +

+ +

Application creation

+ +

+When your application launches, the blank starting window remains on the screen +until the system finishes drawing the app for the first time. At that point, +the system process swaps out the starting window for your app, allowing the +user to start interacting with the app. +

+ +

+If you’ve overloaded {@link android.app.Application#onCreate() Application.oncreate()} +in your own app, the app starts by calling this +method on your app object. Afterwards, the app spawns the main thread, also +known as the UI thread, and tasks it with creating your main activity. +

+ +

+From this point, system- and app-level processes proceed in accordance with +the +app lifecycle stages. +

+ +

Activity creation

+ +

+After the app process creates your activity, the activity performs the +following operations: +

+ +
    +
  1. Initializes values.
  2. +
  3. Calls constructors.
  4. +
  5. Calls the callback method, such as + {@link android.app.Activity#onCreate(android.os.Bundle) Activity.onCreate()}, + appropriate to the current lifecycle state of the activity.
  6. +
+ +

+Typically, the +{@link android.app.Activity#onCreate(android.os.Bundle) 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. +

+ +

Warm start

+ +

+A warm start of your application is much simpler and lower-overhead than a +cold start. In a warm start, all the system does is bring your activity to +the foreground. If all of your application’s activities are still resident in +memory, then the app can avoid having to repeat object initialization, layout +inflation, and rendering. +

+ +

+However, if some memory has been purged in response to memory trimming +events, such as +{@link android.content.ComponentCallbacks2#onTrimMemory(int) onTrimMemory()}, +then those objects will need to be recreated in +response to the warm start event. +

+ +

+A warm start displays the same on-screen behavior as a cold start scenario: +The system process displays a blank screen until the app has finished rendering +the activity. +

+ +

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. There are many potential states that could be considered +lukewarm starts. For instance: +

+ + + +

Profiling Launch Performance

+ +

+In order to properly diagnose start time performance, you can track metrics +that show how long it takes your application to start. +

+ +

Time to initial display

+ +

+From Android 4.4 (API level 19), logcat includes an output line containing +a value called {@code Displayed}. This value represents +the amount of time elapsed between launching the process and finishing drawing +the corresponding activity on the screen. The elapsed time encompasses the +following sequence of events: +

+ +
    +
  1. Launch the process.
  2. +
  3. Initialize the objects.
  4. +
  5. Create and initialize the activity.
  6. +
  7. Inflate the layout.
  8. +
  9. Draw your application for the first time.
  10. +
+ +

+The reported log line looks similar to the following example: +

+ +
+ActivityManager: Displayed com.android.myexample/.StartupTiming: +3s534ms
+
+ +

+If you’re tracking logcat output from the command line, or in a terminal, +finding the elapsed time is straightforward. To find elapsed time in +Android Studio, you must disable filters in your logcat view. Disabling the +filters is necessary because the system server, not the app itself, serves +this log. +

+ +

+Once you’ve made the appropriate settings, you can easily search for the +correct term to see the time. Figure 2 shows how to disable filters, and, +in the second line of output from the bottom, an example of logcat output of +the {@code Displayed} time. +

+
+ + +

+ Figure 2. Disabling filters, and + finding the {@code Displayed} value in logcat. +

+ +

+The {@code Displayed} metric in the logcat output does not necessarily capture +the amount of time until all resources are loaded and displayed: it leaves out +resources that are not referenced in the layout file or that the app creates +as part of object initialization. It excludes these resources because loading +them is an inline process, and does not block the app’s initial display. +

+ +

Time to full display

+ +

+You can use the {@link android.app.Activity#reportFullyDrawn()} method to +measure the elapsed time +between application launch and complete display of all resources and view +hierarchies. This can be valuable in cases where an app performs lazy loading. +In lazy loading, an app does not block the initial drawing of the window, but +instead asynchronously loads resources and updates the view hierarchy. +

+ +

+If, due to lazy loading, an app’s initial display does not include all +resources, you might consider the completed loading and display of all +resources and views as a separate metric: For example, your UI might be +fully loaded, with some text drawn, but not yet display images that the +app must fetch from the network. +

+ +

+To address this concern, you can manually call +{@link android.app.Activity#reportFullyDrawn()} +to let the system know that your activity is +finished with its lazy loading. When you use this method, the value +that logcat displays is the time elapsed +since the creation of the application object, and the moment +{@link android.app.Activity#reportFullyDrawn()} is called. +

+ +

+If you learn that your display times are slower than you’d like, you can +go on to try to identify the bottlenecks in the startup process. +

+ +

Identifying bottlenecks

+ +

+Two good ways to look for bottlenecks are Android Studio’s Method Tracer tool +and inline tracing. To learn about Method Tracer, see that tool’s +documentation. +

+ +

+If you do not have access to the Method Tracer tool, or cannot start the tool +at the correct time to gain log information, you can gain similar insight +through inline tracing inside of your apps’ and activities’ {@code onCreate()} +methods. To learn about inline tracing, see the reference documentation for +the {@link android.os.Trace} functions, and for the +Systrace tool. +

+ +

Common Issues

+ +

+This section discusses several issues that often affect apps’ startup +performance. These issues chiefly concern initializing app and activity +objects, as well as the loading of screens. +

+ +

Heavy app initialization

+ +

+Launch performance can suffer when your code overrides the {@code Application} +object, and executes heavy work or complex logic when initializing that object. +Your app may waste time during startup if your Application subclasses perform +initializations that don’t need to be done yet. Some initializations may be +completely unnecessary: for example, initializing state information for the +main activity, when the app has actually started up in response to an intent. +With an intent, the app uses only a subset of the previously initialized state +data. +

+ +

+Other challenges during app initialization include garbage-collection events +that are impactful or numerous, or disk I/O happening concurrently with +initialization, further blocking the initialization process. Garbage collection +is especially a consideration with the Dalvik runtime; the Art runtime performs +garbage collection concurrently, minimizing that operation's impact. +

+ +

Diagnosing the problem

+ +

+You can use method tracing or inline tracing to try to diagnose the problem. +

+ +
Method tracing
+ +

+Running the Method Tracer tool reveals that the +{@link android.app.Instrumentation#callApplicationOnCreate(android.app.Application) callApplicationOnCreate()} +method eventually calls your {@code com.example.customApplication.onCreate} +method. If the tool shows that these +methods are taking a long time to finish executing, you should explore further +to see what work is occurring there. +

+ +
Inline tracing
+ +

+Use inline tracing to investigate likely culprits including: +

+ + + + +

Solutions to the problem

+ +

+Whether the problem lies with unnecessary initializations or disk I/O, +the solution calls for lazy-initializing objects: initializing only those +objects that are immediately needed. For example, rather than creating global +static objects, instead, move to a singleton pattern, where the app initalizes +objects only the first time it accesses them. +

+ +

Heavy activity initialization

+ +

+Activity creation often entails a lot of high-overhead work. Often, there are +opportunities to optimize this work to achieve performance improvements. Such +common issues include: +

+ + + +

Diagnosing the problem

+ +

+In this case, as well, both method tracing and inline tracing can prove useful. +

+ +
Method tracing
+ +

+When running the Method Tracer tool, the particular areas to +focus on your your app’s {@link android.app.Application} subclass constructors and +{@code com.example.customApplication.onCreate()} methods. +

+ +

+If the tool shows that these methods are taking a long time to finish +executing, you should explore further to see what work is occurring there. +

+ +
Inline tracing
+ +

+Use inline tracing to investigate likely culprits including: +

+ + + +

Solutions to the problem

+ +

+There are many potential bottlenecks, but two common problems and remedies +are as follows: +

+ +