Grindr Ansgar Lin Jul 4, 2019

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

Critical Insight

Always resize images to their exact display dimensions before converting to WebP, or risk OOM crashes from hidden memory bloat.

The crash distribution chart reveals why newer Android versions handle this problem dramatically better than older ones.

About This Article

Problem

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.

Solution

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.

Impact

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.