Java

How to Stack Trace a Running Process in Java

A stack trace is nothing more than the location of exceptions in Java programs or you can say that it is used to find those lines of a code where exceptions occur. A stack trace works in the same way as a stack of papers on your desk does first-in, last-out. You add papers to the pile and then take them out in the reverse order. It includes details about a process or operation that your code invoked. As a result, the Java stack trace is a sequence of frames that begins with the current method and ends with the program’s inception.

The Stack, also known as the runtime or call stack, is a collection of stack frames created by a program while it runs and stored in a stack data structure. Java virtual machine (JVM) will automatically generate a stack trace when an exception occurs. So, this article will provide you the guideline as to how you can use the stack trace of the running process in java.

What is stack tracing in Java

Now before creating a Java file, you need to make sure that you have installed the java development kit (JDK) application in your Linux operating system, otherwise, you won’t be able to execute any Java program by typing:

$ sudo apt install default-jdk

After installing this prerequisite application, you need to create a Java file and one of the easiest ways of doing this is by using any text editor by typing:

$ nano JavaStackTrace.java

After creating a Java file, the next step is to write and save a code in it which can be seen below:

After saving a java file, the next step is to compile and run it:

$ javac JavaStackTrace.java
$ java JavaStackTrace

You can see that we have written demofun3() as the last function while writing a code but it has been executed in the second line as this is the point of trace generation that will be responsible to execute the dumpstack() which will display the output on the screen. The remaining functions starting from demofun2() to demofun() will execute afterward in the reverse order and the main() function is displayed at the bottom as it is responsible for starting the program and will also call other functions as well.

You can say that the stack execution point starts from the first line and the stack frame starts from the second line to the last line that makes up the entire stack trace. We can observe that the method that is performed first becomes the stack trace’s last stack frame, whereas the method that is executed last becomes the stack trace’s first stack frame and as a result, each stack trace piece represents a stack frame.

How to stack trace of a running process

Jstack is a great tool to resolve run time issues especially if your application is acting up, consuming more CPU or memory than expected then you can troubleshoot the application. Let’s implement it in Java:

public class ForceDump {
public static void main(String[] args) throws Exception {
  try {
      while(true) {
          Thread.sleep(2000);
      }
  } catch (NullPointerException Demo)  {
      Demo.printStackTrace();
  }
}
}

After that, you can create a java file using any text editor like we did by using a nano text editor as shown below

$nano ForceDump.java

After that compile it using:

$ javac ForceDump.java

And execute:

$ java ForceDump

After running this program a background process will start; to find the process id (PID), open another terminal window, and use:

$ ps aux | grep ForceDump

Then you can find additional information regarding threads that are running in a process by using the command below:

$ jstack <PID>

The process id is 4457 as shown in the image:

$ jstack 4457

Conclusion

A stack trace is a collection of stack frames that illustrates a specific moment in time during a program’s execution. It includes details about a method or function that your code invoked. Therefore, the Java stack trace is a series of frames that starts with the current method and ends with the beginning of the program. In this article, we have shown you how you can implement the stack tracing process in the Java programming language.

About the author

Taimoor Mohsin