Android Performance Optimization Tips

Source: Internet
Author: User

Official Google Tips

Here are some key points to keep in view http://developer.android.com/training/articles/perf-tips.html
1.0 Basic Principles
Good algorithms and data structures are the first choice to dramatically improve performance
Two basic principles: don't do what you don't need to do; don't allocate memory that you can avoid
1.1 Avoid creating unnecessary objects, paying particular attention to the creation of temporary objects
1.2 If you do not need to access the object's member variables, use a static method. Calling a static method can be as fast as 15%-20%.
1.3 Constants use the Staticfinal declaration. The final declaration does not need to be found directly in the Dex file in the class initialization method, and the reference is given to it (this optimization applies only to the underlying type and string)
1.4 internally avoid using the Getter,setter method and call the member variable directly. No JIT, direct access back 3 times times faster than get, no JIT, 7 times times faster.
1.5 with enhanced for loop. ArrayList with enhanced for can be 3 times times faster, the rest of the collections almost
1.6 Consider using package access rights (default) in place of private access rights when internal classes access members outside the class. Because the VM considers the outer and inner classes to be two different classes, and one class cannot access the private members of another class, while the Java language prescribes that the inner class can access the private members of the external class, in order to be consistent, the compiler automatically generates a series of static methods that access the private members of the outer class for the inner class. When an inner class accesses a private member of an external class, it is automatically called, and the above conclusions are drawn according to 1.4.
1.7 Avoid using Float,float on the Android platform is twice times slower than integers. float and double performance can be similar, just double the space is twice times float, on the current machine assumes that the space is not a problem, so with floating point number when using double instead of float.
1.8 Familiarize yourself with and use the library provided by the system. For example, System.arraycopy () can be 9 times times faster than handwritten copy on a JIT-capable machine.
1.9 Use the native method with caution. Native is more efficient than Java. The most important use of the native code is on Android, based on existing native code.

Some tips in development

1.http with gzip compression, set the connection time-out and response timeout
The HTTP request is divided into whether it can be cached and not cacheable according to the business demand, then, in the network-free environment, the cached HttpResponse is still used to browse some data and realize offline reading.

2. Use a thread pool, divided into core thread pool and normal thread pool, download pictures and other time-consuming tasks placed in the normal thread pool, to avoid time-consuming tasks blocking the thread pool, resulting in all asynchronous tasks must wait
3. Try to avoid static member variables referencing resources that are consuming too many instances, such as Context,view, which can lead to memory leaks

4.listview Performance Optimization
1). Loading pictures asynchronously
If the item contains Webimage, it is best to load asynchronously

2). Do not show pictures when sliding quickly
When a quick slide list (scroll_state_fling), the picture in item or the view that needs to consume resources, can not be displayed, but in the other two states (Scroll_state_idle and Scroll_state_touch_ SCROLL), the view is displayed

3). Multiplexing Convertview, using Viewholder
In Getitemview, determine if the Convertview is empty, and if it is not empty, it can be reused. If the view in Couvertview needs to add Listerner, the code must be outside the if (Convertview==null) {}. Save time by calling Findviewbyid () to get the View object with Viewholder saving per GetView

4). Baseadapter Avoid memory overflow
If the Baseadapter entity class has properties that consume very much memory, it can be saved to a file; For improved performance, you can cache and limit the cache size.

6. Ensure that the memory that the cursor occupies is released in a timely manner, and that the cursor must remember close () instead of waiting for the GC to handle it, not close may cause a memory leak, notice close in finally.

7. Asynchronous tasks, divided into core tasks and common tasks, only the core tasks in the system-level errors will be error, the UI operation of the asynchronous task needs to determine whether the original activity is active;

8. Using WeakReference instead of a strong reference, a weak reference allows you to keep a reference to the object while allowing the GC to release the object and reclaim memory if necessary. Consider using weak references for those objects that create inexpensive but expensive memory, want to keep the object, and use it when the application needs it, while you want the GC to be recycled as necessary.

9. Super Big Fat Bitmap Timely destruction (activity OnDestroy will bitmap recovery, after being used by the UI component to recycle it will throw RuntimeException:Canvas:trying to use a recycled Bitmap android.graphics.Bitmap) Set a certain sample rate (the developer provides the picture without sampling, for the user upload or third-party size of the uncontrolled picture, can be sampled to reduce the memory of the image), from the server to return pictures, It is suggested that the size of the feedback picture cleverly apply the soft reference drawable corresponding to Resid resources, bitmap corresponding to other resources of any type of picture, if not obtained (for example, the file does not exist, or read the file run OutOfMemory exception), There should be a corresponding default picture (the default image is placed in the APK, obtained by RESID);

10.Drawable UI components need to use the picture is the APK package comes with, then all use Setimageresource or setbackgroundresource, and not according to ResourceID
Note: Get (Getresources (), R.drawable.btn_achievement_normal) This method is converted to drawable by RESID, you need to consider the issue of recycling, if drawable is an object private object, The memory is definitely not freed until the object is destroyed.

11. Reuse, recycling activity objects temporary activity timely finish the main interface is set to Singletask general interface set to Singletop.

12. Set the power management of the interface when Onresume, and cancel the setting when OnPause.

13. In the application development of the custom view, the interactive part, do not write thread constantly refresh the interface display, but based on the Touchlistener event active trigger interface update.

14. If the ImageView picture is from the network, it is loaded asynchronously. Use Pascal or Universal Image Loader Library

15. Location information to obtain the user's geo-location information, when the need to obtain data to open the GPS, and then promptly shut down.

Good Articles with some performance optimizations

Android Performance Optimization Model
Introduction to the Android System Performance tuning tool
Http://androidperformance.com

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Performance Optimization Tips

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.