如何在 Java + JNI + C++ 进程中查找内存泄漏
我有一个用 Java 编写的项目,它使用 JNI 使用 C++ 库.所有的代码都是我们写的,所以我有所有的源代码.
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.
我确定有内存泄漏,通常我使用Valgrind,但似乎他不能很好地处理Java并且认为JVM正在泄漏,即使对于hello world"java项目也是如此.
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.
我已经使用单元测试测试了 C++ 部分(主要流程),并在单元测试中使用了 valgrind,但找不到任何泄漏.这并不能证明任何事情,因为我可能会错过很多潜在的流量.
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 ?
知道谁在消耗内存是非常有帮助的,是 java 还是 native 部分?他们在同一个进程中.
It will be extremely helpful to know who is consuming the memory, the java or the native part ? they are in the same process.
谢谢.
推荐答案
您可以使用 jemalloc 来调试本机内存泄漏.这篇博文 有一个使用 jemalloc 调试 java 应用程序中的本机内存泄漏的详细示例.
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.
相关文章