Monday, November 22, 2010

Using HPROF and HAT to profile

HPROF is actually a JVM native agent library which is dynamically loaded through a command line option, at JVM startup, and becomes part of the JVM process.

Live objects allocation in the heap generated by Javac
javac -J-agentlib:hprof=heap=dump Hello.java
Each frame in the stack trace contains class name, method name, source file name, and the line number. The user can set the maximum number of frames collected by the HPROF agent (depth option). The default depth is 4. Stack traces reveal not only which methods performed heap allocation, but also which methods were ultimately responsible for making calls that resulted in memory allocation.

CPU usage Sampling Profiles:
javac -J-agentlib:hprof=cpu=samples Hello.java
The cpu=samples option doesn't use BCI, HPROF just spawns a separate thread that sleeps for a fixed number of micro seconds, and wakes up and samples all the running thread stacks using JVM TI.

CPU usage Times Profiles:
javac -J-agentlib:hprof=cpu=times Hello.java
The cpu=times option attempts to track the running stack of all threads, and keep accurate CPU time usage on all methods. This option probably places the greatest strain on the VM, where every method entry and method exit is tracked. Applications that make many method calls will be impacted more than others.


Using HAT
Generate a binary hprof file using -Xrunhprof flag
java -Xrunhprof:file=dump.hprof,format=b Main

To run HAT:
jhat -port 7002 dump.hprof
(by default the port is 7000)

Have happy profiling your applications :)

No comments: