Pages

January 8, 2014

Java memory analysis using inbuilt tools

jhat(Heap Analysis Tool) is a tool that comes along with the jdk and is present under jdkxxx/bin/ .

There are two way to get the heap dump

1. Give it while starting the java program as
java -Xrunhprof:format=b,file=file.hprof class-name
2. Get the heap dump using jmap as
jmap -dump:file=file-name process_id

Here I am using the first method. The java code ran is the same as described in this post about Thread-Creation-Time.


E:\code\workspaces\projects\prj\target\classes>java -Xrunhprof:format=b,file=sna
pshot1.hprof com.mtk.prj.ThreadCreationTimeIncreasesWithNumberOfThreads
1 numbered thread created in 5ms.
181 numbered thread created in 11ms.
3047 numbered thread created in 15ms.
8045 numbered thread created in 19ms.
8076 numbered thread created in 27ms.
8112 numbered thread created in 34ms.
8146 numbered thread created in 36ms.
8784 numbered thread created in 43ms.
10970 numbered thread created in 58ms.
12896 numbered thread created in 402ms.
Dumping Java heap ... allocation sites ... done.
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new nati
ve thread
        at java.lang.Thread.start0(Native Method)
        at java.lang.Thread.start(Unknown Source)
        at com.mtk.prj.ThreadCreationTimeIncreasesWithNumberOfThreads.main(Threa
dCreationTimeIncreasesWithNumberOfThreads.java:16)

E:\code\workspaces\projects\prj\target\classes>

The second part to open the head dump with jhat is common to both the methods of getting the heap dump.

E:\code\workspaces\projects\prj\target\classes>jhat snapshot1.hprof
Reading from snapshot1.hprof...
Dump file created Tue Dec 31 00:02:56 IST 2013
Snapshot read, resolving...
Resolving 94838 objects...
Chasing references, expect 18 dots..................
Eliminating duplicate references..................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.

In a browser go to localhost:7000 and you'll get the report as shown below. Checking the links reveals more info like the objects that were created.


No comments:

Post a Comment