Tuesday 28 November 2017

Useful Java Debugging Tools

I shall introduce a few easy to use and useful tools to debug java applications.

jps - List the java processes (JVMs) running on your machine. Check the man/help page for this command for more options/flags
L-IDC1JKG3QD-M:balaji-katika.github.io bkatika$ jps
20259 JConsole
4723 PlatformMonitoringApp
40345 Jps

jinfo - Useful for extracting configuration Info about Java Process. This tool is bundled by default with JDK.
Useful to analyze the system properties, VM args supplied to the process


L-IDC1JKG3QD-M:balaji-katika.github.io bkatika$ jinfo -sysprops 4723 | grep file.encoding
file.encoding.pkg = sun.io
file.encoding = UTF-8
jstat - Outputs the JVM statistics. Useful to monitor various stats (like gc, class, compiler etc.,) about the JVM. The below command displays gc statistics for java pid 4723 at 5s interval and ends after 4 samples


L-IDC1JKG3QD-M:balaji-katika.github.io bkatika$ jstat -gc 4723 5s 4
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT   
19968.0 24576.0 13758.7  0.0   262144.0 244184.8  163840.0   36106.6   51968.0 51044.1 6656.0 6468.0     10    0.104   2      0.067    0.172
19968.0 24576.0 13758.7  0.0   262144.0 244184.8  163840.0   36106.6   51968.0 51044.1 6656.0 6468.0     10    0.104   2      0.067    0.172
jstack - Used to take the stack track (thread dump) of the JVM. Useful in detecting scenarios like deadlock, high cpu, performance bottleneck etc., Several online thread analyzer are available for free like https://spotify.github.io/threaddump-analyzer/ http://fastthread.io/ 


L-IDC1JKG3QD-M:apache-cassandra-2.1.2 bkatika$ jstack 4723 | more
2017-11-15 13:40:54
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.40-b25 mixed mode):

"RMI TCP Connection(idle)" #1091 daemon prio=5 os_prio=31 tid=0x00007fcc48932800 nid=0x9d8f waiting on condition [0x0000700009b8e000]
   java.lang.Thread.State: TIMED_WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x00000006c05cef28> (a java.util.concurrent.SynchronousQueue$TransferStack)
        at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
        at java.util.concurrent.SynchronousQueue$TransferStack.awaitFulfill(SynchronousQueue.java:460)
        at java.util.concurrent.SynchronousQueue$TransferStack.transfer(SynchronousQueue.java:362)
        at java.util.concurrent.SynchronousQueue.poll(SynchronousQueue.java:941)
        at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1066)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
jmap - Collect the heap dump from a running JVM. The below command collects the heap in hprof format. You can use tools like jhat for analyzing the dump file

jmap -dump:format=b,file=heap_dump_11405.hprof 11405

jvisualVM - Here is the all in one Java troubleshooting tool. Provides an overview of important metrics like CPU, Memory, threads etc., through one UI. One can monitor these metrics like using intuitive graphs. Lot of plugins like Visual GC are available for free download that would further enhance the functionality of this tool.

jConsole

Some good articles to understand Java Garbage Collection
https://javaperformance.wordpress.com/2017/07/05/garbage-collection-in-java/