Yeah you are right, the immortal array is indeed … immortal and never garbage collected. There is no API to free it.
The problem here lies in the implementation of the SNI calls for safety and performance purpose.
The array given from java can’t be delete by the garbage collector as long as the SNI does not return.:
- once on the native is called, if you are blocking the virtual machine in your native thread, you are right the garbage collector is never triggered
- however if your are using your array in another native thread, the garbage collector could be triggered and free your array (since it “belongs” to the Java heap space)
What is usually done with our libraries are that we create immortal native buffers for all native calls on this library, so that is reusable. If you want you can use a pool of immortal arrays by using the API in
As of SNI 1.3 (not available publicly), we have the option to pass a non-immortal array at the condition that this array is never used in another thread.