tags:

views:

67

answers:

3

Hi,

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 ?

A: 
  1. in all likelihood yes - though i'm not entirely sure whether there doesn't exist a way to clear that memory.
  2. take a look at ProcessBuilder - it might be of some help to exclude JNI to achieve IPC.
anirvan
+2  A: 
  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.
larsmans
+1  A: 
  1. 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.
  2. You can consider use Memory-Mapped Files (sample).

  3. You can use RPCs (between computer and computer) in IPC context, such as socket, web service, JMS, etc.

qrtt1
The Java Heap/ JNI Heap are different once the JNI call is finished why can't the JVM just blindly clean up things ? The JNI Heap is still a part of the same process ?
Geek
OS Resource are not created from JNIEnv instance. No any references keep in JNIEnv. How to release the **unknown** or unmanaged resource ?
qrtt1
Blindly cleaning up the heap is impossible. What would happen to a C++ object with a destructor? To memory allocated using a custom allocator? To a C `FILE*` object, which needs to be `fclose`'d rather than `free`'d?
larsmans
That would be the same as Killing a Thread :(
Geek