{
  "id": "android/outofmemoryerror-bitmap-too-large",
  "signature": "java.lang.OutOfMemoryError: Failed to allocate a 41472012 byte allocation with 16777216 free bytes and 16MB until OOM, target footprint: 268435456, growth limit: 268435456",
  "signature_zh": "java.lang.OutOfMemoryError: 分配 41472012 字节失败，空闲 16777216 字节，距离 OOM 还有 16MB，目标占用：268435456，增长限制：268435456",
  "regex": "java\\.lang\\.OutOfMemoryError: Failed to allocate a \\d+ byte allocation.*",
  "domain": "android",
  "category": "runtime_error",
  "subcategory": null,
  "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.",
  "root_cause_type": "generic",
  "root_cause_zh": "应用尝试将大位图（例如，来自相机或网络）加载到内存而不进行缩放，超过了每个应用的堆限制。",
  "versions": [
    {
      "version": "Android 12 (API 31)",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Android 13 (API 33)",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Android 14 (API 34)",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Glide 4.15",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Coil 2.4.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "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.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Using BitmapFactory.decodeResource() without any options loads the full-resolution bitmap, which will still cause OOM for large images.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Calling System.gc() before loading an image does not guarantee that enough memory will be freed, and the garbage collector may not run immediately.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "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);",
      "success_rate": 0.9,
      "how": "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);",
      "condition": "",
      "sources": []
    },
    {
      "action": "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);",
      "success_rate": 0.95,
      "how": "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);",
      "condition": "",
      "sources": []
    },
    {
      "action": "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.",
      "success_rate": 0.85,
      "how": "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.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "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);",
    "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);",
    "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."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://developer.android.com/topic/performance/graphics/load-bitmap",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2023-05-12",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}