Monday, August 24, 2009

JVM memory profiling (jmap & jhat)

The newer versions of Sun JDK (1.5, 1.6) has better memory profiling tools available. One such combination is jmap and jhat.
Both of the tools are now part of JDK1.6 distribution (located under javahome/bin folder)
To get to a probable source of memory leak (or to figure out which objects are consuming most part of the heap) we have these JDK builtin tools for rescue.
Essentially, we can get the heap snapshot with jmap. we can get a heap dump from jmap which can be read by jhat.
Here is how it looks:  (I am trying to profile my weblogic11g process and its processID is 23707)

To get current heap usage stats :
===================================

# ./jmap -heap 23707   (23707   being java process ID )


To get list of all objects on the heap (both live and not reachable)
This is my WebLogic11g process.

# ./jmap -histo 23707   (23707   being java process ID )


To get a heap dump :
(I have specified file name as myheap.bin)
# ./jmap -dump:format=b,file=myheap.bin 23707
Dumping heap to /weblogic11g/jdk160_11/bin/myheap.bin ...
Heap dump file created

To read the heap dump thats generated, jhat can be used as follows :
# ./jhat myheap.bin
Reading from myheap.bin...
Dump file created Mon Aug 24 16:20:26 EDT 2009
Snapshot read, resolving...
Resolving 546388 objects...
.......................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.

jhat would start a webserver and all object allocations can be viewed and queried. The object allocation can be accessed through browser as http://myIP:7000

Here is a snapshot with default access. we find basic allocation information and also different options to query the objects.
We can get object allocation by size as :


If you prefer a GUI based view, you can check MAT(from eclipse) or visualGC.

3 comments:

Eyal K. said...

Hi,

How do you identify the Java pid? is it specific to a web application deployed on your weblogic?

Very useful information - Thanks.

Eyal

chinmay said...

ps -ef | grep java

Anonymous said...

Hi,

Do you have any heap dump analyzer. Can you please provide the link


-Suresh