Java HeapDump

Java堆内存分析
本文介绍如何使用多种方法生成Java应用程序的堆内存转储文件,并利用这些文件定位内存泄漏问题。文章详细解释了如何通过命令行参数和运行时操作来启用堆内存转储,并推荐了一些用于分析堆内存转储文件的工具。

http://www.startux.de/index.php/java/45-java-heap-dump

Java Heap Dump PDF Print E-mail
  
Wednesday, 30 January 2008 14:32

In order to find memory leaks within a java application it is sometimes not enough looking at thread dumps. With tools like visualgc, a tool the see the different memory pools inside a running java app, you can tune the memory setup concering heap size, perm size, survivor ratio, etc., but you can't see which type of objects are causing the heap to fill up.

Enable Heap Dumps

There are several possibilities to enable heap dumps.

One way is to start to application with -XX:+HeapDumpOnCtrlBreak (Introduced since JDK 1.4.2 update 12 and 5.0 update 7), see for additional information.
Sending signal SIGQUIT (3) to the running process causes the jvm to write a heap dump into its working directory. Unfortunately the -XX:HeapDumpPath option, to set the path to write the heap dump file to, currently doesn't work. See Sun Bug report.

Another way to get a heap dump of any running java application is using jmap, which is now part of the jdk.

jmap -dump:format=b pid

writes heap.bin to current directory. Since JDK 1.6 jmap you can use -heap:format=b,file=<heap_dump.out>


Analyse Heap Dumps

The best tool start with is probably hat, which is part of the jdk since JDK1.6 and called jhat.

If you don't have java 6 yet, get hat from here. Good examples and Documentation are available as well.
Prior to JDK 1.6 call: (attune heap size if hat to the size of your heap dumps)

java -jar hat.jar -Xmx512M heap_dump.out

After hat finished analysing the heap dump, it starts a HTTP server accessible on port 7000.

 

 

Last Updated ( Friday, 11 February 2011 19:49 )
 
Comments (2)
arguments correction
2 Friday, 11 February 2011 18:56
John
You want:
jmap -dump:format=b

(instead of -heap)

[Re: Thx]
Updated!
SIGQUIT (3)
1 Wednesday, 25 March 2009 17:20
Shane
Note, be sure to send SIGQUIT, not SIGHUP.

------------------------------------------------------------------

http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html

 http://websphere.sys-con.com/node/1229281/mobile

http://www.mhaller.de/archives/85-Java-heap-dumps.html

For analysis of memory leaks in Java applications, the heap dump is an essential source of information which is quite helpful. The dump files can be analyzed with tools like Eclipse Memory Analyzer Tool.
To get a heap dump, there are multiple ways. The first, which should be enabled in all circumstances and especially on production systems, is by adding a VM option. By enabling -XX:+HeapDumpOnOutOfMemory, a heap dump is automatically written to disk when OutOfMemoryErrors occur.
The second way is to manually force a heap dump at runtime at any point of time, without the need of an actual OutOfMemoryError. For this to work, you need to enable Java Management Extensions. Enable JMX by adding the following system properties to your launch configuration of Tomcat:
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port="9004"
-Dcom.sun.management.jmxremote.authenticate="false"
-Dcom.sun.management.jmxremote.ssl="false

Now run the following code to get a dump of the heap at runtime:

  1. String url = "service:jmx:rmi:///jndi/rmi://localhost:9004/jmxrmi";   
  2. JMXServiceURL jmxURL = new JMXServiceURL(url);   
  3. JMXConnector connector = JMXConnectorFactory.connect(jmxURL);   
  4. MBeanServerConnection connection = connector.getMBeanServerConnection();   
  5. String hotSpotDiagName = "com.sun.management:type=HotSpotDiagnostic";   
  6. ObjectName name = new ObjectName(hotSpotDiagName);   
  7. String operationName = "dumpHeap";   
  8. File tempFile = File.createTempFile("heapdump"".hprof");   
  9. tempFile.delete();   
  10. String dumpFilename = tempFile.getAbsolutePath();   
  11. Object[] params = new Object[] { dumpFilename, Boolean.TRUE };   
  12. String[] signature = new String[] { String.class.getName(), boolean.class.getName() };   
  13. Object result = connection.invoke(name, operationName, params, signature);   
  14. System.out.println("Dumped to " + dumpFilename);  

http://www.mhaller.de/archives/85-Java-heap-dumps.html

 

转载于:https://www.cnblogs.com/diyunpeng/archive/2011/11/14/2249026.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值