Android Performance Optimization Chapter

Source: Internet
Author: User

Many apps encounter several common performance issues:

Slow start-up speed, slow interface jump, slow event response, slippage and animation lag.

First, start speed optimization.

To optimize the initialization of the task:

1. Lazy-loading initialization of some initial tasks

2. Parallelization of initialization tasks (asynchronous)

3. Enable the initialization task to be pluggable (one task problem does not affect other tasks)

Other:

1. Control the number of threads, pay attention to the use of threads, replace the thread pool of the three-party or two-party SDK with your own thread pool, the thread consumes too much CPU resources,

2. Use cache to reduce I/O operations (read database, read file, sharedpreference) reduce network requests (judging no network direct return)

3. The sharedpreference has been specifically optimized to reduce the size of individual files and to separate the unrelated storage key values into different files.

and prevent large chunks of data from being stored in sharedpreference, which is not conducive to performance or memory,

Because Sharedpreference will have an extra copy of the cache for a long time to exist.

4. Activity uses hot start, press back key, do not destroy activity, release some pictures and other memory-intensive resources

Second, the interface optimization

Complex layout, transition drawing, activity main functions time consuming, content display slow, interface re-layout (layout), GC number

These problems can cause the interface to stutter.

1. Over-rendering

Activity uses the default theme background, in Layout view to minimize background settings (with foreground content, do not set and show the background),

Reduce multi-layer layout structure background settings to reduce drawable complex shape use.

2. Optimize the layout hierarchy

The more tiers, the more time it takes to measure and layout, and the time to create a hardware list increases accordingly.

Sometimes we embed a lot of layouts to implement functionality that would otherwise be possible with a simple layout, and sometimes add layouts that will be used by the test phase.

Use the merge label or viewstub tag to optimize overall layout performance by removing useless hierarchies. For example, some display error interface, loading cue box interface, etc.

These layouts that you do not have to display can use the viewstub tag to improve performance. In addition, the flexibility to use the layout, not the more hierarchy will be the worse performance,

Sometimes a 1-layer relativelayout can perform worse than a 3-layer nested linearlayout.

In addition to the flexible use of layouts, we also pre-initialize the layout by pre-inflate and making some necessary inflate in the thread.

Reduce the time-to-live display. For some complex layouts, reuse the pool and reduce the performance loss caused by inflate, especially in the list.

3. Accelerated interface Display

1. Reduce the GC pauses of the main thread, because the heap is locked even if the parallel GC, if the main thread requests to allocate memory, it will also be suspended.

Avoid allocating more and larger objects frequently in OnDraw, removing edgeeffect effects of controls such as ListView, ScrollView, and so on to reduce memory allocations and speed up control creation time

2. The TraceView tool can be used to identify the time-consuming operation of the main thread and the number of operations and other time-consuming threads and optimizations;

3. For network requests, do a good job of local cache, in slow or no network time to display, reduce unnecessary data protocol fields, reduce the name length, etc., and for compression.

You can also speed up the transfer parsing time by paging through the data. Because the larger the JSON, the longer the transfer and parsing time, the more memory object allocations will be thrown;

4. Note the priority of the thread, and the priority of the thread for functions that take up more time on the CPU.

5. Override method Requestlayout, onsizechanged, block too many layout requests if the size does not change.

4. Reduce GC

1. Reduce the allocation of objects, to find out the unnecessary allocation of objects, such as the use of non-wrapper type, the use of packaging type, string of the + number and expansion, reduce handler.post (Runnable R) and other frequent use.

2. Reuse of objects requires the use of a reuse pool for frequently allocated objects.

3. Release references to useless objects as early as possible, especially large objects and collection objects, and recycle them in a timely manner by placing null.

4. To prevent leakage, in addition to the most basic files, streams, databases, network access to remember to close and unregister their own registration of some events, but also to minimize the use of static variables and single cases.

5. Controlling the use of the Finalize method, using a rewrite of the Finalize class in a high-frequency function, can aggravate the GC burden, making several times the difference in performance.

6. Reasonable selection of containers, priority in the performance of the array, even if we are accustomed to the use of containers, but also pay attention to the frequent use of containers in the performance of the hidden point: the first is the expansion overhead, hashmap expansion when the cost of re-hash is larger. The second is memory overhead, HashMap requires additional Map.entry object allocations, requires additional memory, and is prone to more memory fragmentation. Sparsearray and ArrayList have more advantages in memory. Again, a foreach loop should not be used for traversal of a container such as arrylist that implements the Randomaccess interface.

5. Use performance analysis tools to collect data, analyze data, and identify performance optimization points

The memory monitor is used to see the fluctuations and the GC, and the allocation of memory can be observed through the allocation tracker tool, and the allocation of many small objects is found.

Use the trace for OpenGL tool to find points on the interface that lead to hardware acceleration, such as the processing of some rounded pictures.

Using the TraceView analysis method takes time, turns on GPU over-drawing monitoring, and GPU rendering mode uses the Systrace analysis interface to render problems.

6. Optimize points when encoding

Use the final keyword flexibly, avoid using try catch in loops, minimize enum types, and use temporary variables instead of This.member

7. Test Start time mode

Log mode at OnCreate and Onwindowfocus to record when activity starts to display the interface

You can use the Hock method to record the startup time, test it using the test site,

Android 5.0 above uses ADB shell am start-w to test the time.

Android Performance Optimization Chapter

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.