1. Core principle (how the recorder works)
-
Main thread monitoring:
-
Heartbeat detection:Every time
16ms
(One frame time) Check whether the main thread is "alive" (using messages normally). -
Stop judgment: If the "heartbeat" is not completed multiple consecutive times (if it exceeds
200ms
), an alarm is triggered.
-
Heartbeat detection:Every time
-
Stack crawl:
- Timed sampling: When it is stuck, it continuously grabs the stack information of the main thread and restores the "campus scene".
2. Specific implementation plan (three-step recorder installation)
1. Based on the main thread Looper (listening message processing)
Hook Looper log printing:
().setMessageLogging { msg -> if ((">>>>>")) startTimer() // Message processing begins else if (("<<<<<")) stopTimer() // Message processing ends}
Timeout determination:
private val watchdog = Timer() private fun startTimer() { (object : TimerTask() { override fun run() { // Timeout not completed → Triggering lag reportBlock() } }, 200) // 200ms timeout}
2. Based on Choreographer (frame rate monitoring)
Listen to frame callbacks:
().postFrameCallback(object : { override fun doFrame(frameTimeNanos: Long) { val frameCost = (() - frameTimeNanos) / 1_000_000 if (frameCost > 16) { ("Block", "One frame takes time:${frameCost}ms") } // Continue to monitor the next frame ().postFrameCallback(this) } })
3. Open source library integration (off-the-shelf recorder)
BlockCanary(recommend):
// Initialization(this, AppBlockCanaryContext()).start()
- Advantages: Automatically record the stuttering stack, supporting email/DingTalk alarms.
Matrix-TraceCanary(Open source in Tencent):
// Configurationval tracePlugin = TracePlugin(config) (config)
3. Data collection and reporting (analyze accident video)
-
Key information collection:
- Stack snapshot: The method call chain where the main thread is stuck
- Equipment Information: Model, system version, memory status
- Context data: User operation path, network status
-
Reporting strategy:
-
Sample report: Collect only
10%
user data to avoid waste of traffic. - Aggregation Analysis: Merge the same problem by stack feature to reduce duplication.
-
Sample report: Collect only
Sample log format:
{ "block_time": 320, "stacktrace": [ "(:22000)", "(:30)" ], "device": "Xiaomi 12, Android 13", "session_id": "a1b2c3d4" }
4. Pit avoidance guide (record camera does not overturn)
-
Avoid stuttering caused by monitoring itself:
- Stack acquisition is processed asynchronously and does not occupy the main thread.
-
Stack deduplication and filtering:
- Ignore system methods (e.g.
), focus on business code.
- Ignore system methods (e.g.
-
Low battery/backend mode optimization:
- Reduce sampling frequency in the background and reduce power consumption.
-
Compatibility processing:
- Bypass the manufacturer's custom ROM Looper modification (such as Huawei EMUI).
5. Effect display (the recorder has made meritorious)
Stop scene | Stack Positioning | Repair plan |
---|---|---|
Main thread parses large JSON |
() 300ms |
Cut thread analysis + result cache |
Database query not optimized |
() Blocking |
Index optimization + asynchronous query |
Overdrawing causes frame drop |
() Repeat drawing |
Remove redundant background + Use ClipRect |
Summary of the formula:
Automatic monitoring three tricks, main thread polling and frame listening
Open source tools save effort, stack positioning is stuttering
Data reporting should be streamlined, and it should be compatible with avoiding pits and saving power.
Smooth experience relies on monitoring, and users won without interruption!
The above is the detailed content of the implementation method of Android automated acquisition of lag information. For more information about Android obtaining lag information, please follow my other related articles!