{"record":{"id":"5561a40b8963d705","repo":"bumptech/glide","slug":"cannot-obtain-size-for-recycled-bitmap-bitmap","errorCode":null,"errorMessage":"Cannot obtain size for recycled Bitmap: {bitmap}[{width}x{height}] {config}","messagePattern":"Cannot obtain size for recycled Bitmap: (.+?)\\[(.+?)x(.+?)\\] (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"library/src/main/java/com/bumptech/glide/util/Util.java","lineNumber":75,"sourceCode":"   * Returns the allocated byte size of the given bitmap.\n   *\n   * @see #getBitmapByteSize(android.graphics.Bitmap)\n   * @deprecated Use {@link #getBitmapByteSize(android.graphics.Bitmap)} instead. Scheduled to be\n   *     removed in Glide 4.0.\n   */\n  @Deprecated\n  public static int getSize(@NonNull Bitmap bitmap) {\n    return getBitmapByteSize(bitmap);\n  }\n\n  /** Returns the in memory size of the given {@link Bitmap} in bytes. */\n  @TargetApi(Build.VERSION_CODES.KITKAT)\n  public static int getBitmapByteSize(@NonNull Bitmap bitmap) {\n    // The return value of getAllocationByteCount silently changes for recycled bitmaps from the\n    // internal buffer size to row bytes * height. To avoid random inconsistencies in caches, we\n    // instead assert here.\n    if (bitmap.isRecycled()) {\n      throw new IllegalStateException(\n          \"Cannot obtain size for recycled Bitmap: \"\n              + bitmap\n              + \"[\"\n              + bitmap.getWidth()\n              + \"x\"\n              + bitmap.getHeight()\n              + \"] \"\n              + bitmap.getConfig());\n    }\n    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {\n      // Workaround for KitKat initial release NPE in Bitmap, fixed in MR1. See issue #148.\n      try {\n        return bitmap.getAllocationByteCount();\n      } catch (\n          @SuppressWarnings(\"PMD.AvoidCatchingNPE\")\n          NullPointerException e) {\n        // Do nothing.\n      }","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/bumptech/glide/blob/eb14a895d8f866e6a08c3e19d50ea3bd2c12c20a/library/src/main/java/com/bumptech/glide/util/Util.java#L57-L93","documentation":"Thrown by Util.getBitmapByteSize(Bitmap) when bitmap.isRecycled() is true. The byte size of a Bitmap is read via getAllocationByteCount, whose return value silently changes from internal buffer size to rowBytes*height once the Bitmap is recycled, which would corrupt Glide's BitmapPool and memory-cache size accounting. The assert keeps cache sizes consistent.","triggerScenarios":"Calling any code path that reads a Bitmap's byte size after Bitmap.recycle(): cache put/size accounting, BitmapPool.put, memory trimming that touches already-recycled bitmaps; manual recycling of a Bitmap still referenced by Glide.","commonSituations":"Calling imageView.drawable.bitmap.recycle() on a Bitmap that Glide still owns/manages; double-recycling; pool evictions racing with external recycle; passing a recycled Bitmap back into Glide via .load(bitmap).","solutions":["Never manually recycle a Bitmap that Glide loaded — Glide's BitmapPool manages recycling","If you must recycle, only do so for Bitmaps you created yourself and that Glide has never seen","Guard: if (!bitmap.isRecycled) Util.getBitmapByteSize(bitmap)","Audit for double-recycle or external recycle of Glide-managed bitmaps (especially inListAdapter / onViewRecycled)"],"exampleFix":"// before\nval bmp = (imageView.drawable as BitmapDrawable).bitmap\nbmp.recycle()\nUtil.getBitmapByteSize(bmp) // IllegalStateException\n\n// after — let Glide manage recycling\nGlide.with(imageView).clear(imageView)\n// or, for your own bitmaps:\nif (!bmp.isRecycled) {\n  Util.getBitmapByteSize(bmp)\n}","handlingStrategy":"validation","validationCode":"if (!bitmap.isRecycled) {\n  val size = Util.getBitmapByteSize(bitmap)\n} else {\n  // bitmap already returned to pool; do not size it\n}","typeGuard":"fun Bitmap.isUsable(): Boolean = !isRecycled","tryCatchPattern":null,"preventionTips":["Never manually recycle a Bitmap that Glide loaded — Glide's BitmapPool owns recycling","Only recycle Bitmaps you created and that Glide has never seen","Clear Glide loads (Glide.with(view).clear(...)) instead of recycling their bitmaps","Audit RecyclerView adapters for stray recycle() calls on Glide-managed bitmaps"],"tags":["android","glide","bitmap","recycling","bitmap-pool"],"backgroundTag":null,"analyzedSha":"eb14a895d8f866e6a08c3e19d50ea3bd2c12c20a","analyzedAt":"2026-08-14T01:43:54.821Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}