Understanding and Optimizing Android Jank (Lag) in Mobile Applications
The article explains Android jank, defines Google and PerfDog metrics, identifies direct and indirect causes, recommends profiling tools such as Systrace, Perfetto and APM, and details a Wesing case study where breaking tasks, lazy loading, view‑hierarchy reduction and thread off‑loading cut PerfDog jank by roughly half, concluding with a checklist for systematic detection and mitigation.
The article begins by explaining the concept of jank (lag) in mobile apps, covering visual inertia, the movie frame threshold (≈24 fps), and how sudden frame‑rate changes disrupt user perception of smoothness.
It then defines Google Jank (a single missed vsync) and PerfDog Jank (current frame time > 2× average of previous three frames and > 2× movie‑frame time), introduces the Stutter metric (total Jank time divided by test duration), and clarifies that high FPS does not guarantee absence of jank, nor does low FPS always indicate jank.
Direct causes of jank are identified as single‑point long‑running tasks, multi‑point accumulated delays, and complex or frequently executed code. Indirect causes include insufficient CPU time for the main thread and frequent garbage collection.
For troubleshooting, the author recommends selecting appropriate tools: APM platforms for online monitoring, Systrace/CPU Profiler (simpleperf) for low‑overhead offline analysis, log‑based timing analysis, and adb commands for thread and memory inspection. Perfetto is highlighted for SQL‑style CPU‑time and lock‑contention queries.
The Wesing case study section reports a ~50 % reduction in PerfDog Jank rate after optimizations. It details multiple optimization strategies: breaking large Message‑handling tasks into smaller ones and using postAtFrontOfQueue, moving SDK initialization, bitmap blurring, and JSON parsing to worker threads, preloading core classes on a background thread for users who have entered the singing room, applying lazy loading (Kotlin’s by lazy, optionally with LazyThreadSafetyMode.NONE), reducing view hierarchy with merge tags and ViewStub, offloading logging to a dedicated HandlerThread, optimizing string concatenation in logs, fixing memory leaks (e.g., un‑stopped animations, ViewPager2 pre‑load limits), and mitigating thread‑spike issues caused by third‑party SDK upgrades.
Finally, the article summarizes practical experience: use APM to filter crash/ANR logs, employ CPU and memory profilers, analyze logs for early/late or slow operations, leverage Perfetto for thread‑CPU accounting, monitor thread counts via adb, address unreasonable memory usage, conduct version‑specific performance testing, and perform systemic analysis covering both direct and indirect jank sources.
Tencent Music Tech Team
Public account of Tencent Music's development team, focusing on technology sharing and communication.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.