Shark: Diving into the guts of LeakCanary's Hprof parser
summary
LeakCanary’s HPROF parser is rewritten as Shark in Kotlin and claims 10× less memory and 6× faster parsing; it replaces Perfetto’s heavy in-memory indexes with file-position pointers for instances, uses Okio and Java NIO to read on demand, and finds shortest root-to-object paths via breadth‑first search. A 160 MB heap that once forced 2 GB+ gets parsed in ~4s using ~250 MB, and switching HashMap indexes to a sorted byte-array map slashes index cost; profiling used YourKit to find dominant retainers.
AI-written summary · about
chapters (12)
- 0:00Introduction to Shark
- 1:07What is Shark and LeakCanary 2
- 2:11How LeakCanary detects leaks
- 4:19Garbage collection basics
- 6:26Heap dumps and GC roots
- 9:36Heap parsing overview
- 13:02Out of memory with HPROF parser
- 17:36Analyzing heap parser memory usage
- 22:31Breadth-first search for leak paths
- 26:01HPROF file format and parsing
- 33:17Optimizing memory usage in Shark
- 41:45Key takeaways and conclusion
related talks
39mLeakCanary 2: Leaner, Better, Faster, Kotliner!
37mUniversal heap trimming with LeakCanary 3
35mA small leak can sink a great ship
31mA Small Leak Can Sink A Great Ship
37mRoot causing memory growth with LeakCanary 3
43mMemory Dumps: Going beyond the memory leak
20mMaking the memory dump a powerful development tool
37mLeakCanary, then what? Nuking Nasty Memory Leaks
