Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 824
Geek Avatar asked Oct 07 '10 07:10

Geek


People also ask

Does JNI use Java heap?

A JNI library can easily cause the Java program to exceed its maximum specified heap.

What is Java native memory?

Native memory is the memory provided to the application process by the operating system. The memory is used for heap storage and other purposes. The native memory information available in the native memory perspective view varies by platform but typically includes the following information: Table 1.

Where is JNI memory leak?

The only way to discover JNI memory leaks is to use a heap-dump tool that explicitly marks native references. If possible, you should not use any global references. It's better to assign the desired object to the field of a normal Java class.

How does JNI work in Java?

JNI provides functions for accessing the contents of array objects. While arrays of objects must be accessed one entry at a time, arrays of primitives can be read and written directly as if they were declared in C.


1 Answers

  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.
like image 144
Fred Foo Avatar answered Oct 03 '22 07:10

Fred Foo