267

I have a production server running with the following flag: -XX:+HeapDumpOnOutOfMemoryError

Last night it generated a java-38942.hprof file when our server encountered a heap error. It turns out that the developers of the system knew of the flag but no way to get any useful information from it.

Any ideas?

trincot
  • 317,000
  • 35
  • 244
  • 286
Nick Stinemates
  • 41,511
  • 21
  • 59
  • 60

7 Answers7

248

If you want a fairly advanced tool to do some serious poking around, look at the Memory Analyzer project at Eclipse, contributed to them by SAP.

Some of what you can do is mind-blowingly good for finding memory leaks etc -- including running a form of limited SQL (OQL) against the in-memory objects, i.e.

SELECT toString(firstName) FROM com.yourcompany.somepackage.User

Totally brilliant.

Cowan
  • 37,227
  • 11
  • 66
  • 65
  • 20
    I'd just like to add a +100 for Eclipse Memory Analyzer. I'm currently attempting to sift thru a 400mb+ heap dump file, and it took jhat more than 70 minutes to read the file, before it caused a complete JVM crash. EMA is able to open it up in < 5 minutes. – matt b Jun 18 '09 at 18:37
  • 4
    I keep getting parsing errors when opening HPROF files using the Eclipse Memory Analyzer (that were in fact also dumped by Eclipse!). Unfortunate.. sigh. – lost_bits1110 Dec 14 '11 at 22:16
  • 4
    MAT can still require quite a bit of RAM [less than JHAT but still quite a bit]. See http://stackoverflow.com/questions/7254017/tool-for-analyzing-large-java-heap-dumps for tips if you run into that happening. – rogerdpack Mar 30 '15 at 23:43
  • 1
    `Error opening heap dump 'strictmode-death-penalty.hprof'. Check the error log for further details. Error opening heap dump 'strictmode-death-penalty.hprof'. Check the error log for further details. Unknown HPROF Version (JAVA PROFILE 1.0.3) (java.io.IOException) Unknown HPROF Version (JAVA PROFILE 1.0.3)` – Dr.jacky Dec 24 '20 at 21:30
87

You can use JHAT, The Java Heap Analysis Tool provided by default with the JDK. It's command line but starts a web server/browser you use to examine the memory. Not the most user friendly, but at least it's already installed most places you'll go. A very useful view is the "heap histogram" link at the very bottom.

ex: jhat -port 7401 -J-Xmx4G dump.hprof

jhat can execute OQL "these days" as well (bottom link "execute OQL")

Community
  • 1
  • 1
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 5
    After running above command In the console you will get the message int the terminal "server is ready" Port : 7401. After that open this URL : http://localhost:7401 "you can see details in the browser windows ". – Gautam May 30 '18 at 09:42
  • 1
    `java.io.IOException: Version string not recognized at byte 17` – Dr.jacky Dec 24 '20 at 21:28
  • 1
    This is not delivered by default in the latest versions of OpenJDK or Oracle JDK. – em_bo Jan 13 '22 at 14:55
39

You can also use HeapWalker from the Netbeans Profiler or the Visual VM stand-alone tool. Visual VM is a good alternative to JHAT as it is stand alone, but is much easier to use than JHAT.

You need Java 6+ to fully use Visual VM.

moxi
  • 1,390
  • 20
  • 21
James Schek
  • 17,844
  • 7
  • 51
  • 64
15

Just get the Eclipse Memory Analyzer. There's nothing better out there and it's free.

JHAT is only usable for "toy applications"

kohlerm
  • 2,596
  • 1
  • 17
  • 22
  • 3
    JHAT is necessary for impressing those "l33t" hackers who hand-build a BSD distro starting with the LILO. Wait... they'd never use java anyway. :-) – James Schek Oct 09 '08 at 16:43
  • I think this is more of a comment... :\ – rogerdpack Nov 28 '17 at 17:36
  • Yeah, JHAT is for "toy applications". I had a dump file (9GB), JHAT runs about 30 mins with 20GB heap and fails with OOM finally. The Eclipse Memory Analyzer handles it with 15GB heap within 1 min. – zwy Jul 06 '21 at 20:46
  • 1
    when I open a 3 GB dump file, The Eclipse Memory Analyzer, show out of memory error and unable to open the dump file. Windows 10/JRE17 - An internal error occurred during: "Parsing heap dump from 'D:\java_pid12269.hprof'". java.lang.OutOfMemoryError – mkag Oct 04 '21 at 10:54
15

I personally prefer VisualVM. One of the features I like in VisualVM is heap dump comparison. When you are doing a heap dump analysis there are various ways to go about figuring out what caused the crash. One of the ways I have found useful is doing a comparison of healthy vs unhealthy heap dumps.

Following are the steps you can follow for it :

  1. Getting a heap dump of OutOfMemoryError let's call it "oome.hprof". You can get this via JVM parameter HeapDumpOnOutOfMemoryError.
  2. Restart the application let it run for a bit (minutes/hours) depending on your application. Get another heap dump while the application is still running. Let's call it "healthy.hprof".
  3. You can open both these dumps in VisualVM and do a heap dump comparison. You can do it on class or package level. This can often point you into the direction of the issue.

link : https://visualvm.github.io

Waleed
  • 514
  • 1
  • 6
  • 17
11

YourKit Java Profiler seems to handle them too.

Polaris
  • 119
  • 1
  • 2
9

If you want to do a custom analysis of your heapdump then there's:

This library is fast but you will need to write your analysis code in Java.

From the docs:

  • Does not create any temporary files on disk to process heap dump
  • Can work directly GZ compressed heap dumps
  • HeapPath notation
Alexander
  • 38
  • 6
Andrejs
  • 26,885
  • 12
  • 107
  • 96