Java 如何分析 .hprof 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/185893/
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
How do I analyze a .hprof file?
提问by Nick Stinemates
I have a production server running with the following flag: -XX:+HeapDumpOnOutOfMemoryError
我有一个使用以下标志运行的生产服务器: - XX:+HeapDumpOnOutOfMemoryError
Last night it generated a java-38942.hprof file when our server encountered a heap error. It turns out that the developers of the system knew of the flag but no way to get any useful information from it.
昨晚,当我们的服务器遇到堆错误时,它生成了一个 java-38942.hprof 文件。事实证明,系统的开发人员知道该标志,但无法从中获取任何有用的信息。
Any ideas?
有任何想法吗?
采纳答案by Cowan
If you want a fairly advanced tool to do some serious poking around, look at the Memory Analyzer projectat Eclipse, contributed to them by SAP.
如果您想要一个相当高级的工具来进行一些认真的探索,请查看Eclipse中的 Memory Analyzer 项目,该项目由 SAP 提供。
Some of what you can do is mind-blowingly good for finding memory leaks etc -- including running a form of limited SQL (OQL) against the in-memory objects, i.e.
您可以做的一些事情对于查找内存泄漏等非常有用 - 包括针对内存对象运行一种形式的受限 SQL (OQL),即
SELECT toString(firstName) FROM com.yourcompany.somepackage.User
SELECT toString(firstName) FROM com.yourcompany.somepackage.User
Totally brilliant.
非常棒。
回答by CMS
You can use JHAT, The Java Heap Analysis Tool provided by default with the JDK. It's command line but starts a web server/browser you use to examine the memory. Not the most user friendly, but at least it's already installed most places you'll go. A very useful view is the "heap histogram" link at the very bottom.
您可以使用JHAT,即 JDK 默认提供的 Java 堆分析工具。它是命令行,但会启动用于检查内存的 Web 服务器/浏览器。不是最用户友好的,但至少它已经安装在你要去的大多数地方。一个非常有用的视图是最底部的“堆直方图”链接。
ex: jhat -port 7401 -J-Xmx4G dump.hprof
前任: jhat -port 7401 -J-Xmx4G dump.hprof
jhat
can execute OQL "these days" as well (bottom link "execute OQL")
jhat
也可以“这些天”执行 OQL(底部链接“执行 OQL”)
回答by kohlerm
Just get the Eclipse Memory Analyzer. There's nothing better out there and it's free.
只需获取Eclipse 内存分析器。没有比这更好的了,而且是免费的。
JHAT is only usable for "toy applications"
JHAT 仅可用于“玩具应用程序”
回答by James Schek
You can also use HeapWalkerfrom the Netbeans Profiler or the Visual VMstand-alone tool. Visual VM is a good alternative to JHAT as it is stand alone, but is much easier to use than JHAT.
您还可以使用来自 Netbeans Profiler 或Visual VM独立工具的HeapWalker。Visual VM 是 JHAT 的一个很好的替代品,因为它是独立的,但比 JHAT 更容易使用。
You need Java 6+ to fully use Visual VM.
您需要 Java 6+ 才能完全使用 Visual VM。
回答by Polaris
YourKit Java Profiler seems to handle them too.
YourKit Java Profiler 似乎也可以处理它们。
回答by Andrejs
If you want to do a custom analysis of your heapdump then there's:
如果你想对你的 heapdump 进行自定义分析,那么有:
- JVM Heap Dump Analysis libraryhttps://github.com/aragozin/jvm-tools/tree/master/hprof-heap
This library is fast but you will need to write your analysis code in Java.
这个库速度很快,但你需要用 Java 编写分析代码。
From the docs:
从文档:
- Does not create any temporary files on disk to process heap dump
- Can work directly GZ compressed heap dumps
- HeapPath notation
- 不在磁盘上创建任何临时文件来处理堆转储
- 可以直接工作 GZ 压缩堆转储
- 堆路径表示法
回答by Waleed
I personally prefer VisualVM. One of the features I like in VisualVM is heap dump comparison. When you are doing a heap dump analysis there are various ways to go about figuring out what caused the crash. One of the ways I have found useful is doing a comparison of healthy vs unhealthy heap dumps.
我个人更喜欢 VisualVM。我喜欢 VisualVM 的功能之一是堆转储比较。在进行堆转储分析时,有多种方法可以找出导致崩溃的原因。我发现有用的方法之一是比较健康和不健康的堆转储。
Following are the steps you can follow for it :
以下是您可以遵循的步骤:
- Getting a heap dump of OutOfMemoryError let's call it "oome.hprof". You can get this via JVM parameter HeapDumpOnOutOfMemoryError.
- Restart the application let it run for a big (minutes/hours) depending on your application. Get another heap dump while the application is still running. Let's call it "healthy.hprof".
- You can open both these dumps in VisualVM and do a heap dump comparison. You can do it on class or package level. This can often point you into the direction of the issue.
- 获取 OutOfMemoryError 的堆转储,我们称之为“oome.hprof”。您可以通过 JVM 参数 HeapDumpOnOutOfMemoryError 获取此信息。
- 重新启动应用程序,让它根据您的应用程序运行很长时间(分钟/小时)。在应用程序仍在运行时获取另一个堆转储。我们称之为“healthy.hprof”。
- 您可以在 VisualVM 中打开这两个转储并进行堆转储比较。您可以在类或包级别执行此操作。这通常可以为您指明问题的方向。
link : https://visualvm.github.io