I. Introduction of Caton and selection of optimization tools
Background: 1. Many performance problems are difficult to detect, but the lag is easy to feel intuitively; 2. It is difficult to locate and troubleshoot the stuck problem; A. The causes are complex: code, memory, drawing,IO? B. Not easy to reproduce: the scene at that time was strongly related; CUP Profiler: a. Graphical display of execution time, call stack, etc. B. Comprehensive information, including all threads; C. The runtime overhead is serious, the overall will slow down, may lead to biased optimization direction; Usage: debug.startMethodTracing (""); Debug.stopMethodTracing(""); Generated files in sd card: Android/data/packegename/files directly open; 2.Systrace: a. Monitor and track Api calls, thread running status, and generate Html reports; B. API18 above, TraceCompat is recommended; Advantages: Lightweight, low overhead; Intuitive reflection of CPU utilization; Give advice; 3.StrictMode a. StrictMode, a runtime detection mechanism provided by Android; B. convenient and powerful, easy to be ignored; C. Check thread policies and VM policies. Thread policy: 1. Custom slowCalls,detectCustomSlowCalls(); 2. DetectDiskReads; 3. Network operation,detectNetwork; Vm strategy: 1.Activity leak, DetectacvityLeaks (); 2. Sqlite object leak, detectLeakedSqlLiteObjects; 3. Check the number of instances,setClassInstanceLimit();Copy the code
2. Automatic stuck detection scheme and optimization
Why do you need an automated detection solution? 1. System tools are suitable for offline targeted analysis; 2. Automated detection solutions are required for online and testing links; Solution principle: 1. Message processing mechanism, one thread has only one Looper; 2. The mLogging object is called before and after each message processing; 3. The main thread is delayed and time-consuming operations are performed at dispatchMessage. 1. Looper.getmainlooper ().setMessagelogging (); 2. Matching >>>>>Dispatching, the task is executed (obtaining the stack) after the threshold time. 3. Match <<<<<Finished and cancel the task before starting. AndroidPerformanceMonitor: 1. The performance of the noninvasive monitoring component, notification form the pop-up caton information; 2. Convenient and accurate positioning to a line of code; Disadvantages: 1. The caton stack may be inaccurate; 2. As with OOM, the final stack is just a symptom, not a real problem; Optimization: Get multiple stacks in a monitoring cycle, not just the last one; StartMonitor --> High frequency collection stack -->endMonitor--> Record multiple stacks --> report; 1. Analysis: There is a high probability that multiple stacks will repeat under one holdup; 2. Solution: Hash a stack to find duplicate stacks. 3. Effect: greatly reduce the amount of display, and find the stuck stack more efficiently;Copy the code
ANR analysis and actual combat
1.ANR introduction: a. keydispatchTimeout,5s; B. roadcastTimeout, reception 10s, 60s the day after tomorrow; C. Server Timeout, foreground 20s, background 200s; d.ContentProviderTimeout,10s; 2.ANR execution process: a. Occurrence of ANR; B. The process receives an abnormal stop signal and starts to write ANR information. C. The ANR dialog box is displayed (Rom performance varies). A.a.db pull data/ ANR/localted. TXT; B. Detailed analysis :CPU,IO, and lock 4. Online ANR monitoring scheme A. Use FileOberver to monitor file changes and pay attention to higher version permissions. B. Annr-watchdog: non-invasive ANR monitoring component; Fix the problem of no permissions for higher versions; Implementation principle: start--> POST message change value -->sleep--> check whether change --> judge ANR occurs; In its own project: copy its listener, process stack information by itself, and report to the server; 5. AndroidPerformanceMonitor and ANR - WatchDog distinction: a.A ndroidPerformanceMonitor: monitoring Msg; B. Annr-watchdog: See the final result; C. The former is suitable for monitoring lag, while the latter is suitable for supplementary ANR monitoring;Copy the code
4. Caton single point problem detection scheme
Background: 1. The automatic stuck detection scheme is not enough to meet the requirements of all scenarios; 2. Systematic solutions must expose problems as early as possible; 3. Single point problem: main thread IPC,DB operation; IPC problem monitoring: 1. Monitoring index: a.IPC call type; B. Call time and times; C. Call stack, occurrence thread; Conventional scheme :IPC before and after the burying point; Inelegant, easy to forget, high maintenance costs; Adb shell am trace-ipc start adb shell am trace-ipc stop --dump-file /data/local/ TMP /ipc-trace.txt; adb pull /data/local/tmp/ipc-trace.txt; Elegant solution: 1.ARTHook or AspectJ? ARTHook: Can Hook system approach; 3.AspectJ: Non-system method; Summary: 1. Use ARTHook to improve offline tools; 2. Hook related operations, exposure and analysis of problems in the development stage; 3. Monitoring dimensions :IPC,IO,DB,View rendering;Copy the code
Five. How to realize the interface second open?
1. Interface seconds can be used as a small startup optimization; 2. Refer to the chapters of startup optimization and layout optimization; Systrace, elegant asynchronous + elegant delay initialization; Asynchronous synchronized,X2C, draw optimization; Pre-fetch page Data: A Lightweight Android AOP Framework: 1. Fast compilation speed, support incremental compilation; 2. Simple API, no extra code inserted into APK; @proxy: Hook usually used for system API calls; Insert: used to manipulate App and Library classes; Monitoring dimensions: 1. 2. Life cycle time; 3. Life cycle interval time;Copy the code
Gracefully monitor time-consuming blind spots
Background: 1. Interval of life cycle; 2. The interval between onResume and Feed display; Monitoring difficulties: 1. Only know the time of the blind spot, but do not know what they are doing; 2. Online blind areas can not be checked; Offline solution :TraceView: especially suitable for blind area monitoring within a period of time; It's clear what the thread is doing at any given time; On-line scheme thinking and analysis: 1. All methods of main thread are Msg,mLogging? No Msg stack; 2.AOP cut Handler method? Unclear exact execution time; The final implementation: 1. Use unified Handler: customize specific methods; 2. Customized Gradle plug-in, dynamic replacement at compile time;Copy the code
Seven. Summary of Caton optimization skills
1. Practical experience A. Time-consuming operations: asynchronous and delayed; B. Layout optimization: Asynchronous synchronized,X2C, redraw solution; C. Memory: reduce memory usage and reduce GC time; 2. The tool construction: system tools to know and use the a.S ystrace, TraceView, StrictMode b. Automatic monitoring and optimization: AndroidPerformanceMonitor, ANR - WatchDog; High frequency acquisition to find the stack with high repetition rate; 3. Caton monitoring tools: single point problem :AOP,Hook; 4. Monitoring indicators :a. Lag rate,ANR rate, interface opening rate; B. Interaction time, life cycle time; C. Report environment and scenario information.Copy the code
Mock interview
1. How do you do Caton optimization? The first stage: positioning and solving system tools; The second stage: automatic caton scheme and optimization; The third stage: online monitoring and offline monitoring tool construction; 2. How to automatically obtain the information of cardons? The idea comes from the Android message processing mechanism, mlogging.pringin; High frequency acquisition, find the repeated stack; 3. How does Caton's whole solution work? A. Combination of online and offline tools; B. Specific problem breakthrough: single point problem, blind zone monitoring; C. Online monitoring constructionCopy the code