使用 Perfetto、SQL 和 AI 智能体发现并修复 Android 内存泄漏
Finding and fixing Android memory leaks with Perfetto, SQL, and AI agents

原始链接: https://perfetto.dev/blog/hunting-android-memory-leaks

未释放的回调与不匹配的 Android 生命周期会固定已销毁的 Fragment、视图和位图,导致内存压力。Perfetto 提供开源、端到端的 Android 内存分析栈。它无需修改应用即可捕获轻量的 ART 堆/引用图和原生性能剖析,然后在 Perfetto Web UI 中使用堆探索器、支配者火焰图和位图检查器进行可视化。PerfettoSQL 支持从浏览器查询到本地 CI 检查以及大规模 BigTrace 分析的程序化泄漏排查。其 AI 技能将追踪发现与源代码关联,帮助代理诊断、修补、测试和聚类泄漏。文章还描述了常见泄漏模式——回调注册表不匹配、窗口视图作用域问题、无限制的位图保留、取消的清理、执行器积压和响应式 churn——并展示用于保留内存分析的捕获选项和 SQL 示例。

Hacker News 新建 | 往期 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 使用 Perfetto、SQL 和 AI 代理查找并修复 Android 内存泄漏 (perfetto.dev) 4 分,作者 lalitmaganti,1 小时前 | 往期 | 收藏 | 讨论 帮助 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文
All posts

A single unregistered callback in an Android app can quietly pin an entire destroyed Fragment, its view hierarchy, and tens of megabytes of decoded Bitmap buffers in memory. Do that a few times across navigation transitions and a smooth experience turns into aggressive garbage collection pauses, background process kills (lmkd), or an outright OutOfMemoryError.

Every complex, asynchronous application encounters memory leaks sooner or later. Modern Android apps juggle overlapping lifecycles: Activities, Fragments, Compose UI trees, ViewModels, coroutine scopes, hardware callbacks, and window-level observers. When any two of those lifecycles fall out of sync, memory stays reachable long after its work is done.

At Google, I work on Android platform system health. To keep our own applications and the broader Android ecosystem fast and reliable, my colleagues and I built a comprehensive suite of memory profiling and analysis tools directly into Perfetto, and we open-sourced the entire stack so every developer can use the exact same workflows.

In this post, I will walk through how to hunt down Android memory leaks across four complementary layers of the Perfetto ecosystem:

  1. Capturing heap profiles on any Android device with zero app code changes.
  2. Interactive visual forensics in the Perfetto Web UI using the Heap Dump Explorer, dominator flamegraphs, and bitmap metadata inspectors.
  3. Programmatic analysis with PerfettoSQL, from ad-hoc queries in the browser to automated local CI checks with trace_processor and fleet-wide analysis across thousands of traces with BigTrace.
  4. Agentic root-cause analysis using the open-source Perfetto AI Skill, where coding agents combine heap graph queries with repository-wide code search and automated test generation to diagnose, fix, and verify memory leaks end to end.

What makes Perfetto different as a heap analyzer

If you have debugged Java memory leaks before, you have likely used standard JVM .hprof snapshots. Traditional HPROF dumps serialize every primitive field and byte array payload in the process, producing massive files that are slow to transfer and disconnected from what the rest of the operating system was doing when memory spiked. (If you want a refresher on how Android and the Linux kernel account for virtual memory areas, anonymous pages, RSS, PSS, and swap, see the Debugging memory usage on Android case study.)

Perfetto approaches memory analysis differently:

  • Lightweight reference graphs (android.java_hprof): Rather than dumping raw primitive contents, Perfetto's ART heap profiler records the complete object retention graph: every live Java/Kotlin object, its class, its shallow size, its native allocation size (via libcore.util.NativeAllocationRegistry), and the exact named member fields connecting owners to referents. Because no string payloads or pixel buffers leave the process, it is fast, privacy-safe, and works on non-debuggable builds. (When you do need full primitive/bitmap pixel contents on a debuggable build, Perfetto also imports standard ART .hprof files.)
  • Unified timeline context (Memory Counters): Because a heap snapshot is just one data source in a Perfetto trace, you can correlate object retention directly against kernel memory counters (anon_rss, swap, oom_score_adj), CPU scheduling slices, GC pauses, and app lifecycle events on the exact same timeline.
  • Java and Native (heapprofd) under one roof: Android memory pressure frequently crosses the JNI boundary. A tiny 64-byte Kotlin wrapper object might retain a 24 MB native hardware buffer. Perfetto lets you inspect both ART object retention graphs and native C/C++ callstack allocations or ART allocation profiles in a single tool.

Anatomy of real-world Android memory leaks

Before opening a trace, it helps to recognize the structural patterns that cause memory leaks in production Android code. Across hundreds of investigations, almost every memory issue falls into one of six recurring engineering patterns.

Lifecycle mismatch in callback registries

Short-lived UI components frequently register callbacks on longer-lived registries without binding the registration to their own lifecycle.

联系我们 contact @ memedata.com