问题
I have two questions :
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 ?
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 ?
回答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).
- No, Java has sockets and indeed ProcessBuilder. Shared memory can be achieved with MappedByteBuffer.
回答2:
- You need deallocate any os resource created in native code, such as File Descriptor, memory address (allocate by malloc. etc) because they are not binding with any jvm instance.
You can consider use Memory-Mapped Files (sample).
You can use RPCs (between computer and computer) in IPC context, such as socket, web service, JMS, etc.
回答3:
- in all likelihood yes - though i'm not entirely sure whether there doesn't exist a way to clear that memory.
- take a look at ProcessBuilder - it might be of some help to exclude JNI to achieve IPC.
来源:https://stackoverflow.com/questions/3879398/java-memory-management-for-jni