title: JNI Java From C Memory Leak
body: <p>It seems that following code is leaking memory. I checked the JVM memory utilization and it is not freeing up memory after subsequent calls. When I just run the Java as standalone it works fine for multiple calls and keeps deallocating memory just fine.</p>
  
  <p>I would really appreciate any help.</p>
  
  <pre><code>jobjectArray my_obj = (jobjectArray) env-&gt;CallObjectMethod(cls, mid, qstr, pstr);
  
  length = env-&gt;GetArrayLength(my_obj);
  //printf("\nArray Length = %d \n", length);
  
  char result[256]; 
  const char *cstr;
  int numberOfCharsInThisRow = 0;
  
  
  array1 = (char **)malloc(length * sizeof(char *));
  /*Check if pointer is null, if not then free its memory first*/
  
  for(int i=0; i&lt; length ; i++){
      cstr = env-&gt;GetStringUTFChars((jstring)env-&gt;GetObjectArrayElement(my_obj,i), 0); 
  
      numberOfCharsInThisRow = std::strlen(cstr)+1;
  
  
      *(array1+i)=(char *)malloc(numberOfCharsInThisRow * sizeof(char)); 
  
      std::strcpy(result, cstr);
      std::strcpy(*(array1+i),result);
  
  
      env-&gt;ReleaseStringUTFChars((jstring)env-&gt;GetObjectArrayElement(my_obj,i), cstr);
          }
      env-&gt;DeleteLocalRef(my_obj); 
  }
  
      //printf("\n\nDestroy JVM\n\n");
      //jvm-&gt;DestroyJavaVM();
  
  }
  </code></pre>
  
  <p>void libdeallocatememory(char **array1,int length)
  {
      //printf("Free Array memory \n");</p>
  
  <pre><code>for (int j=0 ;j &lt;length ;j ++)
  {
      free(array1+j);
  }
  
  free(array1);   
  </code></pre>
  
  <p>}</p>