After a few more searches, I found a good article at the IBM Research Center.
In short, they recommend using a bunch of Java instead of their own heap for their own objects. Thus, the memory pressure on the JVM garbage collector is more realistic for native objects referenced by Java code through descriptors.
To achieve this, you need to override the default C ++ memory allocation and deletion functions: the new operator and the delete operator. In the new operator, if the JVM is available (JNI_OnLoad has already been called), then it calls NewByteArray and GetByteArrayElements, which returns the allocated memory. To protect the ByteArray created from garbage collection, you must also create a NewGlobalRef for it and save it, for example. in the same allocated memory block. In this case, we need to allocate as much memory as requested, plus memory for the links. In operator deletion, delete DeleteGlobalRef and ReleaseByteArrayElements. If the JVM is not available, it uses its own malloc and free functions.
source share