比较 Java 内存堆转储:Java 桌面应用程序的内存分析
这是一个更具体的问题,需要跟进[我最近问过的另一个问题] 1 。这个问题的正确答案也将为上一个问题赢得正确的答案(因为这仍然悬而未决)!
基本上我有一个 Java 桌面应用程序存在内存泄漏问题。我正在使用 NetBeans IDE 中的内存分析器来分析内存问题。以下是我到目前为止已采取的步骤:
- 将新的内存分析器附加到 NetBeans 项目
- 在几行精心选择的代码处定义分析点,并将它们设置为触发内存堆转储
- 在分析模式下运行应用程序
最终结果其中之一是我有几个内存转储保存到磁盘的 *.hprof
文件中。 NetBeans IDE 让我可以仔细研究这些内存转储的内容(基本排序和搜索),甚至可以让我通过查看每个实例中包含的引用以及引用每个实例的其他对象来遍历堆 。这一切都很好,到目前为止,我已经能够识别 1 或 2 个相当明显的内存泄漏,并纠正了大约 15% 的问题。
然而,现在我使用的方法依赖于创建关于哪些对象不应该在特定时间点内存中的假设,然后进行调查。我现在追求的是一种比较两个单独的堆转储的方法:基本上我有两个堆转储应该几乎相同,因为应用程序已恢复到相同的状态。
但是,一个是内存泄漏之前,一个是内存泄漏之后,所以它们是明显不同的。如果我能够使用工具比较这两个堆,而不是像我现在所做的那样手动比较,那么我就不需要依靠假设来识别泄漏发生的位置,并且可以只得到工具为我识别它们。
这对我来说很重要,因为该特定应用程序涉及的类和实例数量巨大(分别为 700 多个和数百万)
NetBeans IDE 的探查器能够执行此操作吗?
如果没有,是否有工具可以执行此任务?
This is a more specific question to follow up on [another question that I have asked recently] 1. 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:
- 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
The end result of this is that I have several memory dumps saved to disk in *.hprof
files. 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.
However, right now the method I am using relies on creating hypotheses about which objects should NOT be in memory at 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 occurring, 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, respectively)
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
还有一个用于此任务的免费 GUI 工具:VisualVM。它允许您进行多个堆转储,然后告诉它进行比较,将不同的内容显示为列表,并以图形方式表示每个元素所占的已用内存份额。此外,以交互方式浏览堆转储差异比使用 jhat 更舒适。
There is also a free, GUI tool 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.
您可以使用 jhat。具体查看我引用的页面上的选项(-baseline benchmark-dump-file),它的内容如下:
“指定基线堆转储。两个堆转储中具有相同对象 ID 的对象将被标记为不是“新的”其他对象将被标记为“新”,这在比较两个不同的堆转储时很有用。
这在比较两个堆转储时可能会有所帮助。
You could use jhat. Specifically look at the option(-baseline baseline-dump-file) on the page I reference it says the following:
"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."
this may help when comparing the two heap dumps.
YourKit 可以比较堆转储。
YourKit can compare heap dumps.