Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How 'expensive' is it to execute jstack on a running JVM?

Tags:

java

jstack

I'm considering whipping up a script to

  1. run once a minute (or every five minutes)
  2. run jstack against a running JVM in production
  3. parse the jstack output and tally up things I'm interested in
  4. export the results for graphing 24/365 by a centralized Cacti installation on another server

But I've no clue as to how expensive or invasive jstack is on a running JVM. How expensive is it to execute jstack on a running JVM? Am I setting myself up for a world of hurt?

like image 407
Stu Thompson Avatar asked Sep 09 '10 14:09

Stu Thompson


People also ask

Is Jstack part of JDK?

Jstack is a thread stack analysis tool that comes with jdk. I can use this command to view or export thread stack information in a Java application.

What is Jstack in Java?

Description. The jstack command prints Java stack traces of Java threads for a specified Java process. For each Java frame, the full class name, method name, byte code index (BCI), and line number, when available, are printed.

How does jstack work?

The jstack command-line utility attaches to the specified process or core file and prints the stack traces of all threads that are attached to the virtual machine, including Java threads and VM internal threads, and optionally native stack frames. The utility also performs deadlock detection.

What is Jstack dump?

jstack is a command-line JDK utility we can use to capture a thread dump. It takes the pid of a process and displays the thread dump in the console. Alternatively, we can redirect its output to a file. Let's take a look at the basic command syntax for capturing a thread dump using jstack: jstack [-F] [-l] [-m] <pid>


2 Answers

Measure. One of the time variants (/usr/bin/time I believe) has a -p option which allows you to see the cpu resources used:

ravn:~ ravn$ /usr/bin/time -p echo Hello World
Hello World
real         0.32
user         0.00
sys          0.00
ravn:~ ravn$ 

This means that it took 0.32 seconds wall time, using 0.00 seconds of cpu time in user space and 0.00 seconds in kernel space.

Create a test scenario where you have a program running but not doing anything, and try comparing with the usage WITH and WITHOUT a jstack attaching e.g. every second. Then you have hard numbers and can experiment to see what would give a reasonable overhead.

My hunch would be that once every five minutes is neglectible.

like image 34
Thorbjørn Ravn Andersen Avatar answered Sep 28 '22 11:09

Thorbjørn Ravn Andersen


I know this answer is late to the party but the expensive part of jstack comes from attaching to the debugger interface not generally generating the stack traces with an important exception (and the heap size does not matter at all):

Arbitrary stack traces can be generated on a safe point only or while the thread is waiting (i.e. outside java scope). If the thread is waiting/outside java scope the stack requesting thread will carry the task by doing the stack walk on its own. However, you might not wish to "interrupt" a thread to walk its own stack, esp while it is holding a lock (or doing some busy wait). Since there is no way to control the safe points - that's a risk need to be considered.

Another option compared to jstack avoiding attaching to the debugging interface: Thread.getAllStackTraces() or using the ThreadMXBean, run it in the process, save to a file and use an external tool to poll that file.

Last note: I love jstack, it's immense on production system.

like image 135
bestsss Avatar answered Sep 28 '22 09:09

bestsss