Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 729
Pinch Avatar asked Oct 27 '25 17:10

Pinch


1 Answers

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.

like image 119
AlmasB Avatar answered Oct 29 '25 09:10

AlmasB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!