Pinch Pinch - 4 months ago 18
C Question

Java - How is memory in JNI C code managed?

Let's say I'm calling some C code from Java via JNI, and in the C code there is some global array. How is this memory allocated and managed? I'm assuming it won't be on the JVM heap.

Answer

It is not on the JVM heap or the native metaspace. The memory is allocated from native memory space that is given to the java process, i.e. the process that started JVM as a native OS process. It is managed by the native code that allocated it, so if you don't manually free all malloc, then you get a memory leak.

There isn't much concrete material to rely on, however, this Oracle guide is a good place to start digging as any.