How to analyse the heap dump using jmap in java
Asked Answered
O

8

68

I am creating heap dump using below command:

jmap -dump:file=DumpFile.txt <process-id>

I have opened the generated file - DumpFile.txt but it is not in readable format. So please let me know how to analyze the data in the generated file.

Oliguria answered 28/2, 2013 at 8:38 Comment(4)
Have you tried jmap -heap <process-id> > DumpFile.txt?Wellordered
this file opens fine in Eclipse MAT, just give it .hprof extensionMyriammyriameter
See also #186393Rave
NetBeans can read these. Just go to File > Open File... select and open it.Ludovika
C
57

You should use jmap -heap:format=b <process-id> without any paths. So it creates a *.bin file which you can open with jvisualvm.exe (same path as jmap). It's a great tool to open such dump files.

Canny answered 12/6, 2013 at 13:23 Comment(2)
Starting with JDK 9, Visual VM will not be included with Oracle JDK. Developers who would like to use Visual VM with Oracle JDK 9 or later can get it from the Visual VM open source project site.Balladist
Note that you need to File > Load... the heap dump (it's not a core dump). See: https://mcmap.net/q/282198/-visualvm-not-a-valid-core-dumpDisepalous
N
42

You can use jhat (Java Heap Analysis Tool) to read the generated file:

jhat [ options ] <heap-dump-file>

The jhat command parses a java heap dump file and launches a webserver. jhat enables you to browse heap dumps using your favorite webbrowser.

Note that you should have a hprof binary format output to be able to parse it with jhat. You can use format=b option to generate the dump in this format.

-dump:format=b,file=<filename>
Nayarit answered 28/2, 2013 at 8:41 Comment(5)
I ran the jhat command to analyse the heap dump file but I ma getting below error: Reading from 447start.out... java.io.IOException: Unrecognized magic number: 1027423549 at com.sun.tools.hat.internal.parser.Reader.readFile(Reader.java:81) at com.sun.tools.hat.Main.main(Main.java:143) Here 447start.out is the name of the log file.Oliguria
Try to dump with format=b option like this: jmap -dump:format=b,file=<filename>Nayarit
jhat is rather not a user-friendly toolEllinger
I've generated a 16Gb heap dump on a server using the -XX:+HeapDumpOnOutOfMemoryError jvm option, and jhat worked smoothlessly with it. Thanks for th nice hint!Devitalize
Using jhat was a good option, as I am using OpenJDK on Linux, which includes jhat, making this an easy option using openjdk.Bullen
B
20

Very late to answer this, but worth to take a quick look at. Just 2 minutes needed to understand in detail.

First create this java program

import java.util.ArrayList;
import java.util.List;

public class GarbageCollectionAnalysisExample{
    public static void main(String[] args) {
           List<String> l = new ArrayList<String>();
           for (int i = 0; i < 100000000; i++) {
                  l = new ArrayList<String>(); //Memory leak
                  System.out.println(l);
           }
           System.out.println("Done");
    }
}

Use jps to find the vmid (virtual machine id i.e. JVM id)

Go to CMD and type below commands >

C:\>jps
18588 Jps
17252 GarbageCollectionAnalysisExample
16048
2084 Main

17252 is the vmid which we need.

Now we will learn how to use jmap and jhat

Use jmap - to generate heap dump

From java docs about jmap “jmap prints shared object memory maps or heap memory details of a given process or core file or a remote debug server”

Use following command to generate heap dump >

C:\>jmap -dump:file=E:\heapDump.jmap 17252
Dumping heap to E:\heapDump.jmap ...
Heap dump file created

Where 17252 is the vmid (picked from above).

Heap dump will be generated in E:\heapDump.jmap

Now use Jhat Jhat is used for analyzing the garbage collection dump in java -

C:\>jhat E:\heapDump.jmap
Reading from E:\heapDump.jmap...
Dump file created Mon Nov 07 23:59:19 IST 2016
Snapshot read, resolving...
Resolving 241865 objects...
Chasing references, expect 48 dots................................................
Eliminating duplicate references................................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.

By default, it will start http server on port 7000. Then we will go to http://localhost:7000/

Courtesy : JMAP, How to monitor and analyze the garbage collection in 10 ways

Bartholomeus answered 13/5, 2017 at 16:8 Comment(0)
K
10

If you use Eclipse as your IDE I would recommend the excellent eclipse plugin memory analyzer

Another option is to use JVisualVM, it can read (and create) heap dumps as well, and is shipped with every JDK. You can find it in the bin directory of your JDK.

Kill answered 28/2, 2013 at 8:42 Comment(1)
Thanks I download the software to analyze the issue.Oliguria
B
5

VisualVm does not come with Apple JDK. You can use VisualVM Mac Application bundle(dmg) as a separate application, to compensate for that.

Bandylegged answered 15/10, 2013 at 23:24 Comment(0)
R
0

As mentioned in most answers above you could take a Heap Dump easily with bellow command:

jmap -dump:live,format=b,file=/path/to/mydump.hprof <pid>

<pid> - The Java Process ID

It would be easy to understand what you just got using a graphical heap dump analyzer such as Eclipse MAT.

You need Eclipse IDE and Memory Analyzer plugin to get started.

More details can be found in this article.

Redwing answered 2/6, 2023 at 11:8 Comment(0)
L
-1

MAT, jprofiler,jhat are possible options. since jhat comes with jdk, you can easily launch it to do some basic analysis. check this out

Lightfingered answered 18/4, 2013 at 18:15 Comment(0)
A
-1

If you just run jmap -histo:live or jmap -histo, it outputs the contents on the console!

Amoreta answered 17/11, 2020 at 6:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.