java.lang.OutOfMemoryError (OOM)解密 & Java heap dumps 解析 (二)

What Is a Java Heap Dump?

We've learned that a Java heap is a runtime data area where all class instances and arrays are allocated and shared among all Java Virtual Machine threads during execution of the JVM. A Java heap dump is a snapshot of a Java heap at a specific time. It's like taking a picture of a busy warehouse at a given time. If we look at the picture, we can identify what items were available at that time. Some items may be shipped to Canada a few minutes later, but you can see them in the picture because they were there at the time of the snapshot.

Because the Java specification does not mention the Java heap dump, there are different forms of Java heap dump implementations from different Java Virtual Machines. The IBM Java heap dump provides information mostly about the Java heap.

The Sun Microsystems hprof Java heap dump provides information about the Java Virtual Machine stacks, the runtime constant pool as well as the Java heap.

什么是Java堆转储?

我们已经了解到Java堆是所有的Java类实例以及数组分配的地方,它是一个运行时数据存放区域,在JVM执行期间,Java堆在所有的JVM线程之间共享。Java堆转储是Java堆在一个特定时刻的快照,就像是在某一时刻对一个繁忙的仓库拍个快照。如果我们查看这张照片,我们可以看出哪些商品在那一时刻可用。一些商品可能几分钟后就会被装运到加拿大,但是你可以在这张照片中看到他们,因为在拍照的那一刻,它们就在那儿。

由于Java规范中并没有提到Java堆转储,有来自不同的Java虚拟机不同形式的Java堆转储的实现。 IBMJava堆转储提供的主要是关于Java堆的信息。

Sun MicrosystemsHPROF Java堆转储提供了Java虚拟机栈信息,运行时常量池以及Java堆信息。

How Can I Generate Java Heap Dumps?
A Java heap dump is usually automatically generated by the Java Virtual Machine, but you can also force Java heap dump generation. On most IBM Java Virtual Machines, Java heap dumps are generated automatically when the Java heap becomes exhausted. On most Sun Microsystems JVMs, you need to configure the virtual machine to generate Java heap dumps. If you want to generate a Java heap dump when a java.lang.OutOfMemoryError occurs, you need to set the -XX:+HeapDumpOnOutOfMemoryError command-line option on certain releases of Sun's JVM. You could also use a HPROF profiler by using the -agentlib:hprof=heap=dump command-line option. You could also use jmap if your Sun JVM provides the utility. For example, jmap -dump 1234 will generate the Java heap dump from the process whose identifier is 1234. You could utilize JConsole by calling the HotSpotDiagnostic MBean and the dumpHeap operation if it's available from your Sun JVM.

If you want to generate Java heap dumps for Java virtual machine crashes (an unexpected termination of process) or user signals on IBM JVMs, you can set the environment variableIBM_HEAPDUMP or IBM_HEAP_DUMP to TRUE. For example, you can send the IBM Java virtual machine the signal SIGQUIT for the Linux operating systems and AIX operating systems or SIGINT(Control-Break key combination) for Windows to generate Java heap dumps. The IBM JVM provides an API, com.ibm.jvm.Dump.HeapDump(), that you can invoke from application code to generate Java heap dumps programmatically.

Please refer to the documentation of your JVM for detailed information since these options vary by platform and implementation.

如何生成Java Heap Dumps?

Java堆转储通常由由Java虚拟机自动生成,但是,你也可以强制生成Java堆转储。对于大多数IBM Java虚拟机,当Java堆被耗尽时,会自动生成Java堆转储。对于大多数Sun公司的JVM,您需要手工配置虚拟机参数来生成Java堆转储。如果您想在报java.lang.OutOfMemoryError时生成Java堆转储,您需要在Sun的JVM的某些特定版本上设置-XX:+HeapDumpOnOutOfMemoryError命令行选项。你也可以使用一个HPROF分析器,通过设置它的-agentlib:hprof=heap=dump命令行选项来生成。当然你也可以使用Jmap如果你的Sun的JVM提供了这个实用工具。例如,你可以通过jmap –dump 1234在为进程Id为1234的Java虚拟机进程来生成Java堆转储,其标识符为1234。如果你使用的Sun公司的Java虚拟机有提供JConsole工具,也可以通过调用它的HotSpotDiagnostic MBean和dumpHeap命令来生成。

如果你想在Java虚拟机崩溃(意外终止进程)时生成Java堆转储,或者user signals on IBM JVMs(此句不知如何翻译为好,求各位大神详解),你可以将IBM_HEAPDUMP或IBM_HEAP_DUMP环境变量设置为true。 例如,你可以在Linux或AIX系统上面通过发送SIGQUIT信号给Java虚拟机来产生Java堆转储,在Windows系统上面可以通过发送SIGINT信号给Java虚拟机来产生Java堆转储(如果是在cmd命令行启动,可以通过Ctrl+C组合键)。当然你也可以通过调用IBM提供的com.ibm.jvm.Dump.HeapDump()API方法以编程方式在生产Java堆转储。

Where Can I Find Java Heap Dumps?
You can find Java heap dumps in the current working directory of the Java Virtual Machine process, unless you specify a different location. You can specify the location with the environment variable IBM_HEAPDUMPDIRor_CEE_DMPTARGon IBM JVMs. If there's not enough space available for Java heap dumps or the JVM cannot acquire write-permission in the location, Java heap dumps are generated to the operating system's temporary directory on the IBM JVM. Please refer to your operating system manual for the location of the system's temporary directory and its configuration.

哪里可以找到Java堆转储文件?

你可以在Java虚拟机进程的当前工作目录下面找到Java堆转储文件,除非你指定了一个不同的位置。如果你使用的是IBM的JVM,你也可以使用IBM_HEAPDUMPDIR或者_CEE_DMPTARG环境变量来指定Java堆转储生成的位置。如果没有足够的空间可用于Java堆转储或JVM无法获得的堆转储位置的写权限,Java堆转储将会生成在IBM JVM所在操作系统的临时目录下请参考你的操作系统手册去查看系统的临时目录位置以及它的配置。

What Do Java Heap Dumps Look Like and How Can I read Them?
Nowadays, the majority of Java heap dump formats are generated in binary. Thus you might want to use a tool unless your brain can interpret hexadecimal codes without any headaches.

The IBM HeapAnalyzer is one of the most popular Java heap analysis tools. It can analyze all Java heap dump formats provided by Sun, HP and most of the Java heap dump formats provided by IBM. It's powered by object query engines and patent-pending heuristic analysis engines. I've been developing the IBM HeapAnalyzer from scratch since 2003, spending my vacation, weekends and weeknights on it. The IBM HeapAnalyzer was so successful that IBM decided to make the IBM HeapAnalyzer an official IBM software product and bundle it with existing products. So I donated all the source code of IBM HeapAnalyzer to IBM to run it on an Eclipse-based interface. Now the IBM HeapAnalyzer has a daughter, MDD4J, which always reminds me of my late daughter lost while I was working on the MDD4J project. The IBM HeapAnalyzer has been the top technology at IBM alphaWorks for six consecutive years since its inception as of March 2009.

Whether your Java heap dump is in binary or text/ascii format, the heap dump contains information about all the live objects that are on the Java heap such as address, object size, and referenced addresses. Let's take a look at the text/ascii format Java heap dumps since binary heap dumps have similar information but are in hexadecimal format to save disk space.

那么Java堆转储到底是什么样子呢?我们又如何读懂Java堆转储信息呢? 

        如今,大多数Java堆转储格式都是以二进制格式生成。所以,你也许会想使用一个工具去分析这些堆转储信息,除非你的大脑可以理解的十六进制代码,确不会感觉到头疼。

IBMHeapAnalyzer软件是最流行的Java堆分析工具之一。它可以分析所有由SunHP提供的Java堆转储格式,以及大部分IBM提供的Java堆转储格式。HeapAnalyzer是由对象查询引擎和正在申请专利的启发式分析引擎所驱动来提供分析功能。自2003年的第一个原型版以来,我一直在从事IBM HeapAnalyzer的持续开发,花费了我大部分假期、周末甚至周末的晚上。由于IBM HeapAnalyzer如此的成功,所以,IBM公司决定让IBMHeapAnalyzer列为正式的IBM软件产品,并且绑定到现在有的产品中。所以,我贡献了所有的IBM HeapAnalyzer源代码,以便其可以运行在基于Eclipse的界面上运行。现在,IBMHeapAnalyzer有了一个子产品MDD4J,它总是让我回想起我当时正在研究MDD4J项目期间失去的女儿。自20093月成立以来IBMHeapAnalyzer已经连续六年被认为是IBM alphaWorks最顶尖的技术。

无论你的Java堆转储是二进制格式还是ASCII文本格式,堆转储里面都包含了所有活动对象的内存地址、对象大小、引用地址等信息。

 上一篇:java.lang.OutOfMemoryError (OOM)解密 & Java heap dumps 解析 (一)

原文链接地址:http://java.sys-con.com/node/1229281


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值