Android Development experience Share (5) Android Optimization summary

Source: Internet
Author: User

When will not be used in the project optimization, and now some of the optimization of the small experience to summarize

1, the evil of the static

Static is a good thing, declaring the invocation of an assignment to be simple and convenient, but with the accompanying performance problem. Since the life cycle of the static declaration variable is actually the same as the life cycle of the app, it is somewhat similar to application. If a large number of uses, it will occupy the memory space is not released, a few will also cause the memory of the constant overhead, until hung out. The rational use of static is generally used to modify the basic data types or lightweight objects, to avoid repairing the collection or large objects, commonly used to decorate global configuration items, tool class methods, internal classes.

2. Irrelevant references

In many cases, we need to use a pass-through reference, but we cannot ensure that the reference is passed out in a timely manner. For example, more representative of the context leaks, in many cases, when the activity is over, because the other objects are still pointing to cause the delay can not be recycled, which caused a memory leak. The third suggestion can be considered at this point.

3. Use Softreference/weakreference/lrucache

There is no such mechanism in Java, Android, when the memory is tight or GC sweep, it can be in time to release some memory, so as to allocate to the place to be allocated. The answer is yes, and Java provides us with two of solutions. If you are concerned about the memory overhead of the app, you can consider using WeakReference, when GC reclamation swept through this area of memory will be recycled, if not so concerned, you can use SoftReference, it will be automatically released in case of insufficient memory request, It can also solve the oom problem. Android since 3.0 also launched the LRUCache class, using the LRU algorithm to free memory, the same can solve oom, if compatible with 3.0 version, please import v4 package. On the question of irrelevant references to the second article, we can consider using WeakReference to wrap the message.

4, cautious handler

Handler + thread is a good choice when it comes to handling asynchronous operations. But believe that when using handler, everyone will encounter a warning situation, this is lint for the developer's reminder. Handler runs on the UI thread, constantly processes messages from MessageQueue, and if handler has a message to process but the activity page has ended, the activity's reference is not actually recycled, which creates a memory leak. Solution, one that is called in the activity's OnDestroy method

Handler.removecallbacksandmessages (NULL), cancels all processing of messages, including pending messages, and second, declares that the inner class of handler is static.

5. Bitmap Ultimate Killer

The improper handling of bitmap is likely to cause oom, most of which is due to this cause. Bitamp Bitmap is a well-deserved fat kid in Android, so be careful when it comes to operation. Since DALIVK does not take the initiative to recycle, developers need to recycle out when bitmap is not being used. In the process of use, timely release is very important. At the same time, if the requirements allow, you can also go to bitmap for a certain scale, through the Bitmapfactory.options Insamplesize property control. If you just want to get bitmap properties, you don't need to allocate memory based on bitmap pixels, just use Bitmapfactory.options's Injustdecodebounds property when parsing the BMP. Finally, we recommend that you load the network pictures, using soft references or weak references and local cache, recommended to use Android-universal-imageloader or xutils, cattle produced, will be a boutique. A few days ago in the "Custom control (c) inherit the control", also organized a, you can go to github download to see.

6. Cursor closed in time

When querying the SQLite database, will return a cursor, when the query is complete, close immediately, so that the query result set can be retrieved in a timely manner.

7. Page background and picture loading

When setting the background and picture in the layout and code, if it is a solid color, use color as much as possible, if it is a regular graphic, use shape to draw, if it is slightly more complex, you can use 9patch graph, if you cannot use 9patch, the model for several main resolutions is cut.

8. The item cache for ListView and GridView

For mobile devices, especially the hardware of the uneven Android ecosystem, page rendering is actually very time-consuming, Findviewbyid is quite slow. So do not reuse view, in the list when it is particularly significant, often the phenomenon of sliding very card. Refer to the historical article "another way of saying Viewholder"

9, Broadcastreceiver, Service

To bind broadcasts and services, be sure to remember to unbind them when they are not needed.

10. I/O flow

I/O stream operation finished, read and write end, remember to close.

11. Threads

When the thread no longer needs to continue to execute the time to remember to close, open the number of threads is not easy to too much, generally with their own machine core number of the best, recommended to open threads, the use of thread pool.

12, String/stringbuilder

It is recommended to use StringBuilder when more characters are needed for stitching.

Android Development experience Share (5) Android Optimization summary

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.