C内存泄漏问题,valgrind测试(JNI)

时间:2015-02-26 16:48:21

标签: java c memory-leaks java-native-interface valgrind

我通过JNI调用它来使用JAVA中的C库。我注意到内存大小不断增加,并决定使用valgrind检查内存泄漏。

这是我从Java调用的代码片段。

JNIEXPORT jstring JNICALL Java_GenderService_genderize
  (JNIEnv *env, jobject obj, jstring string)
{
     const char *str = (*env)->GetStringUTFChars(env, string, 0);
     char cap[128];
     strcpy(cap, str);
     (*env)->ReleaseStringUTFChars(env, string, str);

     int gender = get_gender_utf8 (cap,
              GENDER_COMPARE_EXPANDED_UMLAUTS, GENDER_DEFAULT_COUNTRY);

     char* s;
switch (gender)
    {
      case IS_FEMALE        :  s = "FEMALE";            break;
      case IS_MOSTLY_FEMALE :  s = "MOSTLY_FEMALE";     break;
      case IS_MALE          :  s = "MALE";              break;
      case IS_MOSTLY_MALE   :  s = "MOSTLY_MALE";       break;
      case IS_A_COUPLE      :  s = "COUPLE";            break;
      case IS_UNISEX_NAME   :  s = "UNISEX";        break;

      case EQUIVALENT_NAMES :  s = "names are equivalent"; break;
      case NOT_EQUAL_NAMES  :  s = "names are not equal";  break;
      case NAME_NOT_FOUND   :  s = "NAME_NOT_FOUND";       break;
      case ERROR_IN_NAME    :  s = "ERROR_IN_NAME";        break;

      case INTERNAL_ERROR_GENDER :  s = "INTERNAL_ERROR";  break;
      default                    :  s = "UNKNOWN_ERROR";   break;
    }

     return (*env)->NewStringUTF(env, s);
}

我独立地测试了get_gender_utf8()方法,将其包装在main()中并使用valgrind进行调用,结果是没有发现泄漏。

int main (int argc, char *argv[])
{

    char cap[128];
    strcpy(cap, argv[1]);

    int i = get_gender_utf8 (cap,
              GENDER_COMPARE_EXPANDED_UMLAUTS, GENDER_DEFAULT_COUNTRY);

    printf("%d", i);
    char* s;
    s = "FEMALE";
  return (0);
}

=

valgrind --tool=memcheck --leak-check=full ./a.out Dan

65368== Memcheck, a memory error detector
==65368== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==65368== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==65368== Command: ./a.out Dan
==65368==
63==65368==
==65368== HEAP SUMMARY:
==65368==     in use at exit: 0 bytes in 0 blocks
==65368==   total heap usage: 1 allocs, 1 frees, 568 bytes allocated
==65368==
==65368== All heap blocks were freed -- no leaks are possible
==65368==
==65368== For counts of detected and suppressed errors, rerun with: -v
==65368== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)

=

这是否意味着泄漏在这些线路的某处?

const char *str = (*env)->GetStringUTFChars(env, string, 0);
char cap[128];
strcpy(cap, str);
(*env)->ReleaseStringUTFChars(env, string, str);
char* s;
return (*env)->NewStringUTF(env, s);

频繁的完整GC日志。

434711.391: [Full GC 463295K->463264K(463296K), 1.4745520 secs]
434712.868: [Full GC 463296K->463264K(463296K), 1.4686460 secs]
434724.339: [Full GC 463295K->463264K(463296K), 1.5353370 secs]
434735.877: [Full GC 463295K->463264K(463296K), 1.5300430 secs]
434737.409: [Full GC 463296K->463264K(463296K), 1.4186100 secs]
434748.830: [Full GC 463295K->463265K(463296K), 1.6480810 secs]
434760.481: [Full GC 463295K->463264K(463296K), 1.4662190 secs]
434771.949: [Full GC 463295K->463264K(463296K), 1.4691700 secs]
434773.420: [Full GC 463295K->463265K(463296K), 1.4697440 secs]
434784.893: [Full GC 463295K->463264K(463296K), 1.6079780 secs]
434796.502: [Full GC 463295K->463264K(463296K), 1.5225740 secs]
434798.027: [Full GC 463295K->463265K(463296K), 1.3949160 secs]

感谢您的任何帮助和建议!

0 个答案:

没有答案