Handling WebP OutOfMemoryError in Android
Article Summary
Ansgar Lin from Grindr Engineering discovered their app was crashing with OutOfMemoryErrors, and the culprit wasn't bad code. It was a tiny WebP image causing massive runtime memory allocation.
WebP images are popular for reducing APK size and bandwidth, but they hide a dangerous trap. A small WebP file can balloon into a huge bitmap at runtime if created from an oversized source image. Lin breaks down a real production crash and the math behind why it happened.
Key Takeaways
- A 960x960 WebP allocated 3.6MB of memory despite small file size
- Android 3 to 7 store bitmaps in limited Dalvik heap, causing more OOMs
- WebP bitmap size depends on original image dimensions, not compressed file size
- Android 8+ moved bitmaps to expandable Native heap, reducing crash risk
Critical Insight
Always resize images to their exact display dimensions before converting to WebP, or risk OOM crashes from hidden memory bloat.