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.

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

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

Additional Reference:


  1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students.

  2. Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
    Java training in Chennai

    Java training in Bangalore

  3. 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

  4. 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

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

  6. Such great information for blogger iam a professional blogger thanks…

    Get Web Methods Training in Bangalore from Real Time Industry Experts with 100% Placement Assistance in MNC Companies. Book your Free Demo with Softgen Infotech.