Introduction to memory optimization and tool selection
1). Memory jitter: jagged and stuck due to GC; 2). Memory leakage: reduced available memory, frequent GC; 3). Memory overflow :OOM, abnormal program; 2. Tool selection 1).Memory Profiler a. B. Identify memory leaks, jitter, etc. C. Provides the ability to capture heap dumps, force GC and track memory allocation features: convenient and intuitive; Offline normal use; 2).mat a. Powerful Java Heap analysis tool to find memory leaks and footprint; B. Generate overall reports and analyze problems; C. Offline in-depth use; 3).LeakCanary a. Automatic memory leak detection; B. Offline integration (OOM is easy to occur online);Copy the code
Android memory management mechanism
1. Java memory allocation method area local method stack stack program counter 2. Java memory reclamation algorithm 1). Mark - clear algorithm: mark stage: mark objects that can be recycled; Cleanup phase: Reclaim space occupied by marked objects; Android memory management mechanism 1. Memory allocation is elastic. The maximum value and value of memory allocation depend on the device. 2. In OOM scenario, the memory is insufficient. The differences between Dalvik and ART are as follows: 1.Dalvik only fixes a recovery algorithm; 2.ART recovery algorithm can be selected during operation; 3.ART has the ability to organize memory and reduce memory holes; Low Memory Killer 1. B. Visible process; C. Service process; D. Background process; F. Empty process, etc. 2. Reclaim revenueCopy the code
Three. Memory jitter actual combat
Definition: Memory is unstable due to frequent allocation and reclamation. Performance: Frequent GC, zigzag memory curve; Hazards: Lag,OOM memory jitter OOM 1. Frequently create objects, resulting in insufficient memory and fragmentation (discontinuous). 2. Discontinuous memory chips cannot be allocated, causing OOM. Actual combat: 1. Preliminary investigation using Memory Profiler; 2. Use Memory Profiler or CPU Profiler in combination with code inspection;Copy the code
Four. Memory leakage actual combat
Definition: objects in memory that are no longer useful; Performance; Memory jitter causes the available memory to decrease. Hazards: Insufficient memory, frequent GC,OOM; Solution: Initial observation using Memory Profiler; Confirm through MAT and code;Copy the code
ARTHook elegantly detects unreasonable images
1). Before API10, Bitmap itself exists in Dalvik Heap, and pixels are in Native (not automatically reclaimed); 2). After API10, Bitmap itself and pixels are stored in Dalvik Heap; 3). Pixels after API26 are in Native (notify Native layer to retrieve pixels); 2. Obtain the memory occupied by Bitmap. 1).getByteCount; 2). Width * height * one pixel occupies memory (note: * compression ratio is required when placed in RES); 3. Conventional background: picture is very important for memory optimization, picture width is higher than the control width and height: inherit ImageView, copy to calculate the size; Features: strong intrusion not general; ARTHook acts as a hook that hooks additional code to the original method and modifies the execution logic. Inserting pile during operation; Performance analysis; Epic: is a virtual machine level, Java Method granularity runtime Hook framework; Support Android4.0-9.0 features: non-invasive; Strong versatility; Compatibility problems, open source solutions can not be brought to the online environment;Copy the code
Six. Thread memory monitoring scheme
Dump: debug.dumphprofData (); General implementation process: over 80% of the maximum memory -> memory Dump-> return file ->MAT Manual analysis features: 1).Dump file is too large, which is positive related to the number of objects and can be trimmed; 2) upload failure rate is high and analysis is difficult; 3). With certain strategies, there is a certain effect; 1). Bring LeakCanary to the line; 2). Preset leak suspect point; 3). Leakage return is found; Features: Not suitable for all scenarios, must set a point of doubt; Analysis is time-consuming and prone to OOM;Copy the code
Vii.LeakCanary Principle:
1. Monitor life cycle,onDestroy add RefWatcher detection; 2. Confirm the memory leak twice. 3. Analyze leakage and find reference chain; 4. Monitoring component + analysis component; LeakCanary Customise: Default suspect point --> Automatically find suspect point; Slow parsing of leaked links --> Parsing a large Retain size object; OOM--> object clipping, not all loaded into memory;Copy the code
Viii. Complete scheme of online monitoring
Standby memory, key module memory,OOM rate; GC times and GC time of overall and key modules; Enhanced LeakCanary automated memory leak analysis;Copy the code
Ix. Optimize the overall direction
1. Memory leakage 2. Memory jitter 3Copy the code
Ten. Memory optimization simulation interview
1. How is the process of memory optimization project done? A. Analyze the current situation and identify problems; B. Targeted optimization; C. Efficiency improvement; 2. What is the biggest feeling of memory optimization? A. Use of learning tools; B. Technical optimization must be combined with business code; C. Systematic improvement of solutions 3. How to detect all unreasonable places? ARTHook; Emphasize differences;Copy the code