How can you take a threaddump?
Print threads?
What is a safepoint and why you should care?
Which tools can help and how?
And when you have the threaddump… what’s in there?
We’ll talk threads and locks. Dead and alive alike.
We’ll talk CLI and GUI (tools, of course).
We’ll talk about thread lifecycle.
And naming. Oh, the naming!
Example rich talk, though rather aimed at beginners.
-
instances of java.lang.Thread
-
have associated native thread
-
have names (or are named Thread#nextNumberInSequence)
-
have local variables, accessible only to given thread
Important
|
Thread lifecycle
Enum:
|
Native thread gets created in NEW and reclaimed upon thread termination.
Caution
|
RUNNABLE means OK, right? |
Are there any?
What limits the threads?
Let’s add more for just one JVM:
See limit
, Bash built-in. In my case:
➜ ThreaddumpsOnJVM git:(master) ✗ limit cputime unlimited filesize unlimited datasize unlimited stacksize 8MB coredumpsize 0kB memoryuse unlimited maxproc 63635 descriptors 1024 memorylocked 64kB addressspace unlimited maxfilelocks unlimited sigpending 63635 msgqueue 819200 nice 0 rt_priority 0 rt_time unlimited
Interesting limits are stacksize, descriptors and maxfilelocks when speaking about threads.
-
kill -3 JAVA_PID_HERE
- kill
-
send a signal to a process
kill -9 111 kill -s SIGKILL 111 kill -KILL 111
May be a shell built-in AND a command: which kill
Essentially a wrapper around kill syscall.
-
kill -L
- no such option on my Ubuntu/Bash-
kill -l
-
-
/bin/kill -L
-
/bin/kill -l
- same as built-in
-
Tip
|
Further reading about
kill Excellent pieces of information, especially the first one: |
/bin/kill -L
Default? TERM
man --section 7 signals | grep SIGQUIT
man --section 7 signals | grep Core
Caution
|
Java and signals
|
-
ps aux | grep java
-
pgrep java
-
jps -l
- jps
-
Lists the instrumented Java Virtual Machines (JVMs) on the target
system. This command is experimental and unsupported.
-
jcmd
- jcmd
-
sends diagnostic command requests to a running JVM.
Warning
|
jps output is empty?
|
-
kill -3 JAVA_PID
-
jstack JAVA_PID
- jstack
-
Prints Java thread stack traces for a Java process, core file,
or remote debug server. This command is experimental and unsupported.
Tip
|
Poor man’s debugger? jstack in a loop… However, can pinpoint live-locks!
|
-
kill -3 JAVA_PID
-
jstack JAVA_PID
-
Ctrl
+\
-
jconsole
-
jvisualvm
Caution
|
Dump location?
|
jcmd
And when you have the threaddump… what’s in there?
-
JVM and Java info
-
Thread dump info
-
Stack traces of threads
-
Heap breakdown
jconsole ctrl+break kill -3 PID kill -s 3 PID kill -QUIT PID type kill man kill man -k signal man --section 7 signals | grep SIGQUIT man --section 7 signals | grep Core man core vim /proc/sys/kernel/core_pattern ps | grep java pgrep java pgrep -u root java pgrep -u root,tammo java jps -q jps jps -l jps -m jcmd jps -v jcmd -l ls /tmp/hsperfdata_tammo/ while true; do; ls /tmp/hsperfdata_tammo/; done jcmd -options jcmd 12769 help jcmd 12769 help Thread.print jcmd 12769 Thread.print jcmd 12769 Thread.print > threads jcmd 12769 Thread.print -l > threadsAndLocks jcmd 14222 Thread.print -l > threadsAndLocks jstack 14222 > threaddump man jcmd man jinfo man jstack jvisualvm -J-Xverbose:gc for i in $(seq 1 5); do; jstack 16002 > td$i; echo $i; sleep 5; done; jconsole jvisualvm
Besides link already used and added earlier, few others caught my eye:
Caution
|
If you started process in the background and killed it’s terminal, process might get killed. If it ain’t, then to redirect it’s output is no easy task. Still, it’s feasible. |