java runtime_error ai_generated true

java.lang.OutOfMemoryError: Direct buffer memory

ID: java/outofmemoryerror-direct-buffer-memory

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
1Evidence
2023-10-25First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Java 8 active
Java 11 active
Java 17 active
Java 21 active

Root Cause

The JVM has exhausted the off-heap memory limit (MaxDirectMemorySize) for direct byte buffers, typically caused by allocating many direct ByteBuffers without releasing them, or by NIO operations that leak direct memory.

generic

中文

JVM 已耗尽直接字节缓冲区的堆外内存限制(MaxDirectMemorySize),通常由分配大量直接 ByteBuffer 而不释放,或 NIO 操作泄漏直接内存引起。

Official Documentation

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/OutOfMemoryError.html

Workarounds

  1. 85% success Ensure direct ByteBuffers are explicitly released after use by calling the cleaner or using try-with-resources if possible. For Netty or other NIO frameworks, check for buffer pooling and leak detection.
    Ensure direct ByteBuffers are explicitly released after use by calling the cleaner or using try-with-resources if possible. For Netty or other NIO frameworks, check for buffer pooling and leak detection.
  2. 75% success Increase the MaxDirectMemorySize JVM parameter to accommodate legitimate direct memory usage, but also implement proper cleanup.
    Increase the MaxDirectMemorySize JVM parameter to accommodate legitimate direct memory usage, but also implement proper cleanup.
  3. 70% success Use a memory-mapped file or heap-based ByteBuffer instead of direct ByteBuffer when possible, to avoid off-heap memory pressure.
    Use a memory-mapped file or heap-based ByteBuffer instead of direct ByteBuffer when possible, to avoid off-heap memory pressure.

中文步骤

  1. Ensure direct ByteBuffers are explicitly released after use by calling the cleaner or using try-with-resources if possible. For Netty or other NIO frameworks, check for buffer pooling and leak detection.
  2. Increase the MaxDirectMemorySize JVM parameter to accommodate legitimate direct memory usage, but also implement proper cleanup.
  3. Use a memory-mapped file or heap-based ByteBuffer instead of direct ByteBuffer when possible, to avoid off-heap memory pressure.

Dead Ends

Common approaches that don't work:

  1. 95% fail

    Increasing heap space (-Xmx) does not affect direct memory limits; direct memory is separate from heap.

  2. 90% fail

    Enabling GC logging without tuning direct memory does not prevent the leak; it only helps diagnose.

  3. 70% fail

    Setting MaxDirectMemorySize to a very high value only delays the crash; the underlying leak continues.