In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how Android optimizes startup speed. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Startup mode
There are three startup states for an application, each of which affects the time it takes for the application to display to the user: cold startup, warm startup, and warm startup
Cold start (startup optimization target)
Cold start means that the application starts from scratch: the system process creates the application process only after the cold start. When cold start occurs, the application starts for the first time after the device starts or after the system terminates the application.
Hot start
In the hot boot, all the work of the system is to bring the Activity to the foreground. As long as all applied Activity resides in memory, the application does not have to repeat object initialization, layout loading, and drawing.
Warm start
Warm startup includes some of the operations that occur during cold startup; at the same time, it is more expensive than hot startup. There are many potential states that can be regarded as warm start. For example:
The user restarts the application after exiting the application. The process may not be destroyed and continue to run, but the application needs to execute onCreate () to recreate the Activity from scratch.
The system releases the application from memory, and then the user restarts it. The process and Activity need to be restarted, but the saved instance savedInstanceState passed to onCreate () helps to accomplish this task.
...
Optimizable links in the startup process
There are not many links that developers can optimize in the startup process, so we can look for it from the APP startup process.
The APP startup process is as follows:
Click the desktop App icon, and the Launcher process uses Binder IPC to initiate a startActivity request to the system_server process.
After receiving the request, the system_server process sends a request to the zygote process to create the process
The Zygote process fork a new child process, namely the App process
App process, which initiates an attachApplication request to the sytem_server process through Binder IPC
After receiving the request, the system_server process makes a series of preparations, and then sends the scheduleLaunchActivity request to the App process through binder IPC.
After receiving the request, the binder thread (ApplicationThread) of the App process sends a LAUNCH_ACTIVITY message to the main thread through handler
After receiving the Message, the main thread creates the target Activity through the reflection mechanism and calls back methods such as Activity.onCreate ().
At this point, App is officially launched and begins to enter the Activity life cycle. After executing the onCreate/onStart/onResume method, you can see the main interface of App after UI rendering.
So, the only stage we can optimize is Application.onCreate ()-> Activity.onWindowFocusChanged ()
Detection tool start time detection
What is the right time to start? What is the time frame in which users feel smooth? Android Vitals considers the startup time of your application to be too long when:
Cold start took 5 seconds or more
Wen Qi took 2 seconds or more.
Hot start took 1.5 seconds or more
How long did it take for APP to start? What zone is used for testing?
Logcat Displayed
In Android 4.4 (API level 19) and later, logcat contains an output line that contains a value named Displayed. This value represents the time it takes from starting the process to completing the drawing of the corresponding Activity on the screen.
Adb command statistics
Adb shell am start-S-W [packageName] / [activityName]
C:\ Users\ * * > adb shell
Generic_x86_arm:/ $am start-S-W com.miss.misslink/.MainActivity
Stopping: com.miss.misslink
Starting: Intent {act=android.intent.action.MAIN cat= [android.intent.category.LAUNCHER] cmp=com.miss.misslink/.MainActivity}
Status: ok
LaunchState: COLD
Activity: com.miss.misslink/.MainActivity
TotalTime: 941
WaitTime: 961
Complete
WaitTime: including the time of the previous application Activity pause and the time when the new application was launched
ThisTime: indicates that the startup of the last Activity in a series of Activity starts takes time
TotalTime: indicates the time it takes for a new application to start, including the start of a new process and Activity, but not the time spent on the previous application Activity pause.
We usually only use TotalTime, so we can clearly know the startup time of APP. So how can we tell which methods take too much time to start APP?
CPU profileAPI level > = 26
To record CPU activity during the application startup process, you need to do the following
1. Select Run > Edit Configurations
two。 Check Trace Java Methods (trace Java method: detect the application at run time to record a timestamp at the beginning and end of each method call. These timestamps are collected and compared to generate method tracking data, including time information and CPU usage. )
3. Select Run > Profile to deploy your application to a device with Android 8.0 (API level 26) or later
There are three main areas of analysis: Flame Chart, Top Down and bottom up.
API level < 26
For those with API less than 26, we can call Debug API and call the starting point Application constructor
Public class MyApplication extends Application {public MyApplication () {/ / does not specify an absolute path, that is, a relative path, relative sdcard Debug.startMethodTracing ("miss");}}
Call the destination Activity.onWindowFocusChanged ()
@ Override public void onWindowFocusChanged (boolean hasFocus) {super.onWindowFocusChanged (hasFocus); Debug.stopMethodTracing ();}
This will generate a miss file under the sdcard path. Double-click to open it.
StrictMode strict mode
StrictMode is a developer tool that detects things we might inadvertently do and brings them to our attention so that we can fix them. StrictMode is most commonly used to capture unexpected disk or network access on the main thread of an application. Help us keep disk and network operations away from the main thread, making applications smoother and more responsive
/ / use @ Override public void onCreate () {if (BuildConfig.DEBUG) {/ / Thread Detection Policy StrictMode.setThreadPolicy (new StrictMode.ThreadPolicy.Builder () .SecrettDiskReads () / read, Write operation .accountDiskWrites () .accountNetwork () / / or .accountAll () for all detectable problems .penaltyLog () .penaltyDeath () .build ()) StrictMode.setVmPolicy (new StrictMode.VmPolicy.Builder (). SecrettLeakedSqlLiteObjects () / Sqlite object leak. LeaktLeakedClosableObjects () / unclosed Closable object leak .penaltyLog () / illegal print log .penaltyDeath () / violation crash .build ()) } Optimization point
Reasonable use of asynchronous initialization, delayed initialization, lazy loading mechanism.
The startup process avoids time-consuming operations, such as database Iripo operations, which should not be performed on the main thread.
Class loading optimization: class loading is performed asynchronously in advance.
Reasonable use of IdleHandler for delayed initialization.
Simplify the layout
Some third-party libraries have optimization plug-ins, such as ARouter
Black and white screen problem
When the system loads and starts App, it takes time, which will cause users to feel that there will be a "delay" when clicking on the App icon. In order to solve this problem, the practice of Google is to show a blank page in the process of creating App, so that users will respond immediately after clicking the icon.
If your application or activity startup process is so slow that the BackgroundWindow of the system is not replaced in time, there will be a white or black screen at startup (depending on whether the Theme theme is Dark or Light). To eliminate the black / white screen problem at startup, most App use their own way to set the background image in Theme.
@ drawable/bg
Then set the Activity back to the original theme in the onCreate method of Activity
@ Override protected void onCreate (Bundle savedInstanceState) {setTheme (R.style.AppTheme); super.onCreate (savedInstanceState);}
Doing so is only to improve the startup user experience. Can not really speed up the start-up speed.
Thank you for reading! This is the end of the article on "how to optimize the startup speed of Android". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 286
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.