In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "Android performance Optimization Scheme Analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Indicators
There are many metrics to quantify performance, but the most important are the following five:
Packet size
Response time
Memory
CPU
Power consumption
To optimize performance, you can start with the above five points.
2. Optimization of packet size
As the name implies, it reduces the size of the apk package. The apk size mainly depends on the resource files and .class files under the res.
The specific optimization measures are as follows:
Compress the image size and then use it in the project.
Within AndroidStudio, images in formats such as png can be compressed to .webp format, which can further reduce the image size.
To reduce the use of local resources as much as possible, we can consider pulling pictures, lottie, so libraries and other resources from the server.
Use lottie to replace the use of frame animation to reduce the use of frame animation picture resources.
Use confusion to delete useless code and reduce the size of dex files.
3. Response time optimization
For users, the shorter the response time, the better. The shorter the response time, the smoother the operation.
Response speed includes startup speed-the process of clicking the APP button to fully open the home page of APP is as fast as possible, and the response speed of the page is as fast as possible-after the user clicks, slides and other operations, the page can respond quickly. APP cannot produce stutter, let alone ANR.
The specific optimization measures are as follows:
Time-consuming operations should be handled by child threads, and the main thread should not be blocked.
Resources such as SDK should be loaded lazily, only when needed, but not when needed.
The online environment avoids printing a large number of logs.
Use the inBitmap variable of BitmapFactory.Option to reuse the old Bitmap, avoid allocating memory for the new Bitmap multiple times and destroying the old Bitmap (if the Bitmap is used frequently)
Optimize view view rendering time:
① if the view view is complex, consider using ConstraintLayout to constrain the layout to reduce the level of view rendering.
For ②, if the view view is relatively simple, consider using LinearLayout horizontal layout for optimization (because the rendering time of LinearLayout is shorter than that of ConstraintLayout and RelativeLayout).
③ avoids overrendering. If the backgrounds of multiple view overlap, you can consider removing the underlying overlaid view; theme theme and setting it to NoBackground mode.
④ if the view view is created when needed, use the ViewStub control.
Recyclerview list control optimization:
View view optimization for ① item, same as point 4.
② increases the number of item caches in recyclerview and caches the data requested by the network to avoid secondary requests to the network.
③ avoids performing time-consuming operations in onBindViewHolder because onBindViewHolder is executed on the main thread, and onBindViewHolder time-consuming operations affect sliding fluency.
If ④ does not need the default animation of recyclerview, delete it. (for example, the animation effect that flashes when refreshed)
When ⑤ recyclerview refreshes, try to use local refresh to avoid global refresh.
To check whether view is overrendered, you can enable the following settings in mobile developer mode:
4. Memory optimization
Reduce the use of memory, mainly to avoid creating too many objects to take up too much memory, to avoid memory jitter and to avoid memory leaks.
Memory jitter is the frequent creation and destruction of memory, in the process, the garbage collector will also work frequently, affecting memory performance.
A memory leak is the memory that should be reclaimed by GC, which cannot be reclaimed because it is still being referenced by other objects. Memory leak is a serious problem, too much memory leak will lead to memory overflow, resulting in OOM system errors.
The main causes of memory leaks are:
The singleton class references Context to cause a memory leak.
A non-static inner class references an external class to cause a memory leak.
Handler references activity to cause a memory leak.
The property animation is not canceled, causing the view to be referenced all the time, resulting in a memory leak.
The listener is not canceled and the callback is not unregistered.
Measures for memory optimization include:
Use a thread pool to multiplex threads, because the threads themselves take up relatively large amounts of memory, and reuse can save some memory.
Avoid creating objects within the onDraw method. Because onDraw will be called frequently, its internal objects will also be created frequently, taking up too much memory.
Try to use StringBuilder or StringBuffer to concatenate strings to reduce the use of String. (because when concatenating a string, String creates a new object, while StringBuilder and StringBuffer are concatenated based on the original string.)
Clear when the view resource is not visible to avoid taking up memory. For example, Bitmap executes the .recycle method to clear and destroy pictures and lottie resources.
Optimize for memory leaks:
The ① singleton class should refer to the Context of Application, because the life cycle of Application's Context is the same as that of APP and does not cause the problem that the singleton class references the context of an activity so that the activity cannot be recycled.
② changes the non-static inner class to the static inner class so that the external class is not referenced.
③ handler:a.handler calls removeCallbacksAndMessages (null) at the end of the usage to clear the queue; b. Static inner classes + weak references avoid memory leaks.
Static class SafeHandler extends Handler {WeakReference activity; public SafeHandler (MainActivity mainActivity) {activity = new WeakReference (mainActivity);} @ Override public void handleMessage (Message msg) {}}
The animation of ④ properties and the end of the use of listeners should be cancelled in time, and the callback of broadcasts or other external libraries should be unregistered in time.
5. CPU optimization
The function of CPU is to calculate and process information and run programs, so the direction of optimization is to reduce the work of CPU computing and improve the computational efficiency of CPU.
The specific optimization measures are:
Prevent the main thread from executing time-consuming tasks, while time-consuming tasks are executed asynchronously on child threads.
Avoid performing a large number of time-consuming operations in onDraw methods.
Temporarily do not need to use the information for lazy loading, delayed initialization.
6. Optimization of power consumption
The optimization measures are:
Avoid frequent network requests.
To avoid frequent execution of tasks, we can wait until a certain number of tasks are formed, and then execute them together.
Avoid applications waking up the screen frequently. (waking up the screen frequently will prevent the system from going into hibernation and consume a lot of power.)
Ensuring that the performance index does not decline has always been a top priority in the development process. If the development of new features leads to performance problems such as stutter, sharp increase in fuselage heating and power consumption, and increased memory, it will lose users and the loss will outweigh the gain. So focusing on performance is also an invisible job for RD. I hope this article will be helpful to all of you.
This is the end of the content of "Android performance Optimization Scheme Analysis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.