android start speed optimization

app startup speed of the user experience is an important indicator, when unusually slow a strong alternative line app starts, will inevitably lead to the loss of customers.

App startup mode Cold Start Warm Start Hot Start

Cold Start (Cold start)

Cold start means the APP runs for the first time after the phone is activated, or APP kill off the process after the start again.

A necessary condition for a cold start can be seen that the APP process does not exist, which means that the system needs to create a process, APP needs to be initialized. In this three starts, the longest cold start, for optimizing cold start is also the most challenging. Therefore, the focus of this article is talking about related to the optimization of a cold start.

Warm start (Warm start)

App process exists, then Activity may be recovered because of inadequate memory. This App does not require re-create the startup process, but the Activity of onCrate still need to be performed. Similar scenes open Taobao shopping in a circle and then cut to the micro-channel chat to go, and after a half hour back to Taobao again. This time Taobao processes exist, however Activity may be recycled, this time only need to reload Activity can be.

Hot Start (Hot start)

App process exists, and still has not been recovered Activity object exists in memory. You can avoid repeated object initialization, rendering the layout analysis.

Similar scenes would you open a micro letter chatted for a day out at this time looked at the calendar to open a micro-letter-letter this time belongs start hot start.

In a recent mission to the App lock and start the way have anything to do with some vendors in order to provide the user experience to APP locked function, the purpose is to allow users to be themselves locked APP is not killed, in cold start time does not start-up mode, but the lock is not a panacea, Low memory killer in memory extremely tight situation also kill locking APP, when running in this mode will also start a cold start.

We can use the time to get adb command to start the application, to get the current application start-up time

adb shell am start -W [packageName]/[.MainActivity]

Here Insert Picture Description
A total returns three results, we WaitTime prevail.

ThisTime: General and TotalTime the same time, unless an open and transparent application startup Activity pretreated something again shows the main Activity, TotalTime smaller than this.

TotalTime: Start time of the application, including the creation process initialization + Activity + Application interface to initialize the display.

WaitTime: General TotalTime than big points, including the time-consuming affected system.

Step impact

The system creates application process, the application process will do the following:

1. Creating an Application Object

2. Start the main thread

3. Create the main Activity

4. Draw view (View)

The layout of the screen

6. Perform initialization draw

Therefore, to optimize the startup speed optimization process is essentially an appeal to reduce these time-consuming processes.

Related optimization

Visual optimization

Open the application will always have a black brief case (white) screen, resulting in Caton experience problems, you can set for the first activity, or to set a transparent background to optimize the visual

optimized application onCreate

1. The process of initializing the third party SDK
Application program main entrance, many constituents SDK sample program are required to do their own initialization operation when the Application OnCreate. This is the main culprit Application OnCreate time increase, it is necessary to avoid during synchronization Application onCreate do initialization. A better solution is on the line for tripartite SDK lazy loading, initialization is not in the Application OnCreate (), go to load when you actually used.

Comparative Examples below the starting speed ImageLoader after optimization using lazy loading.

Generally, we will use imageLoader when loaded in the main thread () in the Application onCreate:

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration.Builder config =
                new ImageLoaderConfiguration.Builder(this);
        ImageLoader.getInstance().init(config.build());
    }
}

Lazy loading tools ImageLoader of example:

public class ImageUtil {

    private static boolean sInit;

    private synchronized static void ensureInit() {
        if (sInit) {
            return;
        }
        ImageLoaderConfiguration.Builder config =
                new ImageLoaderConfiguration.Builder(SecurityCoreApplication.getInstance());
      ....
        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config.build());
        sInit = true;
    }

    public static void display(String uri, ImageView imageView, boolean        cacheOnDisk) {
        imageView.setImageResource(R.drawable.icon_app_default);
        ensureInit();
        ImageLoader loader = ImageLoader.getInstance();
        if (cacheOnDisk) {
            loader.displayImage(uri, imageView);
        } else {
            loader.displayImage(uri, imageView, OPTIONS_NO_CACHE_DISK);
        }
    }
}

Application OnCreate need to avoid
the main thread to do a lot of time-consuming operations, such as logic and IO related, so will affect the application launch speed. If you do need to be placed in the sub-thread.

Multithreading avoid performing unnecessary operations
if the application set up an independent process, then the Application onCreate the need to determine the current process, there are initialization functions required for the current process.

Activity onCreate optimized
reduced LaunchActivity level of View, View reduced drawing time measurement; ViewStub use, the unnecessary View (such as ad power) delay loading

The main thread to avoid I / O operation, deserialization, network operations.

Guess you like

Origin blog.csdn.net/Android_SE/article/details/90755808