6

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.

OopsUser
  • 4,642
  • 7
  • 46
  • 71
  • If you believe that you have sufficiently tested the C++ part of your application, and you have concluded, to your satisfaction, that the C++ part of your application does not leak memory, then the only logical conclusion is that your Java application is using up all available memory. There are no other possibilities. – Sam Varshavchik Oct 25 '15 at 19:59
  • The c++ library was written over few months, It has many flows, although we tried our best to test every flow, we've might missed some, test coverage is not perfect. – OopsUser Oct 25 '15 at 20:03
  • For the Java side of things you can use a profiler like [VisualVM](https://visualvm.java.net/). Install it, start it and then start your application and check out the process. – hotzst Oct 25 '15 at 20:37
  • You could try a heap profiler like https://gperftools.googlecode.com/svn/trunk/doc/heapprofile.html. – znkr Oct 25 '15 at 22:04

2 Answers2

2

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.

Community
  • 1
  • 1
Martin Quinson
  • 1,347
  • 8
  • 16
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.

Lari Hotari
  • 5,190
  • 1
  • 36
  • 43
  • do u know how to create jemalloc.so file or from where I can download it. Actually I can't install jemalloc using apt-get in our env. So I need to use it manually by using jemalloc.so. Please correct me if I am wrong. – Prasanta Barman Apr 21 '17 at 05:56
  • @PrasantaBarman you can download the deb package from http://packages.ubuntu.com/trusty/amd64/libjemalloc1/download and extract it if you cannot use apt-get . It's possible to extract a deb package with dpkg -x – Lari Hotari Apr 21 '17 at 17:59
  • This answer is more detailed about Java native memory leaks: https://stackoverflow.com/a/35610063 – Lari Hotari Sep 13 '19 at 05:10