Top 3 Hidden Native Memory Leak Culprits

Common sources of hard-to-find native memory leaks include excessive use or improper management of Direct ByteBuffers, memory allocated via Java Native Interface (JNI) calls that isn't freed, and uncontrolled Metaspace growth from dynamic class loading.

The top 3

  1. JNI Global Reference Leaks: The Unfreed Native Allocations: One of the most insidious native memory leaks arises from JNI code failing to release native memory allocated via `malloc` or `NewGlobalRef` without a corresponding `free` or `DeleteGlobalRef`, leading to persistent memory consumption outside the JVM's garbage collector's purview.
  2. Direct Byte Buffers: Unfreed Off-Heap Memory: Java's `ByteBuffer.allocateDirect()` creates memory outside the JVM heap for performance, often used in ML; if the corresponding Java `ByteBuffer` object is prematurely garbage collected or improperly managed, the underlying native memory might not be deallocated, leading to silent leaks.
  3. Native Library Allocations: The Uncontrolled `malloc`: When JVM-based ML applications interact with external C/C++ libraries via JNI, these native libraries can allocate memory using functions like `malloc` or `new`; if the native code fails to explicitly `free` or `delete` this memory before its lifecycle ends, it becomes a persistent, untraceable native memory leak.

Open the full topic