简体   繁体   English

如何在Java + JNI + C ++进程中查找内存泄漏

[英]How to Find memory leaks in Java + JNI + C++ process

I have project written in java that using JNI uses a C++ library. 我有一个用Java编写的项目,它使用JNI使用C ++库。 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. 我确定内存泄漏,通常使用Valgrind,但似乎他不能很好地处理Java,甚至认为对于“ hello world” java项目,JVM也在泄漏。

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. 我已经用单元测试测试了C ++部件(主要流程),并在单元测试中使用了valgrind,但是找不到任何泄漏。 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 ? 知道谁在消耗内存,java或本机部分将非常有帮助。 they are in the same process. 他们在同一过程中。

Thanks. 谢谢。

You can use jemalloc to debug native memory leaks . 您可以使用jemalloc 调试本机内存泄漏 This blog post has a detailed example of using jemalloc to debug a native memory leak in java applications. 这篇博客文章有一个使用jemalloc调试Java应用程序中本机内存泄漏的详细示例。

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. 根据我的经验,Valgrind实际上可以与JVM一起使用,并且它仍然是捕获C / C ++代码中泄漏的最佳工具,即使是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 . 如果您无法同时使用JNI和Valgrind,请参考Valgrind和Java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM