当Java中出现堆溢出时,如何判断是什么在使用内存?
我对分析有一点了解,但我特别感兴趣的是,当我收到这些堆溢出异常时,拥有所有内存的是什么。
我将在大约一个小时的调试后开始获取它们。我希望有某种转储或其他东西,我可以用它来获取程序启动时周围实例的列表。
顺便说一句,如果这是一个懒惰的问题,我很抱歉。我真的应该花一些时间来学习分析。
格雷
I know a little about profiling, but what I am particularlly insterested in, is what has all the memory when I get these heap over flow exceptions.
I will start getting them after about a hour of debugging. I am hoping there is some sort of dump or something, that I can use to get a list of what instances are around at the time the program starts.
By the way, sorry if this is a lazy question. I really shoud put sometime in learning about profiling.
Grae
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
jmap -dump
生成堆转储。您可以使用各种程序分析堆转储,例如 jhat 或 VisualVM(两者都随 JDK 提供)。那里还有很多东西供你谷歌搜索。 :-)
You can use
jmap -dump
to generate heap dumps. And you can analyse the heap dumps using a variety of programs, such as jhat or VisualVM (both of which come with the JDK).There, lots more things for you to Google up now. :-)
请注意,OutOfMemoryError 的类型很重要。它有很多变体。阅读异常附带的消息。它可能是“Java堆空间”,表示与堆相关的错误,但它也可能有另一条消息,指的是其他内存区域(请参见这篇文章 例如)。
假设存在堆分配问题,您可以使用 JVM 标志 XX:+HeapDumpOnOutOfMemoryError 以便在错误发生时获取完整的堆转储。然后,您可以使用一些堆转储阅读器并分析结果(VisualVM 或 jhat 用于例子)。
正如其他回复提到的,您也可以使用命令行工具 jmap。我将从 jmap -histo 开始,它创建不同类的简单直方图。如果您看到某个类的实例数量不合理,则怀疑存在内存泄漏。
Note that the type of the OutOfMemoryError is important. There are many variations of it. Read the message that comes with the exception. It may be "Java heap space", which indicates a heap related error, but it can also have another message, referring to other memory areas (See this post for example).
Assuming that there's a heap alocation problem, you can use the JVM flag XX:+HeapDumpOnOutOfMemoryError in order to get a full heap dump when the error occurs. Then, you can use some heap dump reader and analyze the results (VisualVM or jhat for example).
As the other response mentions, you can use the command line tool jmap as well. I would start with jmap -histo, which creates a simple histogram of the different classes. If you see a class with an unreasonable number of instances, you have a memory leak suspect.
安装 JRockit VM,然后使用 JRockit 命令中心:)它真的非常非常好,并给出了方法名称和这些方法消耗的 CPU 时间的列表,以及系统中浮动的对象。
Install the JRockit VM and then use JRockit command center :) It's really, really good, and gives a list of method names and the CPU time these methods are consuming, as well what objects are floating around in the system.
Sun Java 6 JDK 中最有用的工具之一是“jvisualvm”,它可以附加到先前启动的进程并执行解决此问题所需的内存分析。
(您也可以手动下载 VisualVM,但 JDK 可能是最简单的启动和运行方法)。
请参阅 http://download.oracle.com/javase/ 6/docs/technotes/guides/visualvm/applications_local.html 了解初始使用说明。
One of the most useful tools in the Sun Java 6 JDK is "jvisualvm" which can attach to a previously started process and do the memory profiling you need to do to resolve this issue.
(You can also download VisualVM manually, but the JDK is probably the easiest way to get up and running).
See http://download.oracle.com/javase/6/docs/technotes/guides/visualvm/applications_local.html for initial usage notes.
VisualVM 是免费且有用的。 JProfiler 和 YourKit 不是免费的,而且更有用。
VisualVM is free and useful. JProfiler and YourKit are non-free and more useful.