Thursday 16 August 2018

HPROF tool: Heap and CPU profiling

HPROF is a tool to perform heap and cpu profiling. By using this tool, you can write the profiling information either to a file or to a socket in ASCII or binary format.

What can you do using HPROF tool?
a.   Analyze CPU usage
b.   Check heap allocation statistics
c.   Monitor contention profiles
d.   Gives information about state of all threads and monitors in the JVM
e.   Since HPROF tool, reports complete heap dump information, you can get more information while analyzing heap performance, lock contention, memory leaks etc.,

Get the list of options available with HPROF tool
Open command prompt and execute 'java -agentlib:hprof=help' command, it prints all the options available with HPROF tool.

C:\>java -agentlib:hprof=help

     HPROF: Heap and CPU Profiling Agent (JVMTI Demonstration Code)

hprof usage: java -agentlib:hprof=[help]|[<option>=<value>, ...]

Option Name and Value  Description                    Default
---------------------  -----------                    -------
heap=dump|sites|all    heap profiling                 all
cpu=samples|times|old  CPU usage                      off
monitor=y|n            monitor contention             n
format=a|b             text(txt) or binary output     a
file=<file>            write data to file             java.hprof[{.txt}]
net=<host>:<port>      send data over a socket        off
depth=<size>           stack trace depth              4
interval=<ms>          sample interval in ms          10
cutoff=<value>         output cutoff point            0.0001
lineno=y|n             line number in traces?         y
thread=y|n             thread in traces?              n
doe=y|n                dump on exit?                  y
msa=y|n                Solaris micro state accounting n
force=y|n              force output to <file>         y
verbose=y|n            print messages about dumps     y

Obsolete Options
----------------
gc_okay=y|n

Examples
--------
  - Get sample cpu information every 20 millisec, with a stack depth of 3:
      java -agentlib:hprof=cpu=samples,interval=20,depth=3 classname
  - Get heap usage information based on the allocation sites:
      java -agentlib:hprof=heap=sites classname

Notes
-----
  - The option format=b cannot be used with monitor=y.
  - The option format=b cannot be used with cpu=old|times.
  - Use of the -Xrunhprof interface can still be used, e.g.
       java -Xrunhprof:[help]|[<option>=<value>, ...]
    will behave exactly the same as:
       java -agentlib:hprof=[help]|[<option>=<value>, ...]

Warnings
--------
  - This is demonstration code for the JVMTI interface and use of BCI,
    it is not an official product or formal part of the JDK.
  - The -Xrunhprof interface will be removed in a future release.
  - The option format=b is considered experimental, this format may change
    in a future release.

How to invoke HPROF tool?
Execute below command to invoke hprof tool on given class.
java -agentlib:hprof ClassName

HelloWorld.java
public class HelloWorld {
 public static void main(String args[]) throws Exception {
  int count = 0;
  
  Thread t1 = new Thread() {
   public void run() {
    for(int i=0; i<5; i++){
     try{
      Thread.sleep(2000);
     }catch(Exception e){
      
     }
    }
   }
  };

  t1.start();
 }
}


Compile HelloWorld.java.

Execute HelloWorld application.
java -classpath . -agentlib:hprof HelloWorld
C:\Users\krishna\Documents\Study\Miscellaneous>java -classpath . -agentlib:hprof HelloWorld
Dumping Java heap ... allocation sites ... done.

After execution of above command, you can see below two files.
java.hprof.txt
java.hprof.txt.TMP

Note: By default, heap profiling information (sites and dump) is written out to java.hprof.txt (in ASCII) in the current working directory.

Get the heap allocation profile
Execute below statement to get heap allocation profile information.

java -agentlib:hprof=heap=sites ToBeProfiledClass
C:\Users\krishna\Documents\Study\Miscellaneous>java -classpath . -agentlib:hprof=heap=sites HelloWorld
Dumping allocation sites ... done.

Get sample cpu information every 20 millisec, with a stack depth of 3
java -agentlib:hprof=cpu=samples,interval=20,depth=3 classname
C:\Users\krishna\Documents\Study\Miscellaneous>java -classpath . -agentlib:hprof=cpu=samples,interval=20,depth=3 HelloWorld
Dumping CPU usage by sampling running threads ... done.



Previous                                                 Next                                                 Home

No comments:

Post a Comment