Tools for native memory leak analysis
Asked Answered
P

6

10

I am suspecting a native memory leak in my java code. Are there any tools which do native memory profiling? Also, does any tool support native memory analysis of a running java process?

Thanks!!

Edit: I have already tried Memory Validator and Purify but it seems they support only 32-bit processes. Is there some tool similar to the above ones which can simply attach to a running windows process and give us native memory analysis for that particular process?

Plutus answered 28/7, 2011 at 17:55 Comment(0)
M
4

The Troubleshooting guide for Java SE 6 with Hotspot VM contains a fairly elaborate section on techniques to aid in detecting native memory leaks. These include:

  • wrapping all memory allocation and deallocation calls to track the amount of memory used.
  • relying on platform specific support like the debug support provided by the Microsoft Visual C++ compiler or on mtrace (and MALLOC_TRACE) to debug memory allocations on Linux.
  • using memory leak analysis tools like Rational Purify.

among others. Notably, the article mentions that no ideal solution exists for all platforms.

Also, consider using the -Xcheck:jni flag that appears to be available in most JVMs. The -X flag itself indicates that the flag is non-standard, but the flag appears to be available in the IBM JDK, Oracle JRockit R28, and even the Oracle/Sun JVM. Enabling the flag switches on a mode where wrappers are added around JNI calls, thereby allowing you to track illegal arguments passed to JVM calls as noted in the JNI programmers' guide and specification. While, it's use in detecting memory leaks is subjective, it would definitely help if you suspect that the leak is being caused due to invalid parameters being issued.

Mime answered 28/7, 2011 at 18:55 Comment(0)
O
2

AFAIK you can't do it with Java tools like JProfiler, JVisualVM etc. If you have memory leak in native code use tools for native code. You ie. can run it from C (i.e. loading jvm.dll). You can look at this articles finding memory leaks using Visual Studio or Memory Leak Detection in C++ (Linux)

Note: of course if you leak is connected to heap leak (forgot about deleteglobalref) you can find it with Java tools, but it is very rare in JNI.

Openhearth answered 28/7, 2011 at 18:18 Comment(0)
S
1

I have been working on an open-source project named "MySafe" (https://github.com/serkan-ozal/mysafe) It basicly intercepts and monitors "Unsafe" calls. (In fact, it makes more than). With version 2.0, it can be useful for tracking and detecting "Unsafe" based native memory leaks.

Demo code: https://github.com/serkan-ozal/mysafe/blob/master/src/test/java/tr/com/serkanozal/mysafe/NativeMemoryLeakHuntingDemo.java

Diagram showing source of the leak: https://github.com/serkan-ozal/mysafe/blob/master/src/test/resources/native-memory-leak-hunting.png

Sherwin answered 9/7, 2016 at 13:19 Comment(0)
C
0

To diagnose native memory leak, JIT code symbol mapping and Linux recent profiling tools are required: perf, perf-map-agent and bcc.

Please refer to details in related answer https://mcmap.net/q/1169248/-find-native-memory-leak-in-java-application-using-jemalloc

Many thanks to Brendan Gregg

Cohobate answered 11/10, 2018 at 19:52 Comment(0)
M
0

These are tools you can use for debugging

  1. libtcmalloc HPROF: For heap profiling
  2. The jcmd Utility, PSS for the process: Can help in confirming a native leak.
  3. Native Memory Tracking: Tracking native memory leak in JVM (only works for allocation inside JVM)
  4. Core dump analysis, pmap and gdb checking for anon blocks and process memory overtime
  5. -Xcheck:jni

Further details can be found out here https://www.bro-code.in/blog/java-debugging-native-memory-leak http://www.oracle.com/technetwork/java/javase/memleaks-137499.html#gbyvk

Magdalen answered 4/2, 2021 at 8:48 Comment(0)
A
-6

I'm a big fan of JProfiler. That's the best tool for profiling and memory leaks. It's fairly cheap relative to most tools, really easy to learn, and lots of features.

http://www.ej-technologies.com/products/jprofiler/overview.html

Antilog answered 28/7, 2011 at 18:9 Comment(1)
JProfiler is a good profiler which also allows tracking of object creation. But thats not related to native heap allocations.Showers

© 2022 - 2024 — McMap. All rights reserved.