目录
一、Arthas学习
1、JVM相关命令一
1、dashboard
2、thread
3、jvm
4、sysprop
一、Arthas学习
Arthas(阿尔萨斯)--(一)
Arthas代码开源地址
1、JVM相关命令一
1、dashboard
dashboard:显示当前系统的实时数据面板,按q或ctrl+c退出
ID: Java 级别的线程 ID,注意这个 ID 不能跟 jstack 中的 nativeID 一一对应。
NAME: 线程名
GROUP: 线程组名
PRIORITY: 线程优先级, 1~10 之间的数字,越大表示优先级越高
STATE: 线程的状态
CPU%: 线程的 cpu 使用率。比如采样间隔 1000ms,某个线程的增量 cpu 时间为 100ms,则 cpu 使用率=100/1000=10%
DELTA_TIME: 上次采样之后线程运行增量 CPU 时间,数据格式为秒
TIME: 线程运行总 CPU 时间,数据格式为分:秒
INTERRUPTED: 线程当前的中断位状态
DAEMON: 是否是 daemon 线程
2、thread
thread:查看当前JVM的线程堆栈信息
参数:数字:线程id
[n:]:指定最忙的前N个线程并打印堆栈
[arthas@14156]$ thread -n 3
"main" Id=1 cpuUsage=0.0% deltaTime=0ms time=218ms TIMED_WAITING
at java.base@21.0.1/java.lang.Thread.sleep0(Native Method)
at java.base@21.0.1/java.lang.Thread.sleep(Thread.java:558)
at java.base@21.0.1/java.util.concurrent.TimeUnit.sleep(TimeUnit.java:446)
at app//demo.MathGame.main(MathGame.java:17)
"Reference Handler" Id=9 cpuUsage=0.0% deltaTime=0ms time=0ms RUNNABLE
at java.base@21.0.1/java.lang.ref.Reference.waitForReferencePendingList(Native Method)
at java.base@21.0.1/java.lang.ref.Reference.processPendingReferences(Reference.java:246)
at java.base@21.0.1/java.lang.ref.Reference$ReferenceHandler.run(Reference.java:208)
"Finalizer" Id=10 cpuUsage=0.0% deltaTime=0ms time=0ms WAITING on java.lang.ref.NativeReferenceQueue$Lock@2b55402b
at java.base@21.0.1/java.lang.Object.wait0(Native Method)
- waiting on java.lang.ref.NativeReferenceQueue$Lock@2b55402b
at java.base@21.0.1/java.lang.Object.wait(Object.java:366)
at java.base@21.0.1/java.lang.Object.wait(Object.java:339)
at java.base@21.0.1/java.lang.ref.NativeReferenceQueue.await(NativeReferenceQueue.java:48)
at java.base@21.0.1/java.lang.ref.ReferenceQueue.remove0(ReferenceQueue.java:158)
at java.base@21.0.1/java.lang.ref.NativeReferenceQueue.remove(NativeReferenceQueue.java:89)
at java.base@21.0.1/java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:173)
[arthas@14156]$
[b]:找出当前阻塞其他线程的线程
有时候我们发现应用卡住了, 通常是由于某个线程拿住了某个锁, 并且其他线程都在等待这把锁造成的。 为了排查这类问题, arthas 提供了thread -b
运行一个死锁程序
public class DeadLock {
static Object a = new Object();
static Object b = new Object();
public static void main(String[] args) {
new Thread(() -> {
synchronized (a) {
System.out.println(Thread.currentThread().getName() + "持有锁a,试图获取锁b");
synchronized (b) {
System.out.println(Thread.currentThread().getName() + "持有锁b");
}
}
}, "t1").start();
new Thread(() -> {
synchronized (b) {
System.out.println(Thread.currentThread().getName() + "持有锁b,试图获取锁a");
synchronized (a) {
System.out.println(Thread.currentThread().getName() + "持有锁a");
}
}
}, "t2").start();
}
}
运行arthas,粘附到这个死锁程序中
[arthas@11400]$ thread -b
"t2" Id=34 BLOCKED on java.lang.Object@1e8eb080 owned by "t1" Id=33
at app//com.lwz.xxx.DeadLock.lambda$main$1(DeadLock.java:21)
- blocked on java.lang.Object@1e8eb080
- locked java.lang.Object@6fe0e9fb <---- but blocks 1 other threads!
at app//com.lwz.controller.DeadLock$$Lambda/0x0000020111003418.run(Unknown Source)
at java.base@21.0.1/java.lang.Thread.runWith(Thread.java:1596)
at java.base@21.0.1/java.lang.Thread.run(Thread.java:1583)
[arthas@11400]$
[i <value>]:指定cpu占比统计的采样间隔,单位为毫秒
thread -i 1000 : 统计最近 1000ms 内的线程 CPU 时间。
thread -n 3 -i 1000 : 列出 1000ms 内最忙的 3 个线程栈
[arthas@14156]$ thread -i 1000
Threads Total: 16, NEW: 0, RUNNABLE: 9, BLOCKED: 0, WAITING: 3, TIMED_WAITING: 4, TERMINATED: 0
ID NAME GROUP PRIORITY STATE %CPU DELTA_TIM TIME INTERRUPT DAEMON
1 main main 5 TIMED_WA 0.0 0.000 0:0.234 false false
9 Reference Handler system 10 RUNNABLE 0.0 0.000 0:0.000 false true
10 Finalizer system 8 WAITING 0.0 0.000 0:0.000 false true
11 Signal Dispatcher system 9 RUNNABLE 0.0 0.000 0:0.000 false true
12 Attach Listener system 5 RUNNABLE 0.0 0.000 0:0.046 false true
30 Notification Thread system 9 RUNNABLE 0.0 0.000 0:0.000 false true
31 Common-Cleaner InnocuousThrea 8 TIMED_WA 0.0 0.000 0:0.000 false true
33 arthas-timer system 5 WAITING 0.0 0.000 0:0.000 false true
59 arthas-NettyHttpTelnetBootstr system 5 RUNNABLE 0.0 0.000 0:0.046 false true
60 arthas-NettyWebsocketTtyBoots system 5 RUNNABLE 0.0 0.000 0:0.000 false true
61 arthas-NettyWebsocketTtyBoots system 5 RUNNABLE 0.0 0.000 0:0.000 false true
62 arthas-shell-server system 5 TIMED_WA 0.0 0.000 0:0.000 false true
63 arthas-session-manager system 5 TIMED_WA 0.0 0.000 0:0.000 false true
64 arthas-UserStat system 5 WAITING 0.0 0.000 0:0.000 false true
66 arthas-NettyHttpTelnetBootstr system 5 RUNNABLE 0.0 0.000 0:0.140 false true
67 arthas-command-execute system 5 RUNNABLE 0.0 0.000 0:0.046 false true
[arthas@14156]$ thread -n 3 -i 1000
"main" Id=1 cpuUsage=0.0% deltaTime=0ms time=234ms TIMED_WAITING
at java.base@21.0.1/java.lang.Thread.sleep0(Native Method)
at java.base@21.0.1/java.lang.Thread.sleep(Thread.java:558)
at java.base@21.0.1/java.util.concurrent.TimeUnit.sleep(TimeUnit.java:446)
at app//demo.MathGame.main(MathGame.java:17)
"Reference Handler" Id=9 cpuUsage=0.0% deltaTime=0ms time=0ms RUNNABLE
at java.base@21.0.1/java.lang.ref.Reference.waitForReferencePendingList(Native Method)
at java.base@21.0.1/java.lang.ref.Reference.processPendingReferences(Reference.java:246)
at java.base@21.0.1/java.lang.ref.Reference$ReferenceHandler.run(Reference.java:208)
"Finalizer" Id=10 cpuUsage=0.0% deltaTime=0ms time=0ms WAITING on java.lang.ref.NativeReferenceQueue$Lock@2b55402b
at java.base@21.0.1/java.lang.Object.wait0(Native Method)
- waiting on java.lang.ref.NativeReferenceQueue$Lock@2b55402b
at java.base@21.0.1/java.lang.Object.wait(Object.java:366)
at java.base@21.0.1/java.lang.Object.wait(Object.java:339)
at java.base@21.0.1/java.lang.ref.NativeReferenceQueue.await(NativeReferenceQueue.java:48)
at java.base@21.0.1/java.lang.ref.ReferenceQueue.remove0(ReferenceQueue.java:158)
at java.base@21.0.1/java.lang.ref.NativeReferenceQueue.remove(NativeReferenceQueue.java:89)
at java.base@21.0.1/java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:173)
例:
thread --state WAITING :查看线程中处于等待状态的线程
3、jvm
与JVM相关的信息
[arthas@14156]$ jvm
RUNTIME
-----------------------------------------------------------------------------------------------------------------------
MACHINE-NAME 14156@lwzoozyc
JVM-START-TIME 2023-11-13 23:10:24
MANAGEMENT-SPEC-VERSION 4.0
SPEC-NAME Java Virtual Machine Specification
SPEC-VENDOR Oracle Corporation
SPEC-VERSION 21
VM-NAME Java HotSpot(TM) 64-Bit Server VM
VM-VENDOR Oracle Corporation
VM-VERSION 21.0.1+12-LTS-29
INPUT-ARGUMENTS []
CLASS-PATH math-game.jar
BOOT-CLASS-PATH
LIBRARY-PATH C:\Program Files\Java\jdk-21\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Win
dows;C:\Program Files\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Comm
on Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wb
em;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Prog
ram Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation
\NVIDIA NvDLISR;"C:\Program Files\Java\jdk-21\bin;C:\Program Files\Java\jdk-21\jre\
bin;";C:\Program Files\Go\bin;C:\Program Files\Go\bin;C:\Program Files\PuTTY\;C:\Pr
ogram Files\Git\cmd;C:\Program Files\TortoiseGit\bin;C:\Program Files\Microsoft VS
Code\bin;C:\Program Files\Java\jdk-21\bin;C:\Users\Administrator\AppData\Local\Micr
osoft\WindowsApps;C:\Users\Administrator\go\bin;C:\Program Files\JetBrains\GoLand 2
023.2\bin;;C:\Program Files\JetBrains\IntelliJ IDEA 2023.2.3\bin;;.
-----------------------------------------------------------------------------------------------------------------------
CLASS-LOADING
-----------------------------------------------------------------------------------------------------------------------
LOADED-CLASS-COUNT 4623
TOTAL-LOADED-CLASS-COUNT 4623
UNLOADED-CLASS-COUNT 0
IS-VERBOSE false
-----------------------------------------------------------------------------------------------------------------------
COMPILATION
-----------------------------------------------------------------------------------------------------------------------
NAME HotSpot 64-Bit Tiered Compilers
TOTAL-COMPILE-TIME 3011
[time (ms)]
-----------------------------------------------------------------------------------------------------------------------
GARBAGE-COLLECTORS
-----------------------------------------------------------------------------------------------------------------------
G1 Young Generation name : G1 Young Generation
[count/time (ms)] collectionCount : 1
collectionTime : 11
G1 Concurrent GC name : G1 Concurrent GC
[count/time (ms)] collectionCount : 0
collectionTime : 0
G1 Old Generation name : G1 Old Generation
[count/time (ms)] collectionCount : 0
collectionTime : 0
-----------------------------------------------------------------------------------------------------------------------
MEMORY-MANAGERS
-----------------------------------------------------------------------------------------------------------------------
CodeCacheManager CodeHeap 'non-nmethods'
CodeHeap 'profiled nmethods'
CodeHeap 'non-profiled nmethods'
Metaspace Manager Metaspace
Compressed Class Space
G1 Young Generation G1 Eden Space
G1 Survivor Space
G1 Old Gen
G1 Concurrent GC G1 Old Gen
G1 Old Generation G1 Eden Space
G1 Survivor Space
G1 Old Gen
-----------------------------------------------------------------------------------------------------------------------
MEMORY
-----------------------------------------------------------------------------------------------------------------------
HEAP-MEMORY-USAGE init : 1073741824(1.0 GiB)
[memory in bytes] used : 27318272(26.1 MiB)
committed : 1073741824(1.0 GiB)
max : 17146314752(16.0 GiB)
NO-HEAP-MEMORY-USAGE init : 7667712(7.3 MiB)
[memory in bytes] used : 31832208(30.4 MiB)
committed : 36438016(34.8 MiB)
max : -1(-1 B)
PENDING-FINALIZE-COUNT 0
-----------------------------------------------------------------------------------------------------------------------
OPERATING-SYSTEM
-----------------------------------------------------------------------------------------------------------------------
OS Windows 10
ARCH amd64
PROCESSORS-COUNT 36
LOAD-AVERAGE -1.0
VERSION 10.0
-----------------------------------------------------------------------------------------------------------------------
THREAD
-----------------------------------------------------------------------------------------------------------------------
COUNT 16
DAEMON-COUNT 15
PEAK-COUNT 16
STARTED-COUNT 19
DEADLOCK-COUNT 0
-----------------------------------------------------------------------------------------------------------------------
FILE-DESCRIPTOR
-----------------------------------------------------------------------------------------------------------------------
MAX-FILE-DESCRIPTOR-COUNT -1
OPEN-FILE-DESCRIPTOR-COUNT -1
4、sysprop
sysprop:查看和修改JVM的系统属性
[arthas@14156]$ sysprop
KEY VALUE
-----------------------------------------------------------------------------------------------------------------------
java.specification.ver 21
sion
sun.cpu.isalist amd64
sun.jnu.encoding GBK
java.class.path math-game.jar
java.vm.vendor Oracle Corporation
sun.arch.data.model 64
user.variant
java.vendor.url https://java.oracle.com/
user.timezone Asia/Shanghai
java.vm.specification. 21
version
os.name Windows 10
sun.java.launcher SUN_STANDARD
user.country CN
sun.boot.library.path C:\Program Files\Java\jdk-21\bin
sun.java.command math-game.jar
jdk.debug release
sun.cpu.endian little
user.home C:\Users\Administrator
user.language zh
java.specification.ven Oracle Corporation
dor
java.version.date 2023-10-17
java.home C:\Program Files\Java\jdk-21
file.separator \
java.vm.compressedOops Zero based
Mode
java.vm.specification. Oracle Corporation
vendor
java.specification.nam Java Platform API Specification
e
user.script
sun.management.compile HotSpot 64-Bit Tiered Compilers
r
java.runtime.version 21.0.1+12-LTS-29
user.name Administrator
stdout.encoding ms936
path.separator ;
os.version 10.0
java.runtime.name Java(TM) SE Runtime Environment
file.encoding UTF-8
java.vm.name Java HotSpot(TM) 64-Bit Server VM
java.vendor.url.bug https://bugreport.java.com/bugreport/
java.io.tmpdir C:\Users\ADMINI~1\AppData\Local\Temp\
java.version 21.0.1
user.dir C:\Users\Administrator\.arthas\lib\3.7.1\arthas
os.arch amd64
java.vm.specification. Java Virtual Machine Specification
name
sun.os.patch.level
native.encoding GBK
java.library.path C:\Program Files\Java\jdk-21\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Pr
ogram Files\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Oracle\Java
\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\Windows
PowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX
\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;"C:\Program Files\Java\jdk-21\bin;
C:\Program Files\Java\jdk-21\jre\bin;";C:\Program Files\Go\bin;C:\Program Files\Go\bin;C:\Pro
gram Files\PuTTY\;C:\Program Files\Git\cmd;C:\Program Files\TortoiseGit\bin;C:\Program Files\
Microsoft VS Code\bin;C:\Program Files\Java\jdk-21\bin;C:\Users\Administrator\AppData\Local\M
icrosoft\WindowsApps;C:\Users\Administrator\go\bin;C:\Program Files\JetBrains\GoLand 2023.2\b
in;;C:\Program Files\JetBrains\IntelliJ IDEA 2023.2.3\bin;;.
java.vm.info mixed mode, sharing
stderr.encoding ms936
java.vendor Oracle Corporation
java.vm.version 21.0.1+12-LTS-29
sun.io.unicode.encodin UnicodeLittle
g
java.class.version 65.0
[arthas@14156]$
查看单个属性
[arthas@14156]$ sysprop java.version
KEY VALUE
------------------------------------------------------------------------------------------
java.version 21.0.1
[arthas@14156]$
修改单个属性
[arthas@14156]$ sysprop user.country
KEY VALUE
------------------------------------------------------------------------------------------
user.country CN
[arthas@14156]$ sysprop user.country US
Successfully changed the system property.
KEY VALUE
------------------------------------------------------------------------------------------
user.country US
[arthas@14156]$ sysprop user.country CN
Successfully changed the system property.
KEY VALUE
------------------------------------------------------------------------------------------
user.country CN
[arthas@14156]$
2、JVM相关命令二
1、sysenv
2、vmoption
3、getstatic
4、ognl
一个程序员最重要的能力是:写出高质量的代码!!
有道无术,术尚可求也,有术无道,止于术。
无论你是年轻还是年长,所有程序员都需要记住:时刻努力学习新技术,否则就会被时代抛弃!