Simple understanding of Android performance optimization direction and related tools

  • 2021-12-04 19:59:29
  • OfStack

Developing an application with excellent performance is a challenge that every Android developer must experience. On the premise of limited mobile resources, it is particularly important to improve the performance of applications. There are three common optimization directions to improve APP performance: layout and rendering optimization, memory optimization and power consumption optimization.

1: Layout optimization

The so-called layout optimization is to minimize the nesting level of layout and reduce useless layout. The main optimization methods are:

(1) Use RelativeLayout first to reduce the number of nested layers in the layout, otherwise use LinearLayout as much as possible. This is because RelativeLayout can complete complex layouts without nesting, while LinearLayout is preferred to improve performance when the layout is simple.

(2) Use include tags to reuse layouts, and use merge tags to merge layouts.

(3) Use ViewStub to dynamically load the view, and load it only when it is used.

(4) Streamline the layout and delete useless layout.

Common tools for layout optimization are: Hierarchy Viewer and so on.

2: Rendering optimization

Rendering optimization is mainly to improve the rendering speed, avoid over-rendering, and thus reduce the picture jamming. The main optimization methods are:

(1) Reduce layout levels and improve layout drawing speed by layout optimization.

(2) When the pixel of the same frame is drawn many times, it will cause over-drawing problem (OverDraw), which will lead to waste of resources, so it is necessary to avoid over-drawing.

(3) The system will send a synchronization signal every 16ms to render UI. If the rendering is successful every time, the FPS can reach 60, so that the user can't feel the picture jamming. This requires that time-consuming logic cannot be done in onDraw () method.

Common tools for rendering optimization are: GPU rendering mode analysis in developer options, debugging GPU over-rendering, etc.

3: Memory optimization

The most important thing in memory optimization is to avoid memory leakage. Common memory leaks are:

(1) Defining a property or control as static using Static

1) In the Activity class, a static member variable of Activity references the Activity instance. Because of the long life cycle of the static member, the Activity instance cannot be recycled when it needs to be referenced by GC. The solution is to assign the member variable to Null in the onDestroy () method of Activity.

2) If View is defined as static, because View holds context of Activity, it will also cause memory leakage. The solution is to call onDestroy () method of View at the same time when reclaiming Activity instance to release View.

(2) Non-static inner classes and anonymous inner classes cause memory leakage

Because non-static inner classes and anonymous inner classes hold strong references to external classes by default, memory leaks can occur when the life cycle of internal classes is longer than that of external classes. Common examples are memory leaks caused by Handler internal classes. The workaround is to define the inner class as static, because the static inner class has no reference to the outer class. If the inner class wants to access the members of the outer class, it can be done by weak reference.

Commonly used memory leak detection tools are: Leakcanary, MAT and so on.

4: Power optimization

Power optimization is mainly to reduce the power consumption of applications and unnecessary network requests.

5: Other optimizations

Such as reducing the size of the installation package, avoiding time-consuming operations in UI threads, and so on.

6: Other tuning tools

IDE comes with static code detection tools, various AndroidMonitor;; traceView of jdk, etc.


Related articles: