Optimization of the Android performance optimization series
Memory optimization in the Android performance optimization series
Android Performance Optimizer series APK slimming
The slow start-up of applications is a common problem in our development process, such as the slow start of a black screen. White screen question, this blog will introduce the app launch optimization knowledge.
How the app starts
Typically, there are two types of start-up: Cold start and hot start.
1, cold start: When the application is started. There is no process for the app in the background. The system then creates a new process to assign to the app again. The starting mode is cold start.
Cold start because the system will create a new process to assign it again. Therefore, the application class is created and initialized, and the Mainactivity class is created and initialized (containing a series of measurements, layouts, drawings), and is finally displayed on the interface.
2, hot start: When the application started, the background already has the application of the process (example: Press the back key, the home button, the app will quit, but the app's process is still in the background, you can go to the Task List to view). So in the case of an existing process, such a startup will start the application from the existing process, which is called hot start. Hot start will not go application this step because it starts from an existing process. Instead of going directly to Mainactivity (which contains a series of measurements, layouts, and plots), the hot-start process simply needs to create and initialize a mainactivity, without having to create and initialize application, due to the destruction of an application from the new process to the process , the application is initialized only once.
app start-up process
The optimization referred to in this article is for cold start. Simply explain the app's startup process:
1. Click Launcher. Start the program and notify Activitymanagerservice
2.ActivityManagerService notification Zygote process hatching application process, allocating memory space, etc.
3. The main () method that runs the application Activitythread
4. Application notification Activitymanagerservice It has been started, Activitymanagerservice save a proxy object for the application, activitymanagerservice through which it can control the application process
5.ActivityManagerService notifies the application process to create an activity instance of the portal, running its life cycle
The life cycle methods of application and ingress activity during startup are called in the following order, for example:
1.Application Construction Method
2.attachBaseContext ()
3.onCreate ()
4. Object construction of the entry activity
5.setTheme () Setting up topics and other information
6. OnCreate of the entry activity ()
7. OnStart of the entry activity ()
8. Onresume of the entry activity ()
9. Onattachtowindow of the entry activity ()
10. Onwindowfocuschanged of the entry activity ()
What is the application start time
Start by clicking on the app's startup icon to create a new process until we see the first frame of the interface. This time is the start time of the application.
We have to measure this time, measured this time can be measured in the way of the adb shell command, such a measure is the most accurate, the command is:
start -W [PackageName]/[PackageName.MainActivity]
1, Thistime: General and TotalTime time. This is smaller than totaltime unless a transparent activity is opened at the start of the app to pre-process something and then show the main activity.
2, TotalTime: The start time of the application, including the creation process +application initialization +activity initialization to the interface display.
3, WaitTime: Generally larger than totaltime, including the system impact time-consuming.
Analyze start-up time with TraceView
At the beginning and end of the OnCreate, I hit trace.
Debug.startMethodTracing("TestApp");...Debug.stopMethodTracing();
Running the program will generate a "testapp.trace" file on the SDcard.
Note: You need to add write storage permissions to the program:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Export it to local via ADB pull
adb pull /sdcard/TestApp.trace ~/testSpeed.trace
Open the DDMS profiling trace file and the following interface appears
Generally only need attention: Calls + recur calls/total and Cpu Time/call
The Cpu Time/call reflects the number of calls, but it takes a very long time for each call to function
Calls + recur calls/total reflect a function that takes a very long time but is called very frequently
How to reduce the time-consuming application startup time
Some time-consuming for cold start time. Can take the following strategy:
1, in the application constructor method, Attachbasecontext (), OnCreate () method do not take the initialization of time-consuming operation, some data prefetching in the asynchronous thread, can take the callable implementation.
2, for the SP initialization, because the SP's characteristics at initialization time will be all the data read out in memory, so this initialization is placed in the main thread is inappropriate, but will delay the application startup speed. This still needs to be handled in an asynchronous thread.
3, for Mainactivity, because before getting to the first frame. The contentview needs to be measured and plotted to minimize the level of layout. Consider Stubview's deferred loading strategy, which of course avoids time-consuming operations in the OnCreate, OnStart, and Onresume methods.
Follow these three strategies to significantly increase app startup speed.
optimize your app's experience when it starts
For application start-up time, it is only possible to avoid some time-consuming, unnecessary operations in the main thread. This reduces the amount of time it takes to start a part. On the other hand, while waiting for the first frame to appear, you can add some configuration to increase the experience, such as adding activity to the background, which will appear in front of the screen before the first frame is displayed. For application start-up time, it is only possible to avoid some time-consuming, unnecessary operations in the main thread, which is relatively able to reduce the time spent on a part of the startup, on the other hand in waiting for the first frame display time, can add some configuration to increase the experience. For example, increase the activity's background, which will appear in front of the screen before the first frame is displayed.
Scenario 1:
1, first the main interface to write a separate theme style, set up a picture to be displayed, here I set a color, and then set to mainactivity in manifest:
<style name="Apptheme.launcher"><item name= "android: Windowbackground">@drawable/bule< /item> </style>//...<activityandroid:name=". Mainactivity "android:label=" @string/app_name "android:theme=" @style/ Apptheme.launcher "> <intent-filter> <action android:name="Android.intent.action.MAIN" /> <category android:name="Android.intent.category.LAUNCHER" /> </intent-filter> </activity>
2. Then set the Apptheme to mainactivity again before loading the layout into the mainactivity:
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) { setTheme(R.style.AppTheme); super.onCreate(savedInstanceState);
This will display the background at startup, and then display the main interface after the interface is drawn:
Scenario 2: By setting the style
(1) Setting the background map theme
By setting a background map.
When the program is started. This background image is displayed first. Prevent black screen from appearing
<stylename="Apptheme"Parent="Theme.AppCompat.Light.DarkActionBar"> <Item name="Android:screenorientation">portrait</Item> <Item name="Android:windowbackground">> @mipmap/splash</Item> <Item name="Android:windowistranslucent">true</Item> <Item name="Android:windownotitle">true</Item></style>
(2) Set transparent theme
By setting the style to transparent, the program will not black screen after startup, but the whole transparent, wait until the interface is initialized to show it once
<stylename="Apptheme"Parent="Theme.AppCompat.Light.DarkActionBar"> <Item name="Android:windownotitle">true</Item> <Item name="Android:windowbackground"> @android: color/transparent</Item> <Item name="Android:windowistranslucent">true</Item> <Item name="Android:screenorientation">portrait</Item> </style>
The two contrasts:
THEME1 program starts fast. The interface displays the background image first, and then refreshes the other interface controls. Give people a refreshing feeling of being out of sync.
Theme2 gives the program a slow-start feeling. The interface is brushed out at once. Refreshes the synchronization.
(3) Change Androidmanifest.xml
<applicationandroid:name=". App "android:allowbackup=" true "android:icon=" @mipmap/ic_launcher " Android:label="@string/app_name"android:supportsrtl="true"> <activity android:name=". Mainactivity "android:theme=" @style/apptheme "> <intent-filter> <action android:name="Android.intent.action.MAIN" /> <category android:name="Android.intent.category.LAUNCHER" /> </intent-filter> </activity>//......</Application>
References:
http://www.jianshu.com/p/a0e242d57360
Android performance Optimization series app launch optimization