Pages

Monday, May 13, 2013

Java Dumps


As a Production Support Guy ,I many times faced the out of Memory(OOM) Issues. Most of times I take a Thread dump, Heap Dump before doing any thing to the server. I analyze the dumps to find out the possible problems that caused the out of Memory.

So what are these Dumps?

Thread Dump, a Thread Dump is a log containing information about currently running threads inside the JVM

Heap Dump, a Heap Dump is a log which contains the memory structure of the JVM with all the classes and objects inside the JVM and their memory usage

Core Dump, A Core Dump is recorded state of the working memory of a program at the specific time. This is generated generally when a program has been terminated abnormally.

Generating these dumps is always necessary for analyzing the issues.

Heap Dump
As is said A JVM heap dump is basically a “snapshot” of the Java heap memory at a given time. It is quite different than a JVM thread dump which is a snapshot of the threads.

Generating a Heap Dump can be done by using the “jmap” command available in the java software.

jmap -dump:format=b,file=heap.hprof JAVA_PID
For Oracle Jrocket we can use
jrcmd <JAVA_PID> hprofdump filename=abc.hprof
So Consider generating a heap Dump Files for the Process ID 22979.
Dev:vx1379:djbs002-jas $ jmap -dump:file=my_stack.bin 22979
Dumping heap to /config/jboss/ews/1.0/domains/jas/my_stack.bin ...
Heap dump file created

It’s easy to get an OutOfMememory exception when opening the java heap. The dump file can be very memory consuming if you application was in the moment it was taken. If you experience the problem you should give to the JVM as much memory as you can:
Most of the times the heap dumps are generated with the .hprof extension.

What is HProf?
HProf is a tool built into JDK for profiling the CPU and heap usage within a JVM. A Java process crash may produce an hprof file containing a heap dump of the process at the time of the failure.
So Once the Heap Dump is generated we need to analyze the file. Java provides a tool called “jhat” which helps in heap analysis.

Now in order to work with jhat to analysis the heap file we use,
jhat -J-mx2000m my_stack.bin

When we run this command we see,
Dev:vx1232:jbs002-jas $ jhat -J-mx2000m my_stack.bin
Reading from my_stack.bin...
Dump file created Sun May 12 23:56:01 CDT 2013
Snapshot read, resolving...
Resolving 1548901 objects...
Chasing references, expect 309 dots .....................................................................................................................................................................................................................................................................................................................
Eliminating duplicate references .....................................................................................................................................................................................................................................................................................................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.

This says that the jhat Server is started. We can access the Server using
http://vx1232:7000/

Now we can point another port as
jhat -port 7010 -J-Xmx1G my_stack.bin

Now the Jhat web console is open, we can see “Object Query Language” Link at the End.
A SQL-like query language to query Java heap. OQL allows to filter/select information wanted from Java heap. OQL is based on JavaScript expression language.

A few queries would be
select i from [Ljava.lang.Object; i where i.length == 10
select all Strings of length 100 or more
show path value of all File objects
select file.path.toString() from java.io.File file
show names of all ClassLoader classes
access static field 'props' of class java.lang.System
select heap.findClass("java.lang.System").statics.props
select heap.findClass("java.lang.System").props
select heap.findClass("java.lang.String").fields.length
find the object whose object id is given
select all classes that have name pattern java.net.*

Happy Learning J