Sunday, December 23, 2007

jhat - Java Heap Analysizer

Since I get involved in some jhat related project with one of my senior team members. I decided to make some blog entries. jhat is java heap analysis tool which is shipped in jdk6. There is an independent project name hat on hat.dev.java.net. jhat is flavor of this project only.

If you are a guy, who write lot of Java code and keep on looking for optimization, if you are getting errors like memory overflow.. then yes its a tools for you only.

jhat is all about giving readable information from java heap dump. Java Heap Dump contains information like how many classes, how many instances, execution flow and many more.
But all these information are in a binary format. And the duty of jhat is to read that file and provide useful information to you.

There are lot of ways to generate heap dump:

1. From normal java execution program, you can see the heap by Ctrl + Break.
2. jdk6,5 is coming with some tools like jmap, jconsole which help us to generate heap dump. Usages of these tools are very easy.
3. jdk5 also comes up with a tool called HPROF which can be used to generate heap dump.

So, here are the steps, how to approach the problem:

Make heap dump with jmap:

1. Run the java program, say SwingSet2 in the example.
2. Open other terminal, and run jmap
jmap -dump:format=b,file=heap.bin (On Windows, you can get pid by Ctrl-Alt-Del -> Process -> PID )

jmap will generate a file with name heap.bin which contains heap dump. Now pass this heap.bin to jhat.

3. jhat heap.bin

There are lot many optional argument, check the detail here.

Open browser, type: http://localhost:7000 (7000 is the default port), you can see hell lot of information. Grab the information of your interest. Or if you want to parse the information from yourself use OQL(Object Query Language) and write query.

No comments: