Identifying and Handling Android Build Memory Issues
Article Summary
Doni Winata from Traveloka's Android Infra team reveals why constantly increasing Gradle's heap size actually makes your build problems worse. His deep dive into Android build memory issues shows how to stop fighting symptoms and fix root causes.
As Android projects grow, out-of-memory errors become inevitable. Most developers respond by cranking up the -xmx heap size, but this often leads to frozen machines and slower builds. Winata shares Traveloka's systematic approach to profiling, identifying, and resolving memory issues using VisualVM, heap dumps, and GC analysis.
Key Takeaways
- Multiple daemon instances can waste gigabytes: sync JDK paths between Android Studio and terminal
- Parceler library leak held 2GB of objects, causing 3m9s of GC time versus 1m39s after fix
- Dagger memory leak accumulated 3.5GB across 5 builds, eventually causing OOM failures
- VisualGC plugin reveals exactly which tasks trigger excessive garbage collection during builds
- Forked daemons reduce main daemon overhead but require careful worker configuration to avoid memory bloat
Profiling with VisualVM and heap dumps helped Traveloka cut GC time by 48% and identify library-specific memory leaks that no amount of heap size increases could solve.
About This Article
The Android team at Traveloka found that just bumping up the -xmx heap size doesn't actually fix memory problems. Instead, it pushes the OS to use swap memory on disk, which freezes machines and tanks build speeds.
Doni Winata's team turned to VisualVM and its VisualGC plugin to watch garbage collection in real-time. When GC consumed more than 5% of build time, that was a sign something deeper was wrong. They dug into heap dumps and used the Eclipse Memory Analyzer (MAT) tool to find the root cause.
After fixing a memory leak in the Parceler library, Traveloka cut total garbage collection time from 3 minutes 9 seconds down to 1 minute 39 seconds. That's a 48% improvement. Major GC events dropped from 5 to 1 per build, and minor GC went from 612 down to 510 occurrences.