首页 > 解决方案 > jstack - 不包含线程堆栈跟踪

问题描述

在使用 jstack 了解特定进程的线程时。对于众多线程,我找不到任何堆栈跟踪。

"Thread-4978" #5139 prio=5 os_prio=0 tid=0x000000001d451800 nid=0x8530 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Thread-4808" #4969 prio=5 os_prio=0 tid=0x000000001d44f000 nid=0x8eb0 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

除了可运行状态之外,我无法从中获取任何信息。请指导我调试问题。

标签: javajvmjstack

解决方案


Java thread has no Java stack trace, when

  • The thread is about to start, and is running pre-start initialization or JVM TI ThreadStart handler.
  • The thread is about to terminate, and is running post-exit cleanup or JVM TI ThreadEnd handler.
  • The thread is a JVM TI agent thread, which is running native code.

To find out what these threads are doing, it's not enough to get Java stack trace - you'd need to get native stack traces as well.

On JDK 8 run

jstack -m <pid>

On JDK 11 and later

jhsdb jstack --mixed --pid <pid>

Another option is to use a profiler which is aware of native stacks, e.g. async-profiler

When used in threaded mode (-t), async-profiler can show mixed stack traces of all threads in the JVM, even if they have no Java stack trace.

For example, when I debugged a similar issue, async-profiler showed me the following stack trace:

native-stack

In my case, this was a JDWP agent running post-thread-end hook. Note that the standard Java debug agent (libjdwp) is a particular case of a JVM TI agent, which has non-trivial ThreadEnd handler.

This was a bug JDK-8227269 that resulted in slow termination of threads. So, if you experience problems with debug agent turned on, the first recommendation is to run JVM without JDWP.


推荐阅读