[Reading notes] "Android Development Art Exploration" chapter 15th notes

Source: Internet
Author: User

Android Performance optimization

Android cannot have unlimited use of memory and CPU resources, too much memory can cause memory overflow, that is, oom.

Excessive use of CPU resources, usually refers to doing a lot of time-consuming tasks, will cause the phone to become stuck or even the program can not respond to the situation, that is, ANR.

15.1.1 Layout optimization

1, how to optimize the layout?

    • Remove unused controls and hierarchies from the layout first
    • Second, there is a choice of viewgroup with lower performance.

    • Another way to optimize the layout is to use labels, labels, viewstub. Labels are primarily used for layout reuse, and are typically used in conjunction with labels, which reduces the level of layout, while Viewstub provides on-demand functionality to load the Viewstub layout into memory when needed. Improves the initialization efficiency of the program.

2, the label only supports android:layout_ the beginning of the property, Android:id attribute exception.

3, Viewstub inherited the view. It is very lightweight and wide/high is 0, so it does not participate in the layout and drawing process of whatever. The point of viewstub is to load the required layout files on demand, in real-world development. There are a lot of layout files under normal circumstances will not display, such as the network anomaly when the interface, this time there is no need to load the entire interface when the initialization, through the viewstub can be used in the time of loading, improve the program initialization performance.

For example, as seen below, Android:id is the ID of viewstub, and Android:inflatedid is the ID of the root element of the layout.

<ViewStub android:id="@+id/xxx"  android:inflatedId="@+id/yyy"  android:layout="@layout/zzz"  ...</ViewStub>

It is necessary to load the layout of viewstub in the following two ways, for example:

((ViewStub)findViewById(R.id.xx)).setVisibility(View.VISIBLE);

Or

View importPanel = ((ViewStub)findViewById(R.id.stub_import)).inflate();
15.1.1 Drawing Optimization

Drawing optimization refers to the view's OnDraw method to avoid running a large number of operations:

    • Do not create a new local object in OnDraw. This is because the OnDraw method may be called frequently, resulting in a large number of temporary objects in a flash, which not only consumes too much memory but also leads to more frequent GC, which reduces the running efficiency of the program.
    • The OnDraw method does not specify time-consuming tasks, nor can it run thousands of cycles, and the frame rate of view is guaranteed to be the best 60fps. This requires that the drawing time per frame does not exceed 16ms, although the program is very difficult to guarantee the 16ms time, but minimizing the complexity of the OnDraw method is always effective.

15.1.3 Memory leak optimization

There are many scenarios that can cause memory leaks, such as static variables, singleton patterns, property animations, Asynctask, Handler, and so on.

15.1.4 Response speed optimization and ANR log analysis
    1. ANR Occurrence: Activity assumes that there is no response to a screen touch event within 5 seconds or the keyboard input event will be ANR. And Broadcastreceiver assumes that 10s does not run out of operations will also appear ANR.

    2. After a process has been ANR, the system creates a file Traces.txt under the/data/anr folder, which is why the ANR can be located by analyzing the file.
15.1.5ListView and bitmap optimization
    1. ListView Optimization: Use Viewholder and avoid running time-consuming operations in the GetView method. Based on the sliding state of the list, the efficiency of the task is plotted, and it is possible to try to turn on the hardware acceleration period to make the ListView slide smoother.
    2. Bitmap optimization: According to the need to take pictures of the sample, mainly through the bitmapfactory.options to the image according to the need to sample, the sample is mainly used in the Bitmapfactory.options insamplesize the number of references.

15.1.6 Threading Optimization
    1. Use a thread pool to avoid the existence of a large number of threads in the program. The thread pool is able to reuse internal threads, thus avoiding the performance overhead of thread creation and destruction, and the thread pool can effectively control the maximum concurrency of the thread pool at the same time, preventing a large number of threads from competing for system resources and causing clogging.
15.1.7 Some performance tuning recommendations
    • Avoid creating too many objects
    • Do not use enumerations too much, the enumeration takes up more memory space than the shaping
    • Constants can be modified with static final
    • Use some Android-specific data structures, such as Sparsearray and pair. They all have better performance.
    • Proper use of soft and weak references
    • Use memory cache and disk cache
    • Use static internal classes as much as possible to avoid potential memory leaks caused by internal classes
15.2 Memory leak Analysis Mat tool

Mat is a powerful memory analysis tool, mainly features histograms and Dominator tree.

15.3 Improve the maintainability of the program
    1. Name to standardize, to be able to correctly convey the meaning of variables or methods. Use fewer abbreviations. The prefix of the variable can be a reference to the name of the Android source code, for example, private members start with M, static members start with S. Constants are practical uppercase letters, and so on.
    2. The layout of the code needs to leave a reasonable gap to distinguish between different blocks of code. The declaration of the same kind of variables to be put together, between the two types of variables to leave a line of whitespace as a distinction.
    3. A reasonable naming style, only in very critical code to join the gaze.

[Reading notes] "Android Development Art Exploration" chapter 15th notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.