OutOfMemoryError: Direct Buffer Memory

java.lang.OutOfMemoryError: Direct buffer memory

Quick Answer

You have exhausted the direct (off-heap) memory allocated for NIO buffers. Increase -XX:MaxDirectMemorySize or fix the buffer leak.

Why This Happens

Direct ByteBuffers allocated with ByteBuffer.allocateDirect() use memory outside the Java heap. This memory has a separate limit controlled by -XX:MaxDirectMemorySize. Netty, NIO channels, and memory-mapped files commonly use direct buffers.

The Problem

// Allocating many direct buffers without releasing them
List<ByteBuffer> buffers = new ArrayList<>();
for (int i = 0; i < 100_000; i++) {
    buffers.add(ByteBuffer.allocateDirect(1_000_000));
}

The Fix

// Reuse buffers or use heap buffers when direct is not needed
ByteBuffer buffer = ByteBuffer.allocateDirect(1_000_000);
for (int i = 0; i < 100_000; i++) {
    buffer.clear(); // Reuse the same buffer
    processData(buffer);
}

Step-by-Step Fix

  1. 1

    Identify direct buffer usage

    Look for ByteBuffer.allocateDirect() calls and NIO channel operations that allocate direct buffers.

  2. 2

    Check for buffer leaks

    Direct buffers are not freed by regular GC. Ensure references are released so the cleaner can reclaim memory.

  3. 3

    Reuse or limit direct buffers

    Pool and reuse direct buffers, use heap buffers where direct is not needed, or increase -XX:MaxDirectMemorySize.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free