tags:

views:

82

answers:

2

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?

A: 

Depending on the number of threads and the size of your heap, jstack could possibly be quite expensive. JStack is meant for troubleshooting and wasn't designed for stats gathering. It might be better to use some form on instrumentation or expose a JMX interface to get the information you want directly, rather than have to parse stack traces.

dogbane
1000 threads, <1GB heap. I'm considered jstack because I know it and can implement quickly. JMX, while a viable option, is going to take some edumacation.
Stu Thompson
+1  A: 

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.

Thorbjørn Ravn Andersen
OK: real 0.54, user 0.51, sys 0.07 on an mildly loaded system. Will try later today. Besides how long it takes, what impact does jstack have on the JVM? An all stop?
Stu Thompson
Remember to measure your application, not the jstack invocation.
Thorbjørn Ravn Andersen
It's a web app running in Tomcat. Hmmm...maybe I should run jstack in a loop, and compare the number of invocations per minute with the CPU usage of the Java Tomcat process. Thanks suggesting this and helping think it through.
Stu Thompson
Then benchmark the Tomcat process...
Thorbjørn Ravn Andersen