Java from c++ (JNI) memory leaks
Hi all.. I am beginner in Java. My problem is: I am calling a Java class's method from c++. For this i am using JNI. Everythings works correct, but i have some memory LEAKS in the process of c++ program...
So.. i did simple example..
1) I create a java machine (jint res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);)
2) then i take a pointer on java class (jclass cls = env->FindClass("test_jni"));
3) after that i create a java class object object, by calling the constructor (testJavaObject = env->NewObject(cls, testConstruct);)
AT THIS very moment in the process of c++ program is allocated 10 MB of memory
3) Next i delete the class , the object, and the Java Machine ..
AT THIS very moment the 10 MB of memory are not free
.................
So below i have a few lines of code
C++ prgram:
Code:
#include <jni.h>
void main()
{
{
//Env
JNIEnv *env;
// java virtual machine
JavaVM *jvm;
JavaVMOption* options = new JavaVMOption[1];
//class paths
options[0].optionString = "-Djava.class.path=C:/Sun/SDK/jdk/lib;D:/jms_test/java_jni_leak;";
// other options
JavaVMInitArgs vm_args;
vm_args.version = JNI_VERSION_1_6;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = false;
// создаем виртуальную машину
jint res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
//Creating java virtual machine
jclass cls = env->FindClass("test_jni");
// Id of a class constructor
jmethodID testConstruct = env->GetMethodID(cls, "<init>", "()V");
// The Java Object
// Calling the constructor, is allocated 10 MB of memory in c++ process
jobject testJavaObject = env->NewObject(cls, testConstruct);
// function DeleteLocalRef,
// In this very moment memory not free
env->DeleteLocalRef(testJavaObject);
env->DeleteLocalRef(cls);
// 1!!!!!!!!!!!!!
res = jvm->DestroyJavaVM();
// In this very moment memory not free. TO ///
}
}
AND Java class
Code:
import java.util.*;
public class test_jni
{
ArrayList<String> testStringList;
test_jni()
{
System.out.println("start constructor");
testStringList = new ArrayList<String>();
for(int i = 0; i < 1000000; ++i)
{
testStringList.add("TEEEEEEEEEEEEEEEEST");
}
}
}