I am trying to build an application in which I am using the JNI to link C and java.
Native-lib.cpp:
extern "C"
JNIEXPORT void JNICALL
Java_com_confuadhoc_wrappers_OboeRecorder_startRecording(JNIEnv *env, jobject thiz) {
auto oboeRecorderClass = env->FindClass("com/confuadhoc/wrappers/OboeRecorder");
auto oboeRecorderClassGlobalReference = (jclass) env->NewGlobalRef(oboeRecorderClass);
oboeRecorder = new OboeRecorder(env, thiz, oboeRecorderClassGlobalReference);
}
OboeRecorder.cpp:
JavaVM *jvm;
jmethodID mID;
jshortArray array;
jclass oboeRecorderClass;
JNIEnv *myEnv;
jshortArray j = nullptr;
jobject obj = nullptr;
int16_t arr[480] = {0};
OboeRecorder::OboeRecorder(JNIEnv *env, jobject thiz, jclass oboeClass) {
oboeRecorderClass = oboeClass;
env->GetJavaVM(&jvm);
mID = env->GetMethodID(oboeRecorderClass, "<init>", "()V");
LOGE("the method id fetched successfully: %d",mID);
if (mID == nullptr) {
LOGE("CLASS IS method id is null");
exit(0);
}
}
OboeRecorder::onAudioReady(oboe::AudioStream *oboeStream, void *audioData, int32_t numFrames) {
oboe::DataCallbackResult callbackResult = oboe::DataCallbackResult::Continue;
auto result = static_cast<int16_t *>(audioData);
LOGE("num of frames in oboerecorder onaudioready:%d ",numFrames);
for (int i = 0; i < numFrames; i++) {
ringBuffer.insert(result[i]);
}
getData();
return callbackResult;
}
Get Data of OboeRecorder:
void OboeRecorder::getData() {
JNIEnv *s = getEnv();
if (s == nullptr) {
LOGE("CLASS IS NULL");
exit(0);
}
if (ringBuffer.readAvailable() >= 480) {
LOGE("OboeRecorder: ringbuffer is more than 480");
for (short &i : arr) {
int16_t x = 0;
if (ringBuffer.remove(&x)) {
LOGE("OboeRecorder: ringbuffer removed successful");
i = x;
} else {
LOGE("OboeRecorder: ERROR IN REMOVE");
}
}
j = s->NewShortArray(480);
s->SetShortArrayRegion(j, 0, 480, arr);
obj = s->NewObject(oboeRecorderClass, mID);
jmethodID methodId = s->GetMethodID(oboeRecorderClass, "getOboeRecordedData", "([S)V");
s->CallVoidMethod(obj, methodId, j);
s->DeleteLocalRef(obj);
} else {
LOGE("OBOE DATA IN ARRAY IS: NOT ENOUGH");
}
}
getEnv:
JNIEnv *OboeRecorder::getEnv() {
int status = jvm->GetEnv((void **) &myEnv, JNI_VERSION_1_6);
LOGE("OboeRecorder: value of status of env:%d",status);
if (status < 0) {
status = jvm->AttachCurrentThread(&myEnv, NULL);
LOGE("OboeRecorder: attach thread is called");
if (status < 0) {
return nullptr;
}
}
return myEnv;
}
The problem is that the memory is constantly increasing. I have tried the DeleteLocalRef but that doesn’t help to release memory. Can someone help me how can I be able to release memory in the current scenario?
Source: Windows Questions C++