# java.lang.OutOfMemoryError: Direct buffer memory

- **ID:** `java/outofmemoryerror-direct-buffer-memory`
- **Domain:** java
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Java 8 | active | — | — |
| Java 11 | active | — | — |
| Java 17 | active | — | — |
| Java 21 | active | — | — |

## Workarounds

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.** (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.
   ```
2. **Increase the MaxDirectMemorySize JVM parameter to accommodate legitimate direct memory usage, but also implement proper cleanup.** (75% success)
   ```
   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.** (70% success)
   ```
   Use a memory-mapped file or heap-based ByteBuffer instead of direct ByteBuffer when possible, to avoid off-heap memory pressure.
   ```

## Dead Ends

- **** — Increasing heap space (-Xmx) does not affect direct memory limits; direct memory is separate from heap. (95% fail)
- **** — Enabling GC logging without tuning direct memory does not prevent the leak; it only helps diagnose. (90% fail)
- **** — Setting MaxDirectMemorySize to a very high value only delays the crash; the underlying leak continues. (70% fail)
