title: Java JNI leak in c++ process
body: <p>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...</p>
  
  <p>So.. i did simple example.. </p>
  
  <p>1) I create a java machine (jint res = JNI_CreateJavaVM(&amp;jvm, (void**)&amp;env, &amp;vm_args);)</p>
  
  <p>2) then i take a pointer on java class (jclass cls = env->FindClass("test_jni"));</p>
  
  <p>3) after that i create a java class object object, by calling the constructor (testJavaObject = env->NewObject(cls, testConstruct);)</p>
  
  <p>AT THIS very moment in the process of c++ program is allocated 10 MB of memory</p>
  
  <p>4) Next i delete the class , the object, and the Java Machine .. </p>
  
  <p>AT THIS very moment the 10 MB of memory are not free 
  .................
  So below i have a few lines of code</p>
  
  <p>c++ program</p>
  
  <pre><code>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;
          // alloc part of memory (for test) before CreateJavaVM
          char* testMem0 = new char[1000];
          for(int i = 0; i &lt; 1000; ++i)
              testMem0[i] = 'a';
          // create java VM
          jint res = JNI_CreateJavaVM(&amp;jvm, (void**)&amp;env, &amp;vm_args);
          // alloc part of memory (for test) after CreateJavaVM
          char* testMem1 = new char[1000];
          for(int i = 0; i &lt; 1000; ++i)
              testMem1[i] = 'b';
          //Creating java virtual machine
          jclass cls = env-&gt;FindClass("test_jni");
          // Id of a class constructor
          jmethodID testConstruct = env-&gt;GetMethodID(cls, "&lt;init&gt;", "()V");
          // The Java Object
          // Calling the constructor, is allocated 10 MB of memory in c++ process
          jobject testJavaObject = env-&gt;NewObject(cls, testConstruct);
          // function DeleteLocalRef,
          // In this very moment memory not free
          env-&gt;DeleteLocalRef(testJavaObject);
          env-&gt;DeleteLocalRef(cls);
          // 1!!!!!!!!!!!!!
          res = jvm-&gt;DestroyJavaVM();
          delete[] testMem0;
          delete[] testMem1;
          // In this very moment memory not free. TO ///
      }
      int gg = 0;
  }
  </code></pre>
  
  <p>java class (it just allocs some memory)</p>
  
  <pre><code>import java.util.*;
  
  public class test_jni
  {
    ArrayList&lt;String&gt; testStringList;
    test_jni()
    {
      System.out.println("start constructor");
      testStringList = new ArrayList&lt;String&gt;();
      for(int i = 0; i &lt; 1000000; ++i)
      {
        // засераю память
        testStringList.add("TEEEEEEEEEEEEEEEEST");
      }
    }
  }
  </code></pre>
  
  <p>process memory view, after crating javaVM and java object:
  testMem0 and testMem1 - test memory, that's allocated by c++.</p>
  
  <pre><code>**************
  testMem0
  **************
  
  
  
  
  JNI_CreateJavaVM
  
  
  
  
  **************
  testMem1
  **************
  
  // create java object
  jobject testJavaObject = env-&gt;NewObject(cls, testConstruct);
  
  **************
  </code></pre>
  
  <p>process memory view, after destroy javaVM and delete ref on java object:
  testMem0 and testMem1 are deleted to;</p>
  
  <pre><code>**************
  
  
  
  
  JNI_CreateJavaVM
  
  
  
  
  **************
  
  // create java object
      jobject testJavaObject = env-&gt;NewObject(cls, testConstruct);
  
  **************
  </code></pre>
  
  <p>So testMem0 and testMem1 is deleted, But JavaVM and
  Java object not....</p>
  
  <p>Sow what i do wrong... and how i can free memory in the c++ process program.</p>
  
  <p>SOME EDIT.... </p>
  
  <p>if i alloc new memory (char* test3 = new char[1000]), after destroying JVM, heap of c++ process looks likes so:</p>
  
  <p>And memory of process is growing up !...</p>
  
  <pre><code>**************
  
  
  
  
  JNI_CreateJavaVM (memory after JVM)
  
  
  
  
  **************
  
  jobject testJavaObject = env-&gt;NewObject(cls, testConstruct);
  memory after Java object
  
  
  **************
  
  char* test3 (memory is allocated by char* test3 = new char[1000])
  
  **************
  </code></pre>