Android Development Technology Weekly 183 Learning record tutorial Android performance optimization Contextual summary record one, performance problems common
Memory leaks, frequent GC, power consumption issues, oom issues.
Second, the cause of performance problems
1. A slight time-consuming operation was done in the UI thread, causing the UI line to incurring.
2.layout is too complex to render at 16ms.
Replacing LinearLayout with Relativelayout is a way to reduce the layout hierarchy, however, it is no longer recommended to use Relativelayout, because Constraintlayout is a more performant artifact that eliminates the layout hierarchy. Constraintlayout is based on the cassowary algorithm, and the advantage of the cassowary algorithm is that it has very high efficiency when solving linear equations, and it turns out that the dominant equations are well suited for defining the parameters of user interface elements.
3. Too many animations are performed at the same time, causing the CPU or GPU to be overloaded.
Mainly because the animation will often change the properties of the view, resulting in display invalidation, and need to re-create a new displaylist, if the animation too much, this cost can be imagined.
4.view over-drawing problems.
You can turn on the show GPU overdraw option by using the developer options in the phone settings, and find the problem as close as possible to the blue.
5.gc too much.
6. Resource loading causes slow execution.
Two solutions: A. Preload, that is, before it comes to the path, is loaded well.
B. To wait for the time to load, add a progress bar.
7. The worker thread priority setting is incorrect, causing the UI thread to preempt CPU time.
When using Rxjava, it is important to note that setting the execution thread of a task can have a significant impact on performance.
8. Static Variables
Third, the settlement
1.GPU over-drawn, locating over-drawn area
check for extra background color settings.
2. The main thread time-consuming operation troubleshooting
start Strictmode, since the itch, The good actions of the main thread are presented to the logcat in the form of warnings.
The object of direct suspicion plus @debuglog, to see how time-consuming the method executes. Debuglog annotations need to introduce plug-ins Hugo, this is the early work of the Android God Jakewharton, for the monitoring function execution time is very convenient, directly on the function to add annotations can be achieved, but there is a disadvantage, Is that the last version of Jakewharton release does not support release version replace the monitoring code with an empty method.
3. Problems with measure,layout too much time consuming
4.leakcany
for memory leak detection, introducing
dependencies { debugImplementation ‘com.squareup.leakcanary:leakcanary-android:1.5.4‘ releaseImplementation ‘com.squareup.leakcanary:leakcanary-android-no-op:1.5.4‘}
Note that the introduction of releaseimplementation guarantees that the monitoring code is removed from the release package, otherwise, his own constant catch memory snapshot itself also affects performance.
5.onDraw inside write code need to notice
Ondra because probably every 16s will be executed once, so itself is equivalent to a forloop, if you are inside the new object, unknowingly met a large number of objects in a short period of time to create and release, so frequent GC occurred, so the card. The right thing to do is to put the object outside and new out.
6.json deserialization Problem
JSON deserialization refers to the conversion of a JSON string into an object, if the amount of data is more, especially when there are quite a lot of string, parsing is not only time-consuming, but also eat memory. Workaround:
A. Streamline the fields, negotiate with the background, and exclude unnecessary fields from the related interfaces. The principle of minimum availability is guaranteed.
B. With flow resolution, using Gson.fromjson, you can increase the resolution efficiency by 25%.
Use of 7.viewstub&merge
For components that are only shown under certain conditions, it is recommended to use Viewstub Wrap, include a layout if it is more fearless and the introduction of his husband layout is consistent, it is recommended to use the merge package together.
8. Load Optimization
Encapsulate time-consuming operations into async.
9. Refresh Optimization
A. For the action of item in the list, there is write only need layout refresh, should not let the whole list refresh.
B. For more complex pages, it is recommended not to write in an activity, it is recommended to use a few fragment to assemble, so that the module changes can only refresh a specific fragment, rather than the entire page to go the refresh logic.
10. Animation Optimization
Use hardware acceleration to optimize, and note that after the animation is done, turn off hardware acceleration, because turning on hardware acceleration is itself a drain.
11. Power Consumption Optimization
A. It is recommended that if the positioning accuracy is not high, it is not necessary to turn on GPS positioning using WiFi or the English-East network location.
B. It is recommended to verify the availability of the network, in the sending network request, for example, when the user is in the 2G state, and the operation is to see you a large image, download may be more than 200 k or even larger, there is no need to send this request, so that users have been waiting to load it.
Iv. Code Recommendations
(Use alarm correctly, apply and release wakelock correctly), (use service sparingly), (release memory when the interface is not visible), (Allocate Memery,pd->model, use optimized data collection), (use abstract programming sparingly), (Try/catch statement, which should be placed on the outermost layer), (system.arraycopy () instead of for loop copy), (Hashmap->sparsearray), (load the network picture after the list scrolling stopped), (note using WRAP_ Content), (use Canvas.cliprect () to help the system identify those visible areas, customize View optimization), (use Alarmmanager for timed tasks instead of sleep or timer management).
It is recommended to use only spasearray instead of HashMap, because Sparsearray is more memory-saving than HashMap and performs better under some conditions, mainly because it avoids the automatic boxing of key, which is internally stored by two arrays of data, a store key, Another store value, in order to optimize performance, it also takes a compressed way to represent the data of sparse data, thereby saving memory space.
Not to be forced, do not use Wrap_content, recommend the use of match_parent, or fixed size, with gravity= ' center ', because in the measurement process, match_parent and fixed width corresponds to exactly, and WRAP_ The content corresponds to At_most, which is more time-consuming compared to at_most.
In-depth understanding of flutter's compilation Principles and optimization records
Check the flutter, do not recommend learning, do not look.
The usage and etiquette record of GitHub outside of technology
Tourists
Watch: Indicates that you are interested in the events that occur in this warehouse.
Star (Star): signifies the special marking of this warehouse.
Fork (Branch): Two purposes, one is that you want to participate in it, submit code for it, the other is that the repository may be deleted by the original author, so fork out a copy, so that even if the author deleted, there is a fork that point in time the version of the snapshot.
Contributing people
There are two types of contributions: the issue and the pill request, which have some common requirements, including:
- Serious doctor follow each other to give the issue template/PR template.
- Timely follow-up, when the other side has a reply should be as early as possible to give clear enough answers. If you think the answer has solved your problem, or if it is not a problem, close it in time, do not wait for the author to do.
- Most warehouses are written in English, but the exception is for warehouses dedicated to Chinese users.
Mentioning issue is to ask questions, can be subdivided into two kinds: to mention the bug and demand (Feature request/proposal).
The basic requirement for a bug is to confirm that it is a problem and to clarify the problem.
The basic requirement of demand is to be able to accept the refusal of others frankly.
Pull request (PR) is the request to merge code into the main library. The premise of the PR is to fork the other side of the warehouse, and do not clone down and then upload to their own warehouse, so that GitHub can not know that the two libraries are homologous.
After fork, you have a branch warehouse in your personal warehouse, you can submit code to this branch warehouse, feel that reached the target of PR, then push it, and return to the GitHub page to launch PR.
Author
As the author of the warehouse, the first step is to include an explicit license file in the warehouse.
Usually the warehouse of the code class chooses more open protocols such as MIT, and if it is an open source fanatic, it is possible to opt for more aggressive protocols such as the GPL, but be aware that GitHub does not allow code in the open repositories to use private/purely commercial licensing agreements.
The warehouse of the document class typically chooses either the CC or the CC-BY-NC protocol, the difference being that the former allows for commercial, and the latter does not allow for commercial or commercial authorization alone.
Android Development Technology weekly 183 learning record