I. High performance App standards: fast, stable, low cost, small

1. Fast: avoid lag, fast response speed, reduce user waiting time, meet user expectations; 2. Stability: reduce crash rate and ANR rate, and avoid crash and no response during user use; 3. Saving: save the flow and power consumption, reduce the user’s cost, avoid the hot mobile phone; 4. Small: Small installation package can reduce the installation cost of users;

Two. Optimization range: caton, memory, power consumption, network, APK and other aspects of optimization

1. Caton optimization:

A. Caton scene:

1.UI drawing and refreshing; 2. Start (installation start, cold start, hot start); 3. Jump: switching between pages, switching between front and back; 4. Response: button, system event, sliding;Copy the code

B. Caton root cause:

1). Layout is complex, rendering cannot be completed within 16ms, resulting in frame loss; 2) overdrawing of.view results in some pixels being drawn multiple times in the same frame; 3). The animation is executed for too many times at the same time, resulting in excessive CPU or GPU load; 2. Data processing: 1). Time-consuming operations are processed in the UI thread; 2). Frequent GC operations;Copy the code

C. Caton optimization:

1. The layout optimization: 1). Reasonable use LinerLayout, RelativeLayout, FrameLayout, ConstraintLayout layout container etc. 2). Use <include> tags for layout reuse; 3). Use the <merge> tag to remove redundant levels; 4). Use ViewStub to improve loading speed; A. Causes: 1. In XML layout, the controls overlap and have setting backgrounds; 2. 2.View's onDraw() method draws multiple times in the same area; B. Solution: 1. Remove unnecessary background; 2. In the onDraw() method of the custom View, use Canvas.cliprect () to specify the drawing area to prevent overdrawing of overlapping components; 2. Startup optimization: 1).UI layout: The application usually has a splash screen page. Optimize the UI layout of the splash screen page and detect frame loss through Profile GPU Rendering; 2). Startup loading data logic optimization: partial loading, asynchronous loading and delayed loading strategies can be adopted to improve the startup speed of the application; 3). Data preparation: data initialization analysis, thread initialization and other strategies can be considered when loading data; 3. Reasonable refresh mechanism: 1). Minimize the refresh times; 2). Try to avoid running high CPU threads in the background; 3). Narrow the refresh area; 4. Others: When realizing animation, it is necessary to choose appropriate animation frame according to different scenes; In some cases, hardware acceleration can be used to improve fluency;Copy the code

D. Analysis and solution tools:

1.Profile GPU Rendering: used to find problematic Rendering interfaces; 2.Systrace: Used for performance data sampling and analysis: 1. Tracing system I/O operations; 2. Kernel work queue; 3.CPU load; 4. Operation of various subsystems, etc.; 5. Provided analysis data for UI display performance, such as poor animation playback and slow rendering; 3.Traceview: Used for data collection and analysis to obtain two types of data :1. A method that takes time for a single execution; Hierarchy Viewer: Used to check the nesting and drawing time of the layout; 5.Android Lint: Code scanning tool that uses static code inspection to find potential problems in code and give optimization suggestions;Copy the code

2. Memory optimization:

A. Ndroid Memory management mechanism:

1.Java object life cycle: Create -> Apply -> Invisible -> Unreachable -> Collect -> End -> Object space reallocation; 2. Memory allocation: It is actually the allocation and release of the heap. In order to control the memory control needs of the whole system, the system sets a maximum threshold for each application. 3. Memory collection mechanism: New generation (Edean,Survivor(form,to)), old generation, GC, garbage collection algorithm;Copy the code

B. Memory leak:

1. Root cause: 1). Simple description: A long-life object holds a reference to a short-life object, causing GC to fail to reclaim the object; 2). Essential understanding: Useless objects are reachable to GC Roots (objects are referenced), so GC cannot reclaim the object; Singleton/static variable: static and its life cycle is the same as the life cycle of the Application. If Context is available, set it to the Application Context. 2). Anonymous inner classes/non-static inner classes hold references to external classes: Handler uses static inner classes, using weak references; Rxjava, etc. 3). Resources are not closed :IO operation,Cursor, broadcast,EventBus, etc. 4). Collection class: not cleaned in time when not in use; 5).WebView: Memory will not be released once it is used. Solution: Open a separate process for WebView and use AIDL to communicate with the main process of the application;Copy the code

C. Memory jitter:

1. Root cause: Objects are frequently created in a short period of time (possibly in a loop), and memory is frequently GC to deal with this; Frequent GC will produce a large amount of pause time, which will reduce the interface drawing time and make the drawing time of a frame for many times exceed 16ms, resulting in the interface stalling phenomenon.Copy the code

D. Memory overflow:

1. The root cause is that the program does not have enough memory space when applying for memory. How to avoid OOM: 1). Use more lightweight data structures: If you use ArrayMap/SparseArray instead of HashMap,HashMap is more memory intensive because it requires additional instance objects to record the Mapping operation. SparseArray is more efficient because it avoids automatic boxing of Key values and unboxing after boxing. 2). Static constants or @intdef annotations can be used instead of enumerations; 3).Bitmap optimization: a. Size compression: Use InSampleSize to set the appropriate size b. Color quality: Set the appropriate format, ARGB_6666/RBG_545/ARGB_4444/ALPHA_6, which varies considerably. C.nbitmap: Using the inBitmap attribute tells the Bitmap decoder to try to use an existing memory region. The newly decoded Bitmap attempts to use the pixel data memory area occupied by the previous Bitmap in the Heap, rather than asking memory to re-allocate a space to hold the Bitmap. Using this feature, even thousands of images only need to take up the memory size of the number of images that can be displayed on the screen. However, there are some limitations on reuse, which are embodied in: Prior to Android 4.4, you could only reuse the memory of a Bitmap of the same size, as long as the later Bitmap was smaller than the previous one. Before using the inBitmap parameter, each Bitmap object is allocated a chunk of memory for its use. After using the inBitmap parameter, multiple bitmaps can reuse a chunk of memory, which improves performance. 4).StringBuilder instead of String: It is necessary to use StringBuilder instead of the frequent "+" in code where a lot of String concatenation is required; 5). Avoid creating objects in methods like onDraw, which can quickly take up a lot of memory and cause frequent GC and even memory jitter; 6). Reducing memory leaks is also a way to avoid OOM;Copy the code

E. Optimize memory space:

1. Object reference: Reasonable use of strong, soft, weak and virtual reference types according to service requirements; 2. Reduce unnecessary memory overhead: pay attention to automatic packing, increase memory overcommitment, such as: effective use of the system's own resources, view reuse, object pool,Bitmap object reuse; 3. Use the best data types: ArrayMap data structure, avoid using enumerated types, use cache Lrucache, etc. 4. Image memory optimization: bitmap specification can be set, compression can be done according to the sampling factor, and some image caching can be used for image management.Copy the code

F. Memory analysis tools:

Profiler is used to observe the temperature of memory, network,CPU, etc. 2). Can identify memory leaks and memory jitter caused by application stutter,ANR and crash; 3). It can generate charts to view memory usage, and can also force memory reclamation and track memory allocation; 2.MAT 1). Generate heap storage files in hprof format through DDM or Memory Monitor; 2). Determine whether memory leak occurs by in-depth analysis of hPROF generated when suspected memory occurs; 3.LeakCanary: A memory leak plug-in developed based on MAT;Copy the code

Power consumption optimization — stay tuned 4. Network optimization — stay tuned 5.Apk optimization — stay tuned