Java memory Management for JNI

吃可爱长大的小学妹 提交于 2019-12-30 03:31:07

问题


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 ?


回答1:


  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.



回答2:


  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.




回答3:


  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.


来源:https://stackoverflow.com/questions/3879398/java-memory-management-for-jni

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!