# java.lang.OutOfMemoryError: Failed to allocate a 41472012 byte allocation with 16777216 free bytes and 16MB until OOM, target footprint: 268435456, growth limit: 268435456

- **ID:** `android/outofmemoryerror-bitmap-too-large`
- **Domain:** android
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

App tries to load a large bitmap (e.g., from camera or network) into memory without downscaling, exceeding the per-app heap limit.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Android 12 (API 31) | active | — | — |
| Android 13 (API 33) | active | — | — |
| Android 14 (API 34) | active | — | — |
| Glide 4.15 | active | — | — |
| Coil 2.4.0 | active | — | — |

## Workarounds

1. **Use BitmapFactory.Options to downsample the image: BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 4; // reduces size by factor of 4 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.large_image, options);** (90% success)
   ```
   Use BitmapFactory.Options to downsample the image: BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 4; // reduces size by factor of 4 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.large_image, options);
   ```
2. **Use an image loading library like Glide or Coil that handles downsampling and caching automatically: Glide.with(context).load(url).override(800, 600).into(imageView);** (95% success)
   ```
   Use an image loading library like Glide or Coil that handles downsampling and caching automatically: Glide.with(context).load(url).override(800, 600).into(imageView);
   ```
3. **If loading from camera, use getBitmap() with a reduced max dimension: Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options); where options.inJustDecodeBounds = true is used first to calculate the sample size.** (85% success)
   ```
   If loading from camera, use getBitmap() with a reduced max dimension: Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options); where options.inJustDecodeBounds = true is used first to calculate the sample size.
   ```

## Dead Ends

- **** — Increasing the heap size via android:largeHeap="true" in the manifest is not a reliable fix because the system may still kill the app, and it only provides a larger limit, not unlimited memory. (80% fail)
- **** — Using BitmapFactory.decodeResource() without any options loads the full-resolution bitmap, which will still cause OOM for large images. (90% fail)
- **** — Calling System.gc() before loading an image does not guarantee that enough memory will be freed, and the garbage collector may not run immediately. (85% fail)
