java runtime_error ai_generated true

java.lang.OutOfMemoryError:直接缓冲区内存

java.lang.OutOfMemoryError: Direct buffer memory

ID: java/outofmemoryerror-direct-buffer-memory

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
1证据数
2023-10-25首次发现

版本兼容性

版本状态引入弃用备注
Java 8 active
Java 11 active
Java 17 active
Java 21 active

根因分析

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

English

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

官方文档

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

解决方案

  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.

无效尝试

常见但无效的做法:

  1. 95% 失败

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

  2. 90% 失败

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

  3. 70% 失败

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