Key Direct Byte Buffer Deallocation Methods

The JVM primarily uses `sun.misc.Cleaner` with phantom references for automatic release, while explicit invocation via `cleaner.clean()` or custom pooling strategies like Netty's offer immediate control.

The top 3

  1. JVM's Default Deallocation: GC's Hidden Role: Explains how `DirectByteBuffer` native memory is implicitly freed when the Java object is garbage collected, often via a `Cleaner` or `PhantomReference`, leading to potential latency and unpredictable timing in off-heap memory release.
  2. `sun.misc.Unsafe`: The Direct, Dangerous Deallocator: Details using `Unsafe.freeMemory` for immediate native memory release, highlighting its performance gains but extreme risks like memory leaks, crashes, or corruption if misused due to its low-level and internal nature.
  3. `java.lang.ref.Cleaner`: Safer Explicit Off-Heap Release: Describes the `java.lang.ref.Cleaner` API (standardized since Java 9) as the modern, safer alternative for registering and managing explicit, yet asynchronous, deallocation of native resources, offering more control than pure garbage collection.

Sources

Open the full topic