Summary of performance optimization techniques for Android programming development
This paper summarizes the performance optimization skills of Android programming development in detail. Share with you for your reference, as follows:
1. HTTP is compressed with gzip, and the connection timeout and response timeout are set
HTTP requests are divided into whether they can be cached or not according to business requirements. In a network free environment, some data can still be browsed through the cached httpresponse to realize offline reading.
2. Listview performance optimization
1) . multiplexed convertview
In getitemview, judge whether convertview is empty. If not, it can be reused. If the view in couvertview needs to add Lister, the code must be outside if (convertview = = null) {}.
2) . load pictures asynchronously
If webimage is included in the item, it is better to load it asynchronously
3) . don't show pictures when sliding quickly
When scrolling_state_flying, the picture in item or the view that needs to consume resources can not be displayed; In the other two states (scroll_state_idle and scroll_state_touch_scroll), those views are displayed
3. Use thread pool
It is divided into core thread pool and ordinary thread pool. Time-consuming tasks such as downloading pictures are placed in the ordinary thread pool to avoid that all asynchronous tasks must wait after time-consuming tasks block the thread pool
4. Asynchronous tasks
It is divided into core tasks and ordinary tasks. Only system level errors in core tasks will be reported. The UI operation of asynchronous tasks needs to judge whether the original activity is active
5. Try to avoid static member variables referencing instances that consume too much resources, such as context
6. Use WeakReference instead of strong reference
Weak references allow you to keep references to objects while allowing the GC to free objects and reclaim memory when necessary. For those objects that are cheap to create but consume a lot of memory, that is, if you want to keep the object and use it when the application needs it, and if you want GC to recycle it when necessary, you can consider using weak references.
7. Super fat bitmap
Timely destroy (when the activity's ondestroy, the bitmap will be recycled)
Set a certain sampling rate
Clever use of soft quotation
Drawable corresponds to the resource of resid, and bitmap corresponds to other resources
8. Ensure that the memory occupied by cursor is released in time instead of waiting for GC to process
And Android obviously prefers programmers to close the cursor manually.
9. Threads are also an important source of memory leaks
The main reason for thread memory leakage is that the thread life cycle is uncontrollable and multithreading is used reasonably.
10. If the image of ImageView comes from the network, load it asynchronously
11. User defined view
When customizing the view in application development, do not write the interactive part as a thread to constantly refresh the interface display, but actively trigger the update of the interface according to the touchlistener event
I hope this article will help you in Android programming.