How to Find memory leaks in Java + JNI + C++ proce

2019-03-02 05:07发布

问题:

I have project written in java that using JNI uses a C++ library. All the code was written by us, so I have all the source code.

After few hours the machine runs out of memory although my process just iterate over files and all the memory regarding the previous file deleted.

I'm sure that there is a memory leak, usually I use Valgrind, but it seems he can't cope with Java very well and believes that the JVM is leaking, even for "hello world" java project.

I've tested the C++ parts (the major flows) with unit-tests and used valgrind on the unit-tests, but couldn't find any leakage. It doesn't prove anything because there are many potential flows I could've missed.

My major question, how can I find my leak ?

It will be extremely helpful to know who is consuming the memory, the java or the native part ? they are in the same process.

Thanks.

回答1:

From my experience, Valgrind is actually usable with the JVM, and it remains the best tool to hunt leaks in C/C++ code, even with JNI. So your question kinda contains the answer that you need ;)

If you fail to use JNI and Valgrind together, please refer to Valgrind and Java.



回答2:

You can use jemalloc to debug native memory leaks. This blog post has a detailed example of using jemalloc to debug a native memory leak in java applications.