# java.lang.OutOfMemoryError: 分配 41472012 字节失败，空闲 16777216 字节，距离 OOM 还有 16MB，目标占用：268435456，增长限制：268435456

- **ID:** `android/outofmemoryerror-bitmap-too-large`
- **领域:** android
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

应用尝试将大位图（例如，来自相机或网络）加载到内存而不进行缩放，超过了每个应用的堆限制。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Android 12 (API 31) | active | — | — |
| Android 13 (API 33) | active | — | — |
| Android 14 (API 34) | active | — | — |
| Glide 4.15 | active | — | — |
| Coil 2.4.0 | active | — | — |

## 解决方案

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);
   ```
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);
   ```
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.
   ```

## 无效尝试

- **** — 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% 失败率)
- **** — Using BitmapFactory.decodeResource() without any options loads the full-resolution bitmap, which will still cause OOM for large images. (90% 失败率)
- **** — 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% 失败率)
