当Java中出现堆溢出时,如何判断是什么在使用内存?

发布于 2024-10-09 14:11:45 字数 174 浏览 3 评论 0原文

我对分析有一点了解,但我特别感兴趣的是,当我收到这些堆溢出异常时,拥有所有内存的是什么。

我将在大约一个小时的调试后开始获取它们。我希望有某种转储或其他东西,我可以用它来获取程序启动时周围实例的列表。

顺便说一句,如果这是一个懒惰的问题,我很抱歉。我真的应该花一些时间来学习分析。

格雷

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小巷里的女流氓 2024-10-16 14:11:45

您可以使用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. :-)

不打扰别人 2024-10-16 14:11:45

请注意,OutOfMemoryError 的类型很重要。它有很多变体。阅读异常附带的消息。它可能是“Java堆空间”,表示与堆相关的错误,但它也可能有另一条消息,指的是其他内存区域(请参见这篇文章 例如)。

假设存在堆分配问题,您可以使用 JVM 标志 XX:+HeapDumpOnOutOfMemoryError 以便在错误发生时获取完整的堆转储。然后,您可以使用一些堆转储阅读器并分析结果(VisualVMjhat 用于例子)。

正如其他回复提到的,您也可以使用命令行工具 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.

掩耳倾听 2024-10-16 14:11:45

安装 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.

alt text

↘紸啶 2024-10-16 14:11:45

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.

羁〃客ぐ 2024-10-16 14:11:45

VisualVM 是免费且有用的。 JProfiler 和 YourKit 不是免费的,而且更有用。

VisualVM is free and useful. JProfiler and YourKit are non-free and more useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文