比较 Java 内存堆转储:Java 桌面应用程序的内存分析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1774240/
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
Comparing java memory heap dumps: Memory profiling for Java desktop application
提问by bguiz
This is a more specific question to follow up on another question that I have asked recently. A correct answer for this question will earn a correct answer for that previous question too (since that is still in limbo)!
这是一个更具体的问题,用于跟进我最近提出的另一个问题。这个问题的正确答案也将获得上一个问题的正确答案(因为它仍然处于不确定状态)!
Basically I have a Java desktop application with a memory leak issue. I am using the memory profiler in Netbeans IDE to profile the memory issue. These are the steps that I have taken done so far:
基本上,我有一个存在内存泄漏问题的 Java 桌面应用程序。我在 Netbeans IDE 中使用内存分析器来分析内存问题。这些是我迄今为止采取的步骤:
- Attach a new memory profiler to the Netbeans project
- Define profiling points at several careful chosen lines of code, and set them to trigger a memory heap dump
- Run the application in profiling mode
- 将新的内存分析器附加到 Netbeans 项目
- 在几个仔细选择的代码行上定义分析点,并将它们设置为触发内存堆转储
- 在分析模式下运行应用程序
The end result of this is that I have several memory dumps saved to disk in *.hproffiles. Netbeans IDE lets me peruse the contents (basic sort and search) of these memory dumps, and even lets me walk the heap, by seeing what the references contained within each instance, and what other objects reference each instance. That is all good, and I have been able to identify 1 or 2 fairly obvious memory leaks and rectify about 15% of the problem thus far.
这样做的最终结果是我将多个内存转储*.hprof文件保存到磁盘中。Netbeans IDE 让我可以仔细阅读这些内存转储的内容(基本排序和搜索),甚至让我通过查看每个实例中包含的引用以及其他对象引用每个实例来遍历堆。这一切都很好,到目前为止,我已经能够识别出 1 或 2 个相当明显的内存泄漏并纠正了大约 15% 的问题。
However, right now the method I am using relies on creating hypotheses about which objects should NOT be in memoryat a particular point of time, and then investigating those. What I am after now is a way to compare two separate heap dumps: Basically I have two heap dumps that should be almost the same, because the application has been restored to the same state.
然而,现在我使用的方法依赖于创建关于哪些对象在特定时间点不应该在内存中的假设,然后调查这些。我现在追求的是比较两个单独的堆转储的方法:基本上我有两个应该几乎相同的堆转储,因为应用程序已恢复到相同的状态。
However, one is before the memory leak, and the other after the memory leak, and so they are obviously different. If I am able to compare these two heaps using a of tool, instead of manually as I am doing now, then I need not rely on hypotheses to identify where the leaks are occuring, and can just have the tool identify them for me.
This is important for me because of the the sheer number of classes and instances involved for this particular application (700+ and millions, repsectively)
但是,一个是在内存泄漏之前,另一个是在内存泄漏之后,所以它们是明显不同的。如果我能够使用 of 工具来比较这两个堆,而不是像我现在所做的那样手动比较,那么我就不需要依赖假设来确定发生泄漏的位置,只需让该工具为我确定它们。
这对我很重要,因为此特定应用程序涉及的类和实例数量庞大(分别为 700 多个和数百万个)
Is the Netbeans IDE's profiler capable of doing this?
If not, is there a tool out there that is able to perform this task?
Netbeans IDE 的分析器是否能够执行此操作?
如果没有,是否有能够执行此任务的工具?
Thank you!
谢谢!
回答by Daniel Warmuth
There is also a free, GUItool for this task: VisualVM. It lets you take several heap dumps and then tell it to compare one with another, displaying the differing contents as a list, with a graphical representation of each element's share of used memory. Also, interactively browsing the heap dump difference is much more comfortable than with jhat.
还有一个用于此任务的免费 GUI工具:VisualVM。它允许您进行多个堆转储,然后告诉它比较一个和另一个,将不同的内容显示为一个列表,并以图形表示每个元素的已用内存份额。此外,以交互方式浏览堆转储差异比使用 jhat 舒服得多。
回答by broschb
You could use jhat. Specifically look at the option(-baseline baseline-dump-file) on the page I reference it says the following:
你可以使用jhat。具体查看我引用的页面上的选项(-baseline baseline-dump-file),它说如下:
"Specify a baseline heap dump. Objects in both heap dumps with the same object ID will be marked as not being "new". Other objects will be marked as "new". This is useful while comparing two different heap dumps."
“指定基线堆转储。具有相同对象 ID 的两个堆转储中的对象将被标记为不是“新”。其他对象将被标记为“新”。这在比较两个不同的堆转储时很有用。”
this may help when comparing the two heap dumps.
这在比较两个堆转储时可能会有所帮助。
回答by Stephen Denne
YourKit can compare heap dumps.
YourKit 可以比较堆转储。


