Performance Optimization for Android Development

Source: Internet
Author: User

1. http uses gzip compression to set the connection timeout and response timeout

Http requests can be divided into cache and non-cache based on business needs. In a network-less environment, some data is still browsed through the cached httpresponse for offline reading.

2. listview Performance Optimization

1). Reuse convertView

In getItemView, determine whether convertView is empty. If it is not empty, it can be reused. If you need to add listerner to the view in couvertview, the Code must be out of if (convertView = null.

2) asynchronously load images

If the item contains a webimage, it is best to load it asynchronously.

3). The image is not displayed during fast sliding.

SCROLL_STATE_FLING) When you slide the list quickly. The image in the item or the view that consumes resources can be retrieved without being displayed. The SCROLL_STATE_IDLE and SCROLL_STATE_TOUCH_SCROLL statuses are in the other two States ), the view is displayed.

3. Use the thread pool

It can be divided into core thread pool and normal thread pool, and time-consuming tasks such as downloading images are placed in the normal thread pool. This prevents time-consuming tasks from waiting for all asynchronous tasks after blocking the thread pool.

4. asynchronous tasks

It can be divided into core tasks and common tasks. Only system-level errors in core tasks can be reported. The ui operation of asynchronous tasks needs to determine whether the original activity is active.

5. Avoid using static member variables to reference instances that consume too much resources.For example, Context

6. Use WeakReference instead of forced reference

Weak references allow you to retain references to objects, and allow GC to release objects and reclaim memory when necessary. For those objects that create cheap but consume a large amount of memory, that is, you want to keep the object and use it when the application needs it. If you want GC to recycle it when necessary, you can consider using weak references.

7. Super fat Bitmap

Timely destroy (when onDestroy of Activity, reclaim bitmap)

Set a certain sampling rate

Clever Use of soft references

Drawable corresponds to resid resources, and bitmap corresponds to other resources

8. Ensure that the memory occupied by Cursor is promptly released, rather than waiting for GC to process

In addition, Android obviously prefers programmers to manually close Cursor.

9. threads are also an important source of Memory leakage.

The main cause of thread Memory leakage is the uncontrollable thread life cycle and the rational use of multithreading.

10. If the ImageView image is from the network, load it asynchronously.

11. Custom View

During custom views in application development, do not write the interaction part as a thread to constantly refresh the interface display. Instead, trigger interface updates based on the TouchListener event.

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.