{"record":{"id":"084dbb48b2e2f725","repo":"bumptech/glide","slug":"file-too-large-to-map-into-memory","errorCode":null,"errorMessage":"File too large to map into memory","messagePattern":"File too large to map into memory","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"library/src/main/java/com/bumptech/glide/util/ByteBufferUtil.java","lineNumber":37,"sourceCode":"@SuppressWarnings({\"unused\", \"WeakerAccess\"}) // Public API\npublic final class ByteBufferUtil {\n  // 16 Kb\n  private static final int BUFFER_SIZE = 16384;\n  private static final AtomicReference<byte[]> BUFFER_REF = new AtomicReference<>();\n\n  private ByteBufferUtil() {\n    // Utility class.\n  }\n\n  @NonNull\n  public static ByteBuffer fromFile(@NonNull File file) throws IOException {\n    RandomAccessFile raf = null;\n    FileChannel channel = null;\n    try {\n      long fileLength = file.length();\n      // See #2240.\n      if (fileLength > Integer.MAX_VALUE) {\n        throw new IOException(\"File too large to map into memory\");\n      }\n      // See b/67710449.\n      if (fileLength == 0) {\n        throw new IOException(\"File unsuitable for memory mapping\");\n      }\n\n      raf = new RandomAccessFile(file, \"r\");\n      channel = raf.getChannel();\n      return channel.map(FileChannel.MapMode.READ_ONLY, 0, fileLength).load();\n    } finally {\n      if (channel != null) {\n        try {\n          channel.close();\n        } catch (IOException e) {\n          // Ignored.\n        }\n      }\n      if (raf != null) {","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/bumptech/glide/blob/eb14a895d8f866e6a08c3e19d50ea3bd2c12c20a/library/src/main/java/com/bumptech/glide/util/ByteBufferUtil.java#L19-L55","documentation":"Thrown by ByteBufferUtil.fromFile(File) when file.length() exceeds Integer.MAX_VALUE (~2 GiB). The method memory-maps the file into a java.nio.ByteBuffer, which is indexed by int and therefore cannot address more than 2^31-1 bytes. This guard prevents a 2GB+ file from producing a bogus partial mapping.","triggerScenarios":"Loading a local file > 2GB via any Glide path that funnels through ByteBufferUtil.fromFile (e.g. asFile().load(file), or a File model with certain decoders). Also triggered by FileChannel.map on huge media files shared into the app.","commonSituations":"Loading very large video files or archives; on devices where the user picks a >2GB file via SAF/Storage Access Framework; misconfigured cache that grew a single cache entry beyond 2GB.","solutions":["Avoid memory-mapping files > 2GB; stream them instead (use an InputStream-based model/decoder rather than asFile().load(file))","Reduce source file size (transcode/downscale the media before handing to Glide)","For video, use VideoView/ExoPlayer rather than Glide","Pre-check file.length() and skip or surface a user-facing 'file too large' message"],"exampleFix":"// before\nGlide.with(ctx).asFile().load(hugeFile).into(target) // > 2 GiB\n\n// after - guard before loading\nif (hugeFile.length() > Integer.MAX_VALUE) {\n  showError(\"File too large to load\")\n} else {\n  Glide.with(ctx).asFile().load(hugeFile).into(target)\n}","handlingStrategy":"validation","validationCode":"fun File.isMappable(): Boolean = exists() && length() in 1..Integer.MAX_VALUE.toLong()\n\nif (file.isMappable()) {\n  Glide.with(ctx).asFile().load(file).into(target)\n} else {\n  showTooLargeError()\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check file.length() <= Integer.MAX_VALUE before any asFile()/File load path","Prefer streaming decoders for very large media","Downscale or transcode huge assets before handing them to Glide"],"tags":["android","glide","io","memory","file-size"],"backgroundTag":null,"analyzedSha":"eb14a895d8f866e6a08c3e19d50ea3bd2c12c20a","analyzedAt":"2026-08-14T01:43:54.821Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}