Search code examples
javajvmnative

Associate a Native Function calls to Java Method


We are running into native memory leak issues with a Java process.

After linking JVM with jemalloc as recommended in numerous resources like (http://www.evanjones.ca/java-native-leak-bug.html), we were able to boil down the root cause to inflateInit2_ and updatewindow calls in the zlib library.

How do we go about associating these calls from Native space to JVM methods? We already tried looking at the stack trace (hoping to get lucky) and also Flame graphs but these did not help much.


Solution

  • ZipInputStream / JarInputStream is a typical source of such memory leaks, for example, when a Class.getResourceAsStream is not closed after use.

    Create a heap dump to see who holds java.util.zip.Inflater objects.


    async-profiler recently got support for profiling native memory leaks on Linux. The following command will profile native allocations for <SEC> seconds and create a report as a FlameGraph containing combined Java+native stacks:

    ./profiler.sh -e malloc -d <SEC> -o malloc.svg <PID>