【jstack】使用jstack查看应用堆栈
1、使用ps查询应用pid
ps -ef | grep java
输出信息
~$ ps -ax | grep mdc 2125 pts/5 S+ 0:00 grep --color=auto mdc 31795 ? Ssl 28:47 java -Xms2G -Xmx2G -XX:MetaspaceSize=256m
2、查看进程的线程使用情况
~$ top -H -p 31795 top - 10:01:56 up 7 days, 11:32, 6 users, load average: 4.42, 4.42, 4.35 Threads: 277 total, 0 running, 277 sleeping, 0 stopped, 0 zombie %Cpu(s): 6.6 us, 2.1 sy, 0.0 ni, 91.0 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st KiB Mem : 13150905+total, 1213448 free, 65027452 used, 65268160 buff/cache KiB Swap: 10485756 total, 10475504 free, 10252 used. 60599632 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 31834 root 20 0 23.953g 2.074g 24544 S 0.3 1.7 1:30.90 G1 Young RemSet 31927 root 20 0 23.953g 2.074g 24544 S 0.3 1.7 3:25.45 com.alibaba.nac 33014 root 20 0 23.953g 2.074g 24544 S 0.3 1.7 3:07.77 SimplePauseDete 33015 root 20 0 23.953g 2.074g 24544 S 0.3 1.7 3:06.63 SimplePauseDete 33016 root 20 0 23.953g 2.074g 24544 S 0.3 1.7 3:06.40 SimplePauseDete 31795 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.04 java 31823 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:07.56 java 31825 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.54 GC Thread#0 31826 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.00 G1 Main Marker 31827 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.00 G1 Conc#0 31833 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.00 G1 Refine#0 31835 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:03.54 VM Thread 31836 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.01 Reference Handl 31837 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.00 Finalizer 31838 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.00 Signal Dispatch 31840 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.05 Service Thread 31841 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:57.08 C2 CompilerThre 31842 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:06.89 C1 CompilerThre 31843 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.21 Sweeper thread 31847 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:40.97 VM Periodic Tas 31848 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.05 Common-Cleaner 31867 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.00 AsyncAppender-W 31879 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.00 AsyncAppender-W 31896 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.54 GC Thread#1 31897 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.55 GC Thread#2 31898 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.54 GC Thread#3 31899 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.54 GC Thread#4 31900 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.55 GC Thread#5 31901 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.54 GC Thread#6 31902 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.55 GC Thread#7 31903 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.54 GC Thread#8 31904 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.55 GC Thread#9 31905 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.55 GC Thread#10 31906 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.55 GC Thread#11 31907 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.55 GC Thread#12 31908 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.55 GC Thread#13 31909 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.55 GC Thread#14 31910 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.55 GC Thread#15 31911 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.55 GC Thread#16 31912 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.56 GC Thread#17 31914 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.54 GC Thread#18 31915 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.55 GC Thread#19 31916 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.55 GC Thread#20 31917 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.54 GC Thread#21 31919 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.55 GC Thread#22 31920 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.55 GC Thread#23 31921 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.54 GC Thread#24 31922 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.55 GC Thread#25 31923 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.55 GC Thread#26 31924 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.55 GC Thread#27 31925 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:02.73 Timer-0 31926 root 20 0 23.953g 2.074g 24544 S 0.0 1.7 0:00.49 com.alibaba.nac
3、将高负载线程号转换成16进制
语法: printf “%x\n”
eg: printf “%x\n” 31795
~$ printf "%x\n" 31795 7c33
4、jstack语法
~$ jstack Usage: jstack [-l][-e] <pid> (to connect to running process) Options: -l long listing. Prints additional information about locks -e extended listing. Prints additional information about threads -? -h --help -help to print this help message
5、jstack打印堆栈信息
jstack -l 14552
jstack -l 14552 2023-05-11 10:19:04 Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.191-b12 mixed mode): "DestroyJavaVM" #31 prio=5 os_prio=0 tid=0x000000000323e800 nid=0x2668 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "TimerQueue" #30 daemon prio=5 os_prio=0 tid=0x000000003f5ae000 nid=0x13c4 waiting on condition [0x000000003808f000] java.lang.Thread.State: TIMED_WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x00000005fb9f32c0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject) at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078) at java.util.concurrent.DelayQueue.take(DelayQueue.java:223) at javax.swing.TimerQueue.run(TimerQueue.java:174) at java.lang.Thread.run(Thread.java:748) Locked ownable synchronizers: - <0x00000005fb9f32e8> (a java.util.concurrent.locks.ReentrantLock$NonfairSync) "AWT-EventQueue-0" #26 prio=6 os_prio=0 tid=0x00000000311f3000 nid=0x3604 waiting on condition [0x0000000032d5f000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x0000000601cb3528> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039) at java.awt.EventQueue.getNextEvent(EventQueue.java:554) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:187) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) Locked ownable synchronizers: - None "AWT-Windows" #24 daemon prio=6 os_prio=0 tid=0x00000000312a0000 nid=0x10d8 runnable [0x000000003210e000] java.lang.Thread.State: RUNNABLE at sun.awt.windows.WToolkit.eventLoop(Native Method) at sun.awt.windows.WToolkit.run(WToolkit.java:313) at java.lang.Thread.run(Thread.java:748) Locked ownable synchronizers: - None "AWT-Shutdown" #23 prio=5 os_prio=0 tid=0x000000003129f800 nid=0x22f8 in Object.wait() [0x000000003200f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000006018c0a40> (a java.lang.Object) at java.lang.Object.wait(Object.java:502) at sun.awt.AWTAutoShutdown.run(AWTAutoShutdown.java:295) - locked <0x00000006018c0a40> (a java.lang.Object) at java.lang.Thread.run(Thread.java:748) Locked ownable synchronizers: - None "Java2D Disposer" #22 daemon prio=10 os_prio=2 tid=0x0000000031245000 nid=0x3cb0 in Object.wait() [0x0000000031f0e000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000006018c0a50> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144) - locked <0x00000006018c0a50> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165) at sun.java2d.Disposer.run(Disposer.java:148) at java.lang.Thread.run(Thread.java:748) Locked ownable synchronizers: - None "Service Thread" #19 daemon prio=9 os_prio=0 tid=0x000000002ee6f800 nid=0x1898 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "C1 CompilerThread11" #18 daemon prio=9 os_prio=2 tid=0x000000002ee46800 nid=0x1db8 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "C1 CompilerThread10" #17 daemon prio=9 os_prio=2 tid=0x000000002ee26000 nid=0x39fc waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "C1 CompilerThread9" #16 daemon prio=9 os_prio=2 tid=0x000000002edfc800 nid=0x3e00 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "C1 CompilerThread8" #15 daemon prio=9 os_prio=2 tid=0x000000002eddb800 nid=0x2944 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "C2 CompilerThread7" #14 daemon prio=9 os_prio=2 tid=0x000000002edd3000 nid=0x1af4 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "C2 CompilerThread6" #13 daemon prio=9 os_prio=2 tid=0x000000002edd0000 nid=0x3c4c waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "C2 CompilerThread5" #12 daemon prio=9 os_prio=2 tid=0x000000002edcf800 nid=0x3d34 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "C2 CompilerThread4" #11 daemon prio=9 os_prio=2 tid=0x000000002edce800 nid=0x3de0 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "C2 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x000000002e27e000 nid=0x3ae0 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000000002e27d000 nid=0x2dcc waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000000002e27c800 nid=0x2604 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000000002e27b800 nid=0x1080 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "Attach Listener" #6 daemon prio=5 os_prio=2 tid=0x000000002e267800 nid=0x814 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "Signal Dispatcher" #5 daemon prio=9 os_prio=2 tid=0x000000002e267000 nid=0x39f4 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "Surrogate Locker Thread (Concurrent GC)" #4 daemon prio=9 os_prio=0 tid=0x000000002edc3800 nid=0x3c60 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000002e241800 nid=0x3cec in Object.wait() [0x000000002ed7f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000005c6241d58> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144) - locked <0x00000005c6241d58> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216) Locked ownable synchronizers: - None "Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x000000002e239000 nid=0x3c58 in Object.wait() [0x000000002ec7f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000005c6232c40> (a java.lang.ref.Reference$Lock) at java.lang.Object.wait(Object.java:502) at java.lang.ref.Reference.tryHandlePending(Reference.java:191) - locked <0x00000005c6232c40> (a java.lang.ref.Reference$Lock) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153) Locked ownable synchronizers: - None "VM Thread" os_prio=2 tid=0x000000002e213800 nid=0x21a4 runnable "Gang worker#0 (Parallel GC Threads)" os_prio=2 tid=0x000000000335c000 nid=0x2f80 runnable "Gang worker#1 (Parallel GC Threads)" os_prio=2 tid=0x000000000335d800 nid=0x1d0c runnable "Gang worker#2 (Parallel GC Threads)" os_prio=2 tid=0x000000000335f000 nid=0x3b20 runnable "Gang worker#3 (Parallel GC Threads)" os_prio=2 tid=0x0000000003360800 nid=0x2454 runnable "Gang worker#4 (Parallel GC Threads)" os_prio=2 tid=0x0000000003363000 nid=0x34b8 runnable "Gang worker#5 (Parallel GC Threads)" os_prio=2 tid=0x0000000003364000 nid=0x3e6c runnable "Gang worker#6 (Parallel GC Threads)" os_prio=2 tid=0x0000000003367000 nid=0x1fe4 runnable "Gang worker#7 (Parallel GC Threads)" os_prio=2 tid=0x0000000003368800 nid=0x191c runnable "Gang worker#8 (Parallel GC Threads)" os_prio=2 tid=0x0000000003369800 nid=0x3da4 runnable "Gang worker#9 (Parallel GC Threads)" os_prio=2 tid=0x000000000336b000 nid=0x12d4 runnable "Gang worker#10 (Parallel GC Threads)" os_prio=2 tid=0x000000000336c000 nid=0xc0 runnable "Gang worker#11 (Parallel GC Threads)" os_prio=2 tid=0x000000000336f800 nid=0x2430 runnable "Gang worker#12 (Parallel GC Threads)" os_prio=2 tid=0x0000000003371000 nid=0x66c runnable "G1 Main Concurrent Mark GC Thread" os_prio=2 tid=0x0000000003400000 nid=0x3c08 runnable "Gang worker#0 (G1 Parallel Marking Threads)" os_prio=2 tid=0x0000000003401800 nid=0x3c7c runnable "Gang worker#1 (G1 Parallel Marking Threads)" os_prio=2 tid=0x0000000003402000 nid=0x3fc8 runnable "Gang worker#2 (G1 Parallel Marking Threads)" os_prio=2 tid=0x0000000003402800 nid=0x3e80 runnable "G1 Concurrent Refinement Thread#0" os_prio=2 tid=0x0000000003395000 nid=0x3de4 runnable "G1 Concurrent Refinement Thread#1" os_prio=2 tid=0x0000000003396000 nid=0x9a4 runnable "G1 Concurrent Refinement Thread#2" os_prio=2 tid=0x000000000338d800 nid=0x289c runnable "G1 Concurrent Refinement Thread#3" os_prio=2 tid=0x0000000003389000 nid=0x2804 runnable "G1 Concurrent Refinement Thread#4" os_prio=2 tid=0x0000000003388800 nid=0x114 runnable "G1 Concurrent Refinement Thread#5" os_prio=2 tid=0x0000000003387800 nid=0x3fa4 runnable "G1 Concurrent Refinement Thread#6" os_prio=2 tid=0x0000000003385000 nid=0x10ac runnable "G1 Concurrent Refinement Thread#7" os_prio=2 tid=0x0000000003384000 nid=0x237c runnable "G1 Concurrent Refinement Thread#8" os_prio=2 tid=0x0000000003383800 nid=0xfc runnable "G1 Concurrent Refinement Thread#9" os_prio=2 tid=0x0000000003383000 nid=0x3efc runnable "G1 Concurrent Refinement Thread#10" os_prio=2 tid=0x000000000337c000 nid=0x30c8 runnable "G1 Concurrent Refinement Thread#11" os_prio=2 tid=0x0000000003376000 nid=0x3ef8 runnable "G1 Concurrent Refinement Thread#12" os_prio=2 tid=0x0000000003375000 nid=0x2b74 runnable "G1 Concurrent Refinement Thread#13" os_prio=2 tid=0x0000000003374000 nid=0x82c runnable "VM Periodic Task Thread" os_prio=2 tid=0x000000002ee72800 nid=0x34b4 waiting on condition JNI global references: 849
6、将堆栈信息保存到文件
jstack -l >> jstack.log jstack -l 14552 >> jstack.log
可以通过16进制ID查看相关线程
7、遇到的问题
问题1、无访问权限
~$ jstack -l 31795 31795: Permission denied
那自然是要权限了,或者在docker容器里执行
参考链接:
https://blog.csdn.net/qq_42947952/article/details/126617731
https://www.cnblogs.com/snake23/p/10329149.html