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
Always resize images to their exact display dimensions before converting to WebP, or risk OOM crashes from hidden memory bloat.
About This Article
Grindr Engineering found that a 960x960 WebP image in drawable-xhdpi was causing OutOfMemoryError crashes. The system converts it to ARGB_8888 format by default, which uses 3.6MB of heap memory even though the compressed file is small.
Ansgar Lin traced the issue by looking at crash logs and working backward from the memory data. He calculated the bitmap size from 3688412 bytes divided by 4 to get 960x960 pixels, then found the image in the resources using screen density matching.
The analysis showed that Android 3-7 keeps bitmaps in the limited Dalvik heap, making them 5 times more prone to OOM crashes than Android 8 and later. Android 8+ moved bitmap storage to the Native heap, which has more physical memory available.