Java Performance profiling Tools
This article will explain some programming tips that will help Java programming. We are going to explore the rationale why some Java application is running slower than expected, tips to identify the deadlock, tools to monitor Java thread/methods call and improve the performance.
To identify why a Java application is running slow, we need to determine what cause it.
The 4 key system resource are –
We need to identify which system resource is the key bottleneck. The tuning process can take several iterations. It’s common that originally memory issue problems solved, and become a CPU or disk issue.
|Resource Bottleneck||Potential Actions|
How much time it takes?
Here is sample code that we can do in the Java to record how much time it takes for certain function or method call.
To measure or benchmark performance, here are some recommended tools we may use.
|CPUDiskProcess/thread||Linux: top, ps, iostat, vmstatWindows: Task Manager, Typeperf|
Java Monitoring Tools
These tools are built-in with JDK and can be executed under console mode.
Jconsole and jvisualvm are recommended since these two tools will provide you a comprehensive view of performance metrics in graphical GUI.
|jcmd||Prints basic class, thread, and VM information for a Java process.|
|jconsole**||It provides a GUI interview JVM activities, including thread usage, class usage, and
GC activities. It can also be used to detect deadlock.
|jhat||Reads and helps analyze memory heap dumps. This is a postprocessing utility.|
|jmap||heap dumps and other information about JVM memory usage|
|jinfo||visibility into the system properties of the JVM|
|jstack||stacks of a Java process|
|jstat||GC (Garbage Collection)|
|jvisualvm**||A GUI tool to monitor a JVM, profile a running application|
Java Profiling by — Java Mission Control
Java Mission Control shows profiling information from recording over period of time.
How to launch it? Just type “jmc” under command console
Profiling for Specific Class
|java -Xrunhprof:cpu=samples,thread=y <classname>|
For example, the class file name is “TestingClass.class”. You may input the following command in the console.
|java -Xrunhprof:cpu=samples,thread=y TestingClass|
Once the class execution is done, it will generate the profiling log file. “java.hprof.txt”
Here is the sample out of the java.hprof.txt file. It tells the “method” call per usage.
Be aware that this option “-Xrunhprof” is only used for testing and profiling not for production. Uses of the option will make the application slower from 10~1000%!
Compressed JAR File
The Java class file can be compressed into JAR file. The example below will generate the ClassName.Jar based on ClassName.class.
|jar cef ClassName ClassName.jar ClassName.class|
Then, the execution will be –
|java -jar ClassName.jar|
Under the command console, type. “Jconsole”.
It will launch the Java Monitoring console as below, and ask which Java application process to monitor. Under the Threads tab, there will be “detect DeadLock” button. It’s very useful to detect which thread was blocked and the root/cause.