Finding and fixing Android memory leaks with Perfetto, SQL, and AI agents Google open-sourced a Perfetto-based memory profiling stack that captures Android heap reference graphs without app code changes, letting developers correlate object retention with kernel memory counters, GC pauses and lifecycle events on one timeline. The stack includes the Perfetto Web UI's Heap Dump Explorer and dominator flamegraphs, PerfettoSQL queries runnable locally via trace_processor or fleet-wide via BigTrace, and an open-source Perfetto AI Skill that lets coding agents combine heap graph queries with repository-wide code search and automated test generation to diagnose and fix leaks. Google says the approach avoids the large, slow HPROF dumps of traditional JVM tooling because no string payloads or bitmap buffers leave the process, making it privacy-safe and usable on non-debuggable builds. All posts https://perfetto.dev/blog/ Finding and fixing Android memory leaks with Perfetto, SQL, and AI agents 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 https://perfetto.dev , 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. 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 https://perfetto.dev/docs/case-studies/memory . Perfetto approaches memory analysis differently: - Lightweight reference graphs android.java hprof https://perfetto.dev/docs/data-sources/java-heap-profiler : 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 https://perfetto.dev/docs/data-sources/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 https://perfetto.dev/docs/data-sources/native-heap-profiler 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 https://perfetto.dev/docs/getting-started/memory-profiling in a single tool. 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. Short-lived UI components frequently register callbacks on longer-lived registries without binding the registration to their own lifecycle. // LEAK: Registered on the Activity's registry without passing a LifecycleOwner. // The Activity outlives the Fragment and retains the callback and Fragment indefinitely. class PhotoAttachmentLauncher private val fragment: Fragment { private lateinit var launcher: ActivityResultLauncher