Tuesday, July 10, 2012

Fixing JNI Memory Leaks


During initial stages of my coding the application that I programmed use to re-start due to Java Exception "java.lang.OutOfMemoryerror". After much analysis, we found that making frequent JNI function calls caused the java.lang.OutOfMemoryError.

Reason for the problem - JNI Memory Leak
The reference for any object created in Java and sent to JNI native functions were not de-referenced automatically. Since these objects were not de-referenced, they couldn't be cleared by GC which caused the memory leak.

Solution
Please refer following sample code to free the memory.

JNIEXPORT jobject JNICALL Java_sample_package_SampleJavaClass_classMethod
(JNIEnv * env, jobject obj, jstring jstrString)
{
    const jchar* szString = (jchar*)env->GetStringChars(jstrString, 0);
    env->ReleaseStringChars(jstrString, (const jchar *)szString);
   
    // De-Referencing the String object
    env->DeleteLocalRef(jstrString);
}


Results
Considerable amount of Memory Leaks were fixed. Throughput is not 100%. 

Additional Reference:
http://www.dynamicobjects.com/d2r/archives/001816.html


8 comments:

  1. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing..
    Believe me I did wrote an post about tutorials for beginners with reference of your blog. 




    Selenium training in bangalore
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  2. Thanks For Sharing The information The Information Shared Is Very Valuable Please Keep Updating us The Information shared Is Very Valuable Python Online Course Data Science Online Course Aws Online Course

    ReplyDelete
  3. Nice Content
    For Data Science training in Bangalore, Visit:
    Data Science training in Bangalore

    ReplyDelete
  4. Manage yourself when concern. Office within trade style enjoy. Word dark environmental dinner admit.world-news

    ReplyDelete