How to find out performance using thread dump

 Analyzing a thread dump is a crucial method for diagnosing performance issues in a Java application, especially related to threading and concurrency problems such as deadlocks, excessive thread contention, and resource bottlenecks. Here's a step-by-step guide on how to find out performance issues using thread dumps:

What is a Thread Dump?

A thread dump is a snapshot of all the threads running in a Java Virtual Machine (JVM) at a particular point in time. It provides information about each thread's state, stack trace, and monitor locks.

When to Take a Thread Dump?

Ø  High CPU Usage: When the application is consuming an unusually high amount of CPU.

Ø  Unresponsiveness: When the application is slow or not responding.

Ø  Deadlocks: When you suspect deadlocks or resource contention issues.

How to Take a Thread Dump?

You can take a thread dump using various tools:

  1. JDK Tools:

ü  jstack: A command-line utility that comes with the JDK.

sh

jstack -l <pid>

ü  jcmd: Another command-line utility for various JVM diagnostics.

sh

jcmd <pid> Thread.print

 

  1. JVisualVM: A graphical tool that comes with the JDK.

ü  Start jvisualvm, connect to your JVM, and generate a thread dump from the "Threads" tab.

  1. Operating System Commands:

ü  On Unix/Linux:

sh

kill -3 <pid>

 

ü  On Windows: Use jvisualvm or jconsole.

Analyzing a Thread Dump

Thread States

Threads can be in different states:

Ø  RUNNABLE: The thread is executing.

Ø  BLOCKED: The thread is waiting for a monitor lock to enter a synchronized block/method.

Ø  WAITING: The thread is waiting indefinitely for another thread to perform a particular action.

Ø  TIMED_WAITING: The thread is waiting for another thread to perform a particular action for up to a specified waiting time.

Ø  TERMINATED: The thread has exited.

Key Points to Analyze

  1. Identify Blocked Threads:

ü  Look for threads in the BLOCKED state.

ü  Check the "waiting to lock" and "locked" information to find out what resource is causing the block.

  1. High CPU Usage:

ü  Look for threads in the RUNNABLE state and check their stack traces to see what code they are executing.

ü  Identify if they are stuck in loops or performing long-running operations.

  1. Deadlocks:

ü  Search for sections in the thread dump that explicitly mention deadlocks.

ü  Identify the threads and resources involved in the deadlock.

  1. Resource Contention:

ü  Check for a high number of threads in the WAITING or TIMED_WAITING state waiting on the same resource.

  1. Thread Pools:

ü  Analyze the usage of thread pools (e.g., ExecutorService) to see if all threads are busy or idle.

ü  Check for potential thread starvation or over-utilization.

Example Analysis

Here’s an example snippet from a thread dump with key points highlighted:

plaintext

"Thread-1" #23 prio=5 os_prio=0 tid=0x000000001d0d4800 nid=0x17fc waiting on condition [0x000000001db7e000]

   java.lang.Thread.State: TIMED_WAITING (sleeping)

        at java.lang.Thread.sleep(Native Method)

        at com.example.MyClass.myMethod(MyClass.java:123)

        - locked <0x00000000f47e60f0> (a java.lang.Object)

        at com.example.MyClass.run(MyClass.java:456)

        at java.lang.Thread.run(Thread.java:748)

 

"Thread-2" #24 prio=5 os_prio=0 tid=0x000000001d0d5000 nid=0x1800 waiting for monitor entry [0x000000001dc7f000]

   java.lang.Thread.State: BLOCKED (on object monitor)

        at com.example.MyClass.mySynchronizedMethod(MyClass.java:789)

        - waiting to lock <0x00000000f47e60f0> (a java.lang.Object)

        at com.example.MyClass.run(MyClass.java:456)

        at java.lang.Thread.run(Thread.java:748)

 

 

Steps for Analysis

  1. Thread States:

ü  Thread-1 is in a TIMED_WAITING state because it is sleeping.

ü  Thread-2 is in a BLOCKED state waiting for the monitor lock on <0x00000000f47e60f0>.

  1. Identify Bottlenecks:

ü  Thread-2 is blocked trying to enter mySynchronizedMethod which is synchronized on the same object <0x00000000f47e60f0> that Thread-1 has locked.

  1. Check Stack Trace:

ü  The stack trace shows where each thread is in the code.

ü  Thread-1 is holding the lock and sleeping, which means Thread-2 cannot proceed until Thread-1 finishes its sleep and releases the lock.

Tools for Analysis

Several tools can help visualize and analyze thread dumps:

Ø  Thread Dump Analyzer (TDA): A graphical tool for analyzing thread dumps.

Ø  FastThread.io: An online tool for analyzing thread dumps. Click http://fastthread.io

Ø  JVisualVM: Can visualize thread states and monitor CPU usage.

More Information’s bash:

  1. Run your program in linux in jboss
  2. Goto jboss or weblogic bin
  3. Search your program running or not using below command
  4. ps -ef | grep -applicationName
  5. Get PID from linux after search
  6. Check the cpu utilization using below any one command
  7. vmstat 1
  8. top -p 123456 H, where 123456 is pid of this component
  9. Now look into the command prompt and see the "id" column in command prompt if it near to 100 then cpu ideal time is good
  10. Now take the thread dump below command
  11. goto folder location like:  /opt/kcm folder
  12. using cd
  13. using like: /opt/kcm/softwares/jdk1.8.0_05/bin/jstack -l 20563 > log.txt
  14. where 20563 is pid in side jdk1.8.0_05 and log.txt is the thread dump file.
  15. Copy this file from root folder from /opt/kcm/log.txt
  16. open this file and see the log
  17. or
  18. open this url http://fastthread.io
  19. upload log.txt file
  20. see the result
  21. Done

Conclusion

Analyzing thread dumps is a powerful method for diagnosing performance issues in Java applications. By understanding thread states, identifying blocked threads, and analyzing stack traces, you can pinpoint issues such as deadlocks, thread contention, and CPU bottlenecks. Using tools like jstack, jcmd, and various thread dump analyzers can significantly aid in this process.



thread dump analysis
thread dump analysis










Post a Comment

0 Comments