JNI (Java Native Interface) is the standard way Java calls into C/C++ (and vice versa).
🔹 Concrete instances where this happens:
-
Standard Java Library internals
- Many parts of the JDK are wrappers over OS or C libraries.
- Example:
java.lang.System.loadLibrary()System.arraycopy()→ implemented in Cjava.io.FileDescriptor,Socket,Thread→ all rely on OS calls via JNI
-
JVM integrations
- GC, JIT, threading, and monitoring tools use native hooks.
- Example: JMX or JVMTI agents.
-
Third-party libraries
- OpenCV (image processing), TensorFlow (ML), SQLite (databases).
- Java code calls into native DLLs /
.solibraries for performance-critical work.
-
Custom native methods
Developers can declare:
public class NativeExample {
public native void doSomething();
static { System.loadLibrary("nativeLib"); }
}Implemented in C:
JNIEXPORT void JNICALL Java_NativeExample_doSomething(JNIEnv *env, jobject obj) {
// Native logic here
}👉 Whenever this happens, the native library might hold references to Java objects passed into it. These references are stored in JNI global/weak handle tables, and the GC treats them as roots until released.