1

第一次使用 JNI 访问 ac 库时,我想到了使用 valgrind 来检查任何内存泄漏。Valgrind 在 jvm 中报告了很多泄漏。:(

使用 Push/Poplocal 框架来管理本地引用,当然还需要从 Get(UTFChars/Arrays) 释放引用。除此之外,它还报告 AttachCurrentThread 方法可能丢失,而实际上我通过回调函数中的 DetachCurrentThread 成功分离了它。

现在我只需要建议/解释报告的泄漏根本不是真的,不会影响我的应用程序,尤其是它在服务器端的应用程序。对我来说,我怀疑在我的应用程序退出并且 valgrind 已经完成检查部分之后,库的卸载和销毁尚未完成。希望听到任何有过这种情况的人的意见。

//示例代码

void event_callback(void .., int ..)
{

printf("--Enter event_callback Function--\n");  
int detach = 0;

//store our jnienv for this thread
JNIEnv *g_env = NULL;

// if our jni env is not attached for this thread, then we will attach it
int getEnvStat = (*g_jvm)->GetEnv(g_jvm, (void **)&g_env, JNI_VERSION_1_6);
if (getEnvStat == JNI_EDETACHED) 
{
    //if not attached, then lets attached g_env
    printf("JNIEnv not attached. Attaching..\n");

    if ((*g_jvm)->AttachCurrentThread(g_jvm, (void **) &g_env, NULL) != 0)
    {
        printf("Failed to attach\n");
        return;
    }
    else
    {
        detach = 1;
    }
}
else if (getEnvStat == JNI_OK)
{
    printf("GetEnv: jenv is just OK, already attached.\n");

} 
else if (getEnvStat == JNI_EVERSION) 
{
    printf("GetEnv: version not supported\n");
    return;
}

if((*g_env)->PushLocalFrame(g_env, 10)<0)
{
    printf("Out of memory\n");

}

//...some useful code here

(*g_env)->PopLocalFrame(g_env, NULL);

if(detach == 1)
{
        int result = (*g_jvm)->DetachCurrentThread(g_jvm);
    printf("DetachCurrentThread result: %d\n", result);
}   

printf("--Exit event_callback Function--\n");
}

报告:

==20914== 1 块中的 432 字节可能在 1,035 的丢失记录 903 中丢失

==20914== 在 0x4028876: malloc (vg_replace_malloc.c:236)

==20914== by 0x4950461: os::malloc(unsigned int) (在/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/libjvm.so)

==20914== by 0x464470C: CHeapObj::operator new(unsigned int) (在/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/libjvm.so)

==20914== by 0x496BB13: ParkEvent::Allocate(Thread*) (在/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/libjvm.so)

==20914== by 0x4A13CCD: Thread::Thread() (在/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/libjvm.so)

==20914== by 0x4A15091: JavaThread::JavaThread(bool) (在 /usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/libjvm.so)

==20914== 0x4853723:attach_current_thread.isra.103.part.104(在 /usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/libjvm.so 中)

==20914== by 0x7B17791: event_callback (在/home/Wrapper/TJNIBridge/libJNIBridge.so)

==20914== 由 0x7B44431: xmpp_event_main (my_xmpp.c:562)

==20914== by 0x412146D: 克隆 (clone.S:130)

我还在 jvm lib 的下方粘贴了一些报告的泄漏。

==20914== 1 块中的 4,612(1,284 直接,3,328 间接)字节在 1,035 的丢失记录 1,009 中肯定丢失

==20914== 在 0x4028876: malloc (vg_replace_malloc.c:236)

==20914== by 0x4950461: os::malloc(unsigned int) (在/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/libjvm.so)

==20914== by 0x464470C: CHeapObj::operator new(unsigned int) (在/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/libjvm.so)

==20914== by 0x4A14BE9:WatcherThread::start()(在 /usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/libjvm.so 中)

==20914== by 0x4A192DF: Threads::create_vm(JavaVMInitArgs*, bool*) (在 /usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/libjvm.so)

==20914== 0x4865BD8:JNI_CreateJavaVM(在 /usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/libjvm.so 中)

==20914== 由 0x41ECD30: start_thread (pthread_create.c:304)

==20914== by 0x412146D: 克隆 (clone.S:130)

==20914== 1 个块中的 8 个字节在 1,035 的丢失记录 41 中肯定丢失了

==20914== 在 0x4028876: malloc (vg_replace_malloc.c:236)

==20914== by 0x4950461: os::malloc(unsigned int) (在/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/libjvm.so)

==20914== by 0x464470C: CHeapObj::operator new(unsigned int) (在/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/libjvm.so)

==20914== by 0x470E29D: CSpaceCounters::CSpaceCounters(char const*, int, unsigned int, ContiguousSpace*, GenerationCounters*) (在 /usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386 /client/libjvm.so)

==20914== by 0x47A3BF9: DefNewGeneration::DefNewGeneration(ReservedSpace, unsigned int, int, char const*) (在 /usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/libjvm 。所以)

==20914== by 0x47FC623: GenerationSpec::init(ReservedSpace, int, GenRemSet*) (在/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/libjvm.so)

==20914== by 0x47EF9D4: GenCollectedHeap::initialize() (在/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/libjvm.so)

==20914== 由 0x4B95FF3: ??? (在/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/libjvm.so)

Ubuntu 11.10 OpenJDK 7

4

1 回答 1

0

JVM 和 Valgrind 相处不来。只需在 Valgrind 下运行 JVM 而不给我任何输入,就会产生大量警告。

您将需要为 JVM 错误生成抑制。

尝试禁用 JIT。

于 2013-11-19T16:50:30.380 回答