Memory Analysis Part 1 – Obtaining a Java Heapdump

转自: https://blog.codecentric.de/en/2008/07/memory-analysis-part-1-obtaining-a-java-heapdump/

For troubleshooting Java memory leaks and high memory usage problems, the heapdump is one of the most important analysis features. The advantage of heapdumps is that they can be produced in productive environments – the place where the problems most frequently occur. All current Java Virtual Machines support the generation of heapdumps without the need of additional tools.

In this blog series I will show you how to analyze and fix memory problems in production . I will also provide a list of common antipatterns and memory problems.

The first part of this series deals with the vital task of heapdump generation – the main precondition for a successful analysis. The various JVM manufacturers (SunIBMBEA) have different tools and formats to dump the heap of the JVM – this blog therefore focus on the implementation of Sun. The Sun Java Virtual Machine contains several options and tools to create a heapdump:

– Automatically when a java.lang.OutOfMemoryError occurs

– With the command line tool jmap

– By using a provided MBean (Java Management Extension – JMX) and the tooljconsole

Of course, there is the possibility to use the Java Virtual Machine Tool Interface (JVMTI) to produce a dump – but therefore you would have to implement an agent in C. Many Profiling Tools (like JProfiler) provide a JVMTI agent to create and evaluate a heapdump with a GUI.

To automatically generate a heap dump when an OutOfMemoryError is thrown you have to provide this JVM command line parameter:

– XX:+HeapDumpOnOutOfMemoryError

The parameter causes the JVM to dump a HPROF headump to the current directory if an OutOfMemoryError occurs. The name of the dump is by conventionjava_pid.hprof. To specify the directory and the name of the file by yourself, you can add the parameter -XX:HeapDumpPath=path_to_file to the JVM command line options.

The automatic production of dumps with these parameters is not always useful. In some situations you want to produce a heapdump at any given time during application execution. In this case Java version 1.4.2_09, 1.5.x and 1.6.x provide the tool jmap. A HPROF heapdump can be requested by executing the following command:

jmap -dump:file=path_to_file java_process_id.

The provided Java process id determines which local JVM should be dumped. The process Id can be determined with the JVM Tool jps (Note: The jmap tool is not available on every platform and JVM version). You can alternatively use the JVM parameter -XX:+HeapDumpOnCtrlBreak and send a SIGQUIT signal (-3 kill for Unix and Ctrl-Break for Windows) to the running Java process – the signal will also create a heapdump without aborting the JVM.

With Java 6, Sun introduced a JMX MBean which provides methods for generating a heapdump. To create a heapdump via JMX you first start the integrated JMX console with the command jconsole and connect it to the corresponding JVM. For a local connection you don’t need any additional configuration of the JVM – for a connection to a remote machine you have to configure JMX correctly.

You can use the MBean Explorer of jconsole to find the correct MBean within the JVM. The MBean com.sun.management.HotSpotDiagnostic contains the alluded method dumpHeap (String, boolean). With the help of the first method parameter you can set the path and the name of the Heapdump. The screenshot shows the view of the MBean. Pressing on the dumpHeap button will create a heapdump with the given name.

The next part of this series will get into more details how to analyse heapdumps and find memory leaks and common memory antipatterns.

Information about heapdump generation with the IBM JVM can be found in the IBM JVM Diagnosis Documentation.

Information about BEA JRockit and the JRockit Memory Leak Detector can be found in the JRockit Dokumentation.

时间: 2024-11-08 15:14:37

Memory Analysis Part 1 – Obtaining a Java Heapdump的相关文章

Eclipse Memory Analysis进行堆转储文件分析

生成堆转储文件 1.新建项目,设置Eclispe Java堆的大小: (1)限制Java堆大小:将最小值 -Xms参数与最大值-Xmx参数设置一样可避免堆的扩展 -Xmx20m -Xms20m -Xmn10m -Xss1m (2)通过参数:-XX:+HeapDumpOnOutOfMemoryError,可以让VM在出现内存溢出异常时Dump当前内存堆转储快照         -Xmx20m -Xms20m -Xmn10m -Xss1m -XX:+HeapDumpOnOutOfMemoryErro

About Memory Analysis

关于内存分析About Memory Analysis 每当应用程序创建对象时,都会为它们分配内存.传统上,它已被应用的工作跟踪这些对象并释放他们时,他们不再需要的内存可以分配其他对象.自动引用计数(ARC)是一种通过让系统负责内存管理而使事情变得更容易的特性.在启用ARC的情况下,系统处理监控对象分配,并在适当时释放它们,只剩下很少的应用程序要做.然而,不管内存是如何管理的,即使是最好的应用程序设计也会遇到难以分离的偶尔内存问题.Whenever your app creates object

Static and Dynamic Analysis of Call Chains in Java

Static and Dynamic Analysis of Call Chains in Java

[转][Windbg] Windows Memory Analysis Checklist

http://www.dumpanalysis.org/windows-memory-analysis-checklist General: Symbol servers (.symfix) Internal database(s) search Google or Microsoft search for suspected components as this could be a known issue. Sometimes a simple search immediately poin

Android应用优化之内存概念

导语 现在的Android智能手机发展信息万变,从一开始的HTC到小米价格战到现在高端市场份额战,在软硬件都发生了翻天覆地的变化.在硬件上内存从一开始的一两百M到现在4G.从软件上我们从一开始为了实现需求而写代码到现在为了代码更健壮.更漂亮而进行不断优化代码.这些都是Android发展的必然一步.今天我来跟大家一起分享Android内存优化的相关概念和实践. 概念 进程内存与RAM之间的关系 进程内存既是虚拟内存(或者叫逻辑内存),而程序的运行需要实实在在的内存,即物理内存(RAM),在需要的时

Andfroid 内存溢出与内存泄漏的简单分析与解决

<一>内存溢出与内存泄露 首先我们要知道内存溢出与内存泄露的概念,什么是内存溢出和内存泄露. 内存溢出:就想杯子里得水满了,就溢出了.内存溢出就是分配的内存被用光了,不够用了. 内存泄露:就如同杯子里面有石子,导致杯子里面的一部分空间没有被利用,在APP中内存泄露就是指该被回收的内存没有被回收,导致一部分内存一直被占着,可利用内存变少了.当泄露过多 时,可利用的内存越来越少,就会引起内存溢出了. <二> 查找内存泄露与内存溢出 (1) 内存溢出,最明显的地方就是报错,APP奔溃并报

On Memory Leaks in Java and in Android.

from:http://chaosinmotion.com/blog/?p=696 Just because it's a garbage collected language doesn't mean you can't leak memory or run out of it. Especially on Android where you get so little to begin with. Now of course sometimes the answer is that you

DTrace to Troubleshoot Java Native Memory Problems

How to Use DTrace to Troubleshoot Java Native Memory Problems on Oracle Solaris 11 Hands-On Labs of the System Admin and Developer Community of OTN by Wang Yu, Vincent Liu, and Gary Wang This lab will introduce the basic concepts of DTrace and provid

Java Objects Memory Structure (转)

原文地址http://www.codeinstructions.com/2008/12/java-objects-memory-structure.html Update (December 18th, 2008): I've posted here an experimental library that implements Sizeof for Java. One thing about Java that has always bothered me, given my C/C++ ro