Finding and fixing Android memory leaks with Perfetto, SQL, and AI agents perfet...
HN Best 200 @best
Finding and fixing Android memory leaks with Perfetto, SQL, and AI agents perfetto.dev · 14 points · 5 comments · HN thread
TL;DR A Google Android platform engineer describes how to find and fix memory leaks on Android using Perfetto: capturing ART Java heap graphs and native heap profiles without app changes, analyzing them in the Heap Dump Explorer and with PerfettoSQL, scaling via BigTrace, and using an AI agent skill to link heap retention chains to source code and tests. The post includes six leak patterns and three worked agent examples.
- Perfetto's android.java_hprof records the object retention graph (classes, shallow sizes, native allocation sizes, named fields) rather than full primitive contents, so it is faster, privacy-safe, and works on non-debuggable builds.
- Heap snapshots can be captured with the java_heap_dump helper script, the Memscope UI in ui.perfetto.dev, or a custom TraceConfig that adds linux.process_stats counters for timeline correlation.
- Recurring leak patterns include lifecycle-unaware callback registration (e.g. activityResultRegistry.register without a LifecycleOwner), window-root listeners capturing short-lived views, unbounded caches of Bitmaps, and cleanup coroutines cancelled with their parent scope (fixed with NonCancellable).
- PerfettoSQL modules such as android.memory.heap_graph.class_summary_tree and dominator_tree rank classes and instances by retained size, and heap_graph_reference shows which field keeps an object alive.
- BigTrace distributes trace_processor queries across many traces, and the authors cluster dominator paths across fleet dumps to rank leak root causes.
- An open-source Perfetto AI skill lets coding agents triage a dump, find the offending source line, propose a fix, and write a regression test.
Discussion The thread is short and split between practical interest in Android native and Java memory tooling and irritation at the article's style. A commenter says Android memory leak hunting is hard because no single tool records all allocations, and a Perfetto engineer replies that heapprofd and Java heap dumps can be captured together in one trace, with Memscope as a new one-click UI. The follow-up exchange covers Poisson sampling versus tracking every allocation. A separate top-level comment rejects the post on the grounds that it reads as AI-generated and the commenter would prefer it written by hand.
- Native and Java memory tooling on Android is fragmented (some): One commenter found no single tool that records all allocations, with Java heap snapshots, malloc interposition and Android Studio's buggy profiler each covering only part of the picture. They noted Android Studio's profiler lacked correlation features.
- Perfetto already combines Java and native heap data (some): A Perfetto developer replied that a single trace can hold both Java heap dumps and native heap profiles, and that the new Memscope UI aligns them in time with a one-click workflow still under active development.
- Sampling is preferred over tracking every allocation (a few): In the reply thread, the Perfetto developer explained that heapprofd uses Poisson sampling with a default 4096-byte interval because tracking every allocation (interval 1, with block_client) slows devices to a crawl or drops data. Large allocations are reliably sampled.
- Article reads as AI-generated and is unpleasant to read (a few): One commenter objected to the post's apparent AI-written prose and said they would rather read something written by a person, while stating they are not against AI in general.
Links from the comments