Note that there are some explanatory texts on larger screens.

plurals
  1. POMemory leak when calling java code from C using JNI
    text
    copied!<p>I have a C program that stores some object in java store using JNI. (Before someone ask, using java store is a requirment here and I have to write a client in C which would be able to add and retrieve objects from this store).</p> <p>I made the program and tried to add 100000 object of size 1KB. But after adding only 50000 objects I am getting 'out of memory' messages (please note that I am printing these 'out of memory' messages whenever I am unable to allocate a new string or byte array using NewStringUTF and NewByteArray functions). At that time my application is using only 80MB of memory. I dont why these methods are returning NULL. Is there something I am missing.</p> <p>Furthermore, the memory keeps on increasing even though I am releasing byte array and string created for java.</p> <p>Here is the source code.</p> <pre><code> void create_jvm(void) { JavaVMInitArgs vm_args; JavaVMOption vm_options; vm_options.optionString = "-Djava.class.path=c:\\Store"; vm_args.version = JNI_VERSION_1_4; vm_args.nOptions = 1; vm_args.options = &amp;vm_options; vm_args.ignoreUnrecognized = 0; JNI_CreateJavaVM(&amp;jvm, (void**)&amp;env, &amp;vm_args); if(env != null) { j_store = (*env)-&gt;FindClass(env, "com/store"); if(j_store == null) { printf("unable to find class. class name: JStore"); } } } void add(char* key, char* value, int length) { jstring j_key = (*env)-&gt;NewStringUTF(env, key); jbyteArray j_value = (*env)-&gt;NewByteArray(env, length); (*env)-&gt;SetByteArrayRegion(env, j_value, 0, length, (jbyte *)value); ret = (*env)-&gt;CallStaticBooleanMethod(env, j_store, method_id, j_key, j_value); if(j_value != null) { (*env)-&gt;ReleaseByteArrayElements(env, j_value, (jbyte *)value, 0); } if(j_key != null) { (*env)-&gt;ReleaseStringUTFChars(env, j_key, key); } } </code></pre> <p>The java side recieves the data in byte[] and stores it in a hashtable. The issue is that everytime the code runs the memory only adds up and is never released. I tried to add 1 MB object and debugged it.</p> <p>The process memory increases by 1MB when I call NewByteArray. But when CallStaticBooleanMethod is called the process memory increase by 4MB. And the call to ReleaseByteArrayElements do not release any memory at all.</p> <p>If I add another 1MB object after this, then process memory remains same when I call NewByteArray and it increase by 1MB when I call CallStaticBooleanMethod but remains the same when I try to release the byte array.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload