JNI Memory leak jni ERROR (App bug): Local reference table overflow (max=512)

Source: Internet
Author: User

The reason is that there is no immediate release of the object, the original code is this

Static Jobject getmaps (jnienv *env,jclass obj) {jclass Stringbuilder_class = (*env)->findclass (env, "java/lang/ StringBuilder "); Jmethodid Init_stringbuilder_method = (*env)->getmethodid (Env,stringbuilder_class," <init > "," () V "); Jobject stringbuilder_obj= (*env)->newobject (Env,stringbuilder_class,init_stringbuilder_method); Jmethodid Append_method = (*env)->getmethodid (Env,stringbuilder_class, "Append", "(ljava/lang/object;) Ljava/lang /stringbuilder; "); FILE * Filep;char item[200]= "II"; Filep = fopen ("/proc/self/maps", "R"), if (!FILEP) return Stringbuilder_obj;while (fgets (Item,sizeof (item), FILEP)!=null) {jstring str = (*env)->newstringutf (env,item);(*env)->callobjectmethod (env, STRINGBUILDER_OBJ,APPEND_METHOD,STR);(*env)->releasestringutfchars (env,str,item);(*env)->DeleteLocalRef ( ENV,STR);} return stringbuilder_obj;}

It seems like no problem to look directly, but every call (*env)->callobjectmethod (ENV,STRINGBUILDER_OBJ,APPEND_METHOD,STR)

Will produce an object, I thought it was okay at first, change it so the program will not collapse

Static Jobject getmaps (jnienv *env,jclass obj) {jclass Stringbuilder_class = (*env)->findclass (env, "java/lang/ StringBuilder "); Jmethodid Init_stringbuilder_method = (*env)->getmethodid (Env,stringbuilder_class," <init > "," () V "); Jobject stringbuilder_obj= (*env)->newobject (Env,stringbuilder_class,init_stringbuilder_method); Jmethodid Append_method = (*env)->getmethodid (Env,stringbuilder_class, "Append", "(ljava/lang/object;) Ljava/lang /stringbuilder; "); FILE * Filep;char item[200]= "II"; Filep = fopen ("/proc/self/maps", "R"), if (!FILEP) return Stringbuilder_obj;while (fgets (Item,sizeof (item), FILEP)!=null) {jstring str = (*env)->newstringutf (Env,item);//Add a temp to release jobject temp= (*env)- >callobjectmethod (ENV,STRINGBUILDER_OBJ,APPEND_METHOD,STR);(*env)->releasestringutfchars (Env,str,item);( *env)->deletelocalref (env,str);(*env)->deletelocalref (env,temp);} return stringbuilder_obj;}

Jni memory leak jni ERROR (App bug): Local reference table overflow (max=512)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.