java 本机内存泄漏分析工具
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6863451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tools for native memory leak analysis
提问by cooltechnomax
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?
我怀疑我的 Java 代码中存在本机内存泄漏。是否有任何工具可以进行本机内存分析?另外,是否有任何工具支持对正在运行的 Java 进程进行本机内存分析?
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?
编辑:我已经尝试过 Memory Validator 和 Purify,但它们似乎只支持 32 位进程。是否有一些类似于上述工具的工具可以简单地附加到正在运行的 Windows 进程并为我们提供该特定进程的本机内存分析?
回答by Vineet Reynolds
The Troubleshooting guide for Java SE 6 with Hotspot VMcontains a fairly elaborate section on techniques to aid in detecting native memory leaks. These include:
Java SE 6 with Hotspot VM的故障排除指南包含一个相当详细的部分,内容涉及帮助检测本机内存泄漏的技术。这些包括:
- 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.
- 包装所有内存分配和释放调用以跟踪使用的内存量。
- 依赖于平台特定的支持,如 Microsoft Visual C++ 编译器或 mtrace(和 MALLOC_TRACE)提供的调试支持来调试 Linux 上的内存分配。
- 使用内存泄漏分析工具,如 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.
此外,请考虑使用-Xcheck:jni
在大多数 JVM 中似乎可用的标志。该-X
标志本身表明该标志是非标准的,但该标志似乎在IBM JDK、Oracle JRockit R28甚至 Oracle/Sun JVM 中可用。启用标志会打开在 JNI 调用周围添加包装器的模式,从而允许您跟踪传递给 JVM 调用的非法参数,如JNI 程序员指南和规范中所述。虽然它用于检测内存泄漏是主观的,但如果您怀疑泄漏是由于发出无效参数而引起的,它肯定会有所帮助。
回答by zacheusz
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 Studioor Memory Leak Detection in C++ (Linux)
AFAIK你不能用 JProfiler、JVisualVM 等 Java 工具来做到这一点。如果你在本机代码中有内存泄漏,请使用本机代码工具。你即。可以从 C 运行它(即加载 jvm.dll)。您可以查看使用 Visual Studio或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.
注意:当然如果你的泄漏连接到堆泄漏(忘记deleteglobalref)你可以用Java工具找到它,但在JNI中很少见。
回答by Serkan ?zal
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.
我一直在研究一个名为“MySafe”(https://github.com/serkan-ozal/mysafe)的开源项目,它基本上拦截和监控“不安全”调用。(事实上,它比)。在 2.0 版中,它可用于跟踪和检测基于“不安全”的本机内存泄漏。
演示代码: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
显示泄漏源的图表:https: //github.com/serkan-ozal/mysafe/blob/master/src/test/resources/native-memory-leak-hunting.png
回答by Yves Martin
To diagnose native memory leak, JIT code symbol mapping and Linux recent profiling tools are required: perf
, perf-map-agent
and bcc
.
要诊断本机内存泄漏,需要 JIT 代码符号映射和 Linux 最新的分析工具:perf
,perf-map-agent
和bcc
.
Please refer to details in related answer https://stackoverflow.com/a/52767721/737790
请参阅相关答案中的详细信息https://stackoverflow.com/a/52767721/737790
Many thanks to Brendan Gregg
非常感谢布伦丹格雷格
回答by chubbsondubs
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.
我是 JProfiler 的忠实粉丝。这是分析和内存泄漏的最佳工具。相对于大多数工具来说,它相当便宜,非常容易学习,而且功能很多。
http://www.ej-technologies.com/products/jprofiler/overview.html
http://www.ej-technologies.com/products/jprofiler/overview.html