Search code examples
javajava-native-interfaceheap-memory

Java memory Management for JNI


I have two questions :

  1. What if I have a JNI call to a method and the JNI method leaks memory. Once this method completes will the JVM Garbage collector be able to get that memory back. I heard that the JVM does not manage the Heap Space used by JNI ? But the memory used by JNI is a part of the memory used by the Java process ?

  2. Is it absolutely necessary to use JNI to achieve IPC ? What are the other popular Java techniques or is there a Open Source Library to achieve Shared memory in Java ?


Solution

    1. No: "the JNI framework does not provide any automatic garbage collection for non-JVM memory resources allocated by code executing on the native side" (Wikipedia).
    2. No, Java has sockets and indeed ProcessBuilder. Shared memory can be achieved with MappedByteBuffer.