Android knowledge points finishing (5)

"Android Advanced Advanced" study notes
first five performance optimization articles


Chapter 33: Code Optimization

1. The choice of data structure

  SparseArray is an implementation of a sparse array unique to the Android platform. It is a mapping from Integer to Object. It can be used to replace HashMap<Integer, <E>> in specific situations to improve performance. Its core implementation is a binary search algorithm.

  SparseArray has the following four categories:

SparseBooleanArray          //用于替代HashMap<Integer, Boolean>
SparseIntArray              //用于替代HashMap<Integer, Integer>
SparseLongArray             //用于替代HashMap<Integer, Long>
SparseArray<String>         //用于替代HashMap<Integer, String>
  • SparseArray is not thread-safe.

  • Due to binary search, SparseArray will insert the inserted data in the order of Key value size.

  • SparseArray is optimized for deletion. It will not delete this element immediately, but will try to use it later by setting the flag (DELETE).

2. Correct usage of Handler and inner classes

  HandlerIs working with Looperand MessageQueuetogether. In Android, after an application is started, the system will create a Looper object serving the main thread by default. The Looper object is used to process all Message objects of the main thread. Its life cycle runs through the entire application life cycle. The Handler used in the main thread will be bound to this Looper object by default. When the Handler object is created in the main thread, it will be immediately associated with the MessageQueue of the Looper object of the main thread. At this time, the Message object sent to the MessageQueue will hold a reference to the Handler object, so that it can call back to the handleMessage of the Handler when the Looper processes the message. method.

  In the Activity, Handler should be declared as static inner classes , rather than non-static anonymous inner classes , otherwise it will cause a memory leak, because non-static anonymous inner class will hold an external class references implicit, but not static inner classes Will hold references to external classes.

3. ContextTypes

According to the different components and uses of Context, Context can be divided into the following types:

  1. Application: The default singleton class in Android applications

  2. Activity/Service: These two classes are subclasses of ContextWrapper

  3. BoadcastReceiver: not a subclass of Context

  4. ContentProvider: Not a subclass of Context

4. Four types of references in Java

  • Strong Reference : One of the most widely used in Java and the default reference type for objects. If an object has a strong reference, then the garbage collector will not reclaim it. If the internal slave space is insufficient, OutOfMemoryError will be thrown.

  • Soft Reference : When the content space is sufficient, it will not be reclaimed; when the internal slave space is insufficient, it will be reclaimed. If used in conjunction with a reference queue (ReferenceQueue) , when the object pointed to by the soft reference is recycled by the garbage collector, the Java virtual machine will add the soft reference to the reference queue associated with it.
      Soft references can be used to implement memory-sensitive caches.

  • Weak Reference : Regardless of whether the memory space is sufficient, when the garbage collector scans an object with a weak reference, it will be recycled. Weak references can also be used in conjunction with a reference queue. When the object pointed to by the weak reference is reclaimed by the garbage collector, the Java virtual machine will add the weak reference to the reference queue associated with it.
      Weak references can be used to prevent memory leaks in callback functions. Because the callback function is often an anonymous inner class, it implicitly saves a reference to the outer class, so if the callback function is called back in another thread, and if the outer class needs to be recycled at this time, then the memory will leak, because the anonymous internal The class holds a strong reference to the external class

  • Phantom Reference : Essentially a marked reference, it is mainly used to track the activity of objects being garbage collected. Phantom reference must be used in conjunction with the reference queue. When the object pointed to by the phantom reference is reclaimed by the garbage collector, the Java virtual machine will add the phantom reference to the reference queue associated with it.

Chapter 34: Picture Optimization

1. The format of the picture

  The image formats natively supported by the Android platform are: JPEG, PNG, GIF, BMP and WebP (starting with Android 4.0), but there are only three codec formats that can be used in Android application development: JPEG, PNG and WebP.

2. 9Patch format

  The left side represents the vertically stretched area of ​​the image, the upper side represents the horizontally stretched area, the right side represents the vertical area where the content is drawn , and the lower side represents the horizontal area where the content is drawn. The right and bottom lines are optional, and the left and top lines cannot be omitted. Essentially it is still a picture in PNG format.

Chapter 35: Battery Optimization

  • Cancel BroadcastReceiver, positioning and other functions in time;

  • The interval of AlarmManager (wake-up operation) should not be too short, cancel it in time;

  • WakeLock (keep the device awake) release the lock in time

Chapter 36: Layout Optimization

1. Use the include tag to share the layout

2. Use ViewStub tags to implement lazy loading

3. Use the merge tag to reduce the layout level #3

4. Try to use CompoundDrawable

5. Use Lint

Chapter 37: Network Optimization

1. Avoid DNS resolution

2. Consolidate network requests

3. Get data in advance

4. Avoid polling

5. Optimize the reconnection mechanism

6. Offline cache

  Use LruCache to implement memory caching, and DiskLruCache to implement external memory caching.

7. Compressed data size

8. Different network environments use different ## timeout strategies

9. Use of CDN

  The full name of CDN is Content Delivery Network, that is, content delivery network. The basic idea is to avoid links on the Internet that may affect the speed and stability of data transmission to make content transmission faster and more stable.

Guess you like

Origin blog.csdn.net/michael_f2008/article/details/77946337