In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what are the methods of starting optimization". In the daily operation, I believe that many people have doubts about the methods of starting optimization. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "what are the methods of starting optimization?" Next, please follow the editor to study!
Cold start, warm start, hot start
First of all, take a look at these three concepts of initiation, which are also often asked in interviews:
Cold start. Cold start refers to the fact that the application has not been created before and occurs when the application is started for the first time or restarted since it was last terminated. To put it simply, there is no app process yet, so you need to create an app process and start app.
For example, after booting, click the app icon on the screen to start the application.
The process of cold start is mainly divided into two steps:
1) system tasks. Load and start the application; display a blank startup window for the application; create an APP process
2) APP process task. Start the main thread; create Activity; load layout; screen layout; draw screen
Actually, this is the startup process of APP, isn't it? So cold start will complete a startup process, from the system to the process.
Warm start. Warm start means that the App process exists, but the Activity may be reclaimed due to insufficient memory. At this time, you don't need to recreate the process to start App, you only need to perform some tasks in the APP process, such as creating Activity.
For example, after returning Home, continue to use other APP, for a long time or open more applications, the Activity of the previous application may be recycled, but the process is still there.
So the warm startup process is equivalent to performing the second process of cold startup, that is, the APP process task, which requires restarting threads, Activity, and so on.
Hot start. Hot start means that the App process exists and the Activity object still exists in memory and has not been reclaimed.
For example, app is cut to the background and the process of starting app again.
Therefore, the cost of hot startup is the least, and this process will only display Activity from the background to the foreground without initialization, layout drawing and other work.
Optimization point
Among the three startup methods, cold startup takes the longest time and completes the most complete startup process, so let's analyze the startup process of cold startup again to see what can be optimized:
Launcher startActivityAMS startActivityZygote fork process ActivityThread main () ActivityThread attachhandleBindApplicationattachBaseContextApplication attachinstallContentProvidersApplication onCreateLooper.loopActivity onCreate,onResume
Throughout the whole process, in fact, there is not much we can move, but Application's attach,onCreate method, Activity's onCreate,onResume method, these methods are our optimization points.
Optimization scheme 1) eliminate the white / black screen at startup
When App starts, there will be a white screen / black screen time. We can provide a drawable to the launched Activity by setting the drawable attribute, which gives the user a false impression of express start.
@ drawable/logo
2) lazy loading / asynchronous loading of third-party libraries
In Application's onCreate method, there are always a lot of initialization operations, such as Friends, Database, Network request Library, Advertising SDK, and so on.
What can we do about this?
Load asynchronously. Some libraries do not need to be initialized in the main thread, so we can initialize and load asynchronously in the child thread. Delayed loading. Some libraries do not need to be initialized at the beginning. We can initialize some libraries as needed, and then initialize some libraries when we use them, or put them on the startup page for initialization.
So we need to analyze these initialization operations, which need to be carried out in the main thread, which can delay loading, which initialization tasks have priority, and so on. Here involves the concept of a starter, the use of the initiator is to make full use of CPU multi-core, automatically sort out the sequence of tasks. If you are free, you can check it out.
One more thing to note here is the use of threads:
That is, do not create threads frequently, which consumes performance, so thread pools are needed to perform asynchronous tasks. 3) pre-create Activity
When an object in Java is first created, the java virtual machine first checks to see if the corresponding Class object of the class has been loaded. If it is not loaded, jvm looks for the .class file based on the class name and loads its Class object. The second new of the same class eliminates the need to load the class object, but instantiates it directly, which shortens the creation time.
This is the practice in Jinri Toutiao. First, create an instance of Activity.
4) preload data
In our startup page or home page, we can save some data to memory or database, so other pages can use and display the data directly when they need it.
5) Multidex preloading optimization
Due to the limitation of the 65536 method, class files generally have to generate multiple dex files. Under Android5.0, when ClassLoader loads classes, it will only load from class.dex (main dex), so you have to execute the MultiDex.install (context) method to read all dex classes normally.
This install method is time-consuming and decompresses apk, traverses dex files, compresses dex, converts dex files into DexFile objects through reflection, and reflects replacement arrays.
The solution needed here is Jinri Toutiao:
In the attachBaseContext method of Application, the LoadDexActivity that starts another process executes the MultiDex logic asynchronously, displaying Loading.
Then the main process Application enters the while loop and constantly checks whether the MultiDex operation is completed. After the MultiDex execution, the main process Application continues to execute the ContentProvider initialization and Application onCreate method, which is the normal logic of the main process.
So the point is to open the process alone to execute the MultiDex logic, so that it does not affect the startup of APP.
Of course, this is only for loading Multidex below 5.0. ART loading classes are used by default above 5.0. when you install, you have already converted dex files to oat files, so there is no need to optimize the Multidex situation.
6) Webview startup optimization
If our home page involves Webview, then we have to deal with WebView optimization. Because the creation of Webview is time-consuming, we optimize the Webview with the following scenarios:
Create WebView beforehand and initialize its kernel ahead of time. Using the WebView cache pool, get the Webview instance from the cache pool. Static page resources are provided locally. 7) avoid layout nesting
If the layout of the startup page and home page is more complex, it will also affect our startup time, so pay attention to our layout, use more merge,include,constraintlayout, etc., especially the problem of multi-layer nesting.
At this point, the study of "what are the ways to start optimization" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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: 0
*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.