Home  >  Article  >  Java  >  Java heap dump

Java heap dump

WBOY
WBOYOriginal
2024-08-30 15:14:19823browse

In Java, a heap dump gives a snapshot of the whole objects present in the memory of the Java Virtual Machine at a certain moment. Heap dumps are useful in troubleshooting memory leakage problems and optimization of memory usage in different java applications. Details of each instance like address, class name, type, references to another object are noted in the heap dump. Heap dump has two formats, such as Portable Heap Dump format, a binary and classic format in asci text. In the below sections, let us see different tools and techniques to generate a heap dump.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Heap Dump Formats

  • Portable Heap Dump (PHD) format: In this format, the dump file consists of a header section as well as a body section. The body section of the dump file contains details about objects, class records, or arrays. To explain the file format, primitive numbers are used.
  • Classic Heap Dump format: This dump formats are generated in ASCII text on almost all platforms except z / OS, which are encoded in EBCDIC.

Why Heap dump is used, and what is its use?

Normally, this heap dump is used in the Java application we are using is taking up memory more than we expected, or the application crashed with the OutOfMemoryError. If we analyze the heap dump, it will help to identify the root cause of the anomaly. In addition to this, heap dump can also be used to identify the details like the memory usage of each class, the count of objects in each class, etc. Moreover, fine details can be captured, and the amount of memory taken by an individual Java object present in the application can also be found out. This information can help us identify the actual code that causes memory leak problems.

Tools and Techniques

Here are the following tools and techniques mention below

1. JDK Tools

Heap Dumps can be captured using different tools under JDK, which are available in the bin folder within the home directory of JDK. For this, the command line can be used since the directory is present in the system path.

jmap

jmap is a tool that can be used in local as well as remote processes that print memory statistics in a running JVM.

To generate a heap dump with a jmap, the dump option can be used as shown below.

jmap -dump:[live] , format = b , file=< path of the file > <process id>
  • live: Optional parameter. Objects with active references only will be printed if this is set
  • format=b: Mentions that the dump file format will be in binary.
  • file: The file where the created dump can be written to
  • PID: Java process id
  • jcmd

jcmd is a tool that sends the command requests to the Java Virtual Machine. Moreover, this has to be present in the machine where the Java process is running.

For getting the heap dump, command GC.heap_dump can be used where the pid and file path of output only has to be mentioned, as shown below.

jcmd < pid > GC.heap_dump < path of the file >
JVisualVM

JVisualVM is a tool that has a simple and intuitive graphical user interface that permits us to monitoring, troubleshooting, and profiling Java applications. To generate a heap dump using this, the Java process has to be right-clicked, and the “Heap Dump” option has to be selected. Once this is done, the tool will create a heap dump, and it gets open in a new tab.

2. Automatic creation of heap dump

Instead of using manual techniques as mentioned above, a command-line option, HeapDumpOnOutOfMemoryError, is used to automatically create the heap dump when a java.lang.OutOfMemoryError is thrown:

java - XX:+HeapDumpOnOutOfMemoryError

java_pid.hprof is the file that stores dump by default, and it is available in the directory where the application is run.

Instead of the default path, other file paths can also be used, and for that, the HeapDumpPath option can be used, as shown below.

java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=< file_path >

So, when the application runs memory outage using this way, the logs can be seen in the created file that consists of the heap dump:

3. JMX

In this, HotSpotDiagnostic MBean is used that generates a dumpHeap technique that permits 2 parameters.

They are:

outputFile, which is the file path for the created dump. Make sure that the file should have an extension hprof.

live, which is similar to the one in jmap. Objects with active references only will be printed if this is set

Let us see two different techniques to invoke this method for capturing a heap dump.

JConsole

JMX client like JConsole is the simplest way for using the HotSpotDiagnostic MBean. If JConsole is opened and connected to a running Java process, navigating to the tab MBeans and identifying the HotSpotDiagnostic present under com.sun.management. Inside the operations, the dumpHeap method explained before can be found. For performing the dumpHeap operation, parameters outputFile and live as the p0 & p1 text fields.

Programmatic method

Similar to JConsole, HotSpotDiagnostic MBean can be used by invoking it through a program from Java code. For that, an MBeanServer instance has to get an MBean registered in the application. Once this is completed, an instance of a HotSpotDiagnosticMXBean is needed, and the dumpHeap method of that instance has to be called.

4. IBM Administrative Console

Suppose the application you are using runs on IBM Websphere Application Server; the administrative console can generate heaps. First, in the navigation pane of the administrative console, select Troubleshooting and choose Java dumps and cores. Once this is done, select the server_name for the one that has to create the heap dump. After that, Click Heap dump to create the heap dump for the mentioned server. wsadmin can also be used to create heap dumps.

The above is the detailed content of Java heap dump. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:JSF ConvertersNext article:JSF Converters