How to capture and analyze Thread dumps in Android?

Techno Master
4 min readSep 11, 2023

--

Photo by Christian Wiediger on Unsplash

Discover the power of thread dumps in Android development. Learn what thread dumps are, how to capture them using commands like ‘dumpsys thread’ and ‘jstack,’ and uncover the insights they offer. Explore the effectiveness of ‘fastThread,’ a user-friendly tool that simplifies thread dump analysis, helping you identify performance issues, deadlocks, and more for a smoother app experience

What is a Thread Dump in Android?

A thread dump in Android is a snapshot of all the threads running in its process at a specific point in time. It can be used to troubleshoot performance issues, identify deadlocks, and debug other problems.

How to Get a Thread Dump in Android?

While there are a few ways to get a thread dump in Android, let’s take a look at a few in detail here:

Dumpsys Thread

The ADB shell is a powerful tool for gathering essential information about various system services. In this context, we are focusing on acquiring details about the ‘thread’ component. To accomplish this, we utilize the ‘dumpsys thread’ command, which systematically furnishes comprehensive information pertaining to threads within the system

We can redirect the output to a file as well by adding the name of the file.

adb shell dumpsys thread <name of the file>

JStack command

This is by far the easiest way to take the thread dump. Just get the process id and call the JStack command.

JStack <PID> > <file_name>

In order to get a process id, you can run the following command:

jps -mv | findstr studio

This command will return the following:

24084 exit -Xms256m -Xmx1280m -XX:ReservedCodeCacheSize=512m -XX:+IgnoreUnrecognizedVMOptions -XX:+UseG1GC -XX:SoftRefLRUPolicyMSPerMB=50 -XX:CICompilerCount=2 -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -ea -Dsun.io.useCanonCaches=false -Djdk.http.auth.tunneling.disabledSchemes=”” -Djdk.attach.allowAttachSelf=true -Djdk.module.illegalAccess.silent=true -Djna.nosys=true -Djna.boot.library.path= -Didea.vendor.name=Google -Dkotlinx.coroutines.debug=off -Djava.system.class.loader=com.intellij.util.lang.PathClassLoader -Didea.vendor.name=Google -Didea.paths.selector=AndroidStudio2021.3 -Didea.platform.prefix=AndroidStudio -Didea.jre.check=true -Dsplash=true -Dide.native.launcher=true -XX:ErrorFile=C:\Users\User\java_error_in_studio64_%p.log -XX:HeapDumpPath=C:\Users\User\java_error_in_studio64.hprof

The process ID is the first number ie 24084. Take this number to get the thread dump.

We can use JStack command.

JStack 24084 > mydump.txt

This command will generate thread dump into the mydump.txt file. It is that easy.

How to analyze Android Thread Dump?

Thread dump analysis can become more efficient and insightful with the help of specialized tools. One such tool that simplifies the process is ‘fastThread’, which is a web-based tool that provides a user-friendly interface for analyzing thread dumps. It offers various features that assist in identifying thread-related problems and performance bottlenecks. In this section, we’ll explore how to use ‘fastThread’ for thread dump analysis in Android.

Step 1: Getting Started with fastThread

  1. The first step is to access fastThread. Visit the ‘fastThread’ website (https://fastthread.io/) and sign up for a free account.
  2. Then, after logging in, you can start a new analysis by uploading a thread dump file.

Step 2: Analyzing Thread Dumps

  • The toolset provides a clear overview of the various thread states present in the thread dump, such as “RUNNABLE,” “WAITING,” “TIMED_WAITING,” and “BLOCKED.” This allows you to quickly identify threads that might be causing performance issues.
  • Then it presents a list of all threads along with their stack traces and states. Clicking on a thread provides a detailed view of its stack trace and the method calls involved.
  • fastThread helps you understand thread dependencies and interactions. It highlights threads that are waiting for locks held by other threads, aiding in the detection of potential deadlocks.

Step 3: Identifying Performance Bottlenecks

  • The report highlights instances where threads are blocked, along with information about potential deadlocks. This is crucial for identifying scenarios where threads are waiting for resources that are held by other threads, leading to performance bottlenecks.
  • ‘fastThread’ identifies methods that are consuming a significant amount of time, allowing you to focus on optimizing these performance-intensive areas of your code.
  • The tool provides details about exceptions that were thrown at the time the thread dump was captured. This aids in identifying error-prone areas of your codebase.
  • ‘fastThread’ points out threads that are consuming high amounts of CPU resources. Addressing these threads can lead to improved overall application performance.
  • The report also sheds light on threads waiting for responses from external systems. This information can help you manage interactions with external services more effectively.

Sample Android Thread Dump Analysis Report

We used the fastThread toolset to capture the thread dump and used the data for analysis. Here is the report that got generated.

Fig: Android Thread dump analysis report generated by online tool fastThread

This report provides a tangible example of what you can expect when utilizing ‘fastThread’ for your own thread dump analysis.

Conclusion

n the dynamic world of Android development, the ability to capture and analyze thread dumps is a superpower that empowers developers to ensure optimal performance and robustness in their applications. By harnessing tools like ‘fastThread,’ the complex landscape of threads and their interactions becomes more manageable. Swift generation of detailed reports, highlighting blocked threads, pinpointing performance bottlenecks, and uncovering exceptions all contribute to a refined development process.

Embracing thread dump analysis as a routine practice equips developers with the insights needed to address issues proactively and continuously enhance the user experience. Remember, a well-optimized application doesn’t just meet expectations — it exceeds them, delivering a seamless and exceptional user experience that keeps users engaged and satisfied.

Happy coding and optimizing!

--

--

Responses (1)