目录
一、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_WAITINGat 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 RUNNABLEat 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@2b55402bat java.base@21.0.1/java.lang.Object.wait0(Native Method)- waiting on java.lang.ref.NativeReferenceQueue$Lock@2b55402bat 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=33at 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_WAITINGat 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 RUNNABLEat 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@2b55402bat java.base@21.0.1/java.lang.Object.wait0(Native Method)- waiting on java.lang.ref.NativeReferenceQueue$Lock@2b55402bat 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]$ jvmRUNTIME
-----------------------------------------------------------------------------------------------------------------------MACHINE-NAME 14156@lwzoozycJVM-START-TIME 2023-11-13 23:10:24MANAGEMENT-SPEC-VERSION 4.0SPEC-NAME Java Virtual Machine SpecificationSPEC-VENDOR Oracle CorporationSPEC-VERSION 21VM-NAME Java HotSpot(TM) 64-Bit Server VMVM-VENDOR Oracle CorporationVM-VERSION 21.0.1+12-LTS-29INPUT-ARGUMENTS []CLASS-PATH math-game.jarBOOT-CLASS-PATHLIBRARY-PATH C:\Program Files\Java\jdk-21\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program 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\WindowsPowerShell\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:\Program Files\PuTTY\;C:\Program Files\Git\cmd;C:\Program Files\TortoiseGit\bin;C:\Program Files\Microsoft VSCode\bin;C:\Program Files\Java\jdk-21\bin;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\Users\Administrator\go\bin;C:\Program Files\JetBrains\GoLand 2023.2\bin;;C:\Program Files\JetBrains\IntelliJ IDEA 2023.2.3\bin;;.-----------------------------------------------------------------------------------------------------------------------CLASS-LOADING
-----------------------------------------------------------------------------------------------------------------------LOADED-CLASS-COUNT 4623TOTAL-LOADED-CLASS-COUNT 4623UNLOADED-CLASS-COUNT 0IS-VERBOSE false-----------------------------------------------------------------------------------------------------------------------COMPILATION
-----------------------------------------------------------------------------------------------------------------------NAME HotSpot 64-Bit Tiered CompilersTOTAL-COMPILE-TIME 3011[time (ms)]-----------------------------------------------------------------------------------------------------------------------GARBAGE-COLLECTORS
-----------------------------------------------------------------------------------------------------------------------G1 Young Generation name : G1 Young Generation[count/time (ms)] collectionCount : 1collectionTime : 11G1 Concurrent GC name : G1 Concurrent GC[count/time (ms)] collectionCount : 0collectionTime : 0G1 Old Generation name : G1 Old Generation[count/time (ms)] collectionCount : 0collectionTime : 0-----------------------------------------------------------------------------------------------------------------------MEMORY-MANAGERS
-----------------------------------------------------------------------------------------------------------------------CodeCacheManager CodeHeap 'non-nmethods'CodeHeap 'profiled nmethods'CodeHeap 'non-profiled nmethods'Metaspace Manager MetaspaceCompressed Class SpaceG1 Young Generation G1 Eden SpaceG1 Survivor SpaceG1 Old GenG1 Concurrent GC G1 Old GenG1 Old Generation G1 Eden SpaceG1 Survivor SpaceG1 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 10ARCH amd64PROCESSORS-COUNT 36LOAD-AVERAGE -1.0VERSION 10.0-----------------------------------------------------------------------------------------------------------------------THREAD
-----------------------------------------------------------------------------------------------------------------------COUNT 16DAEMON-COUNT 15PEAK-COUNT 16STARTED-COUNT 19DEADLOCK-COUNT 0-----------------------------------------------------------------------------------------------------------------------FILE-DESCRIPTOR
-----------------------------------------------------------------------------------------------------------------------MAX-FILE-DESCRIPTOR-COUNT -1OPEN-FILE-DESCRIPTOR-COUNT -1
4、sysprop
sysprop:查看和修改JVM的系统属性
[arthas@14156]$ syspropKEY VALUE
-----------------------------------------------------------------------------------------------------------------------java.specification.ver 21sionsun.cpu.isalist amd64sun.jnu.encoding GBKjava.class.path math-game.jarjava.vm.vendor Oracle Corporationsun.arch.data.model 64user.variantjava.vendor.url https://java.oracle.com/user.timezone Asia/Shanghaijava.vm.specification. 21versionos.name Windows 10sun.java.launcher SUN_STANDARDuser.country CNsun.boot.library.path C:\Program Files\Java\jdk-21\binsun.java.command math-game.jarjdk.debug releasesun.cpu.endian littleuser.home C:\Users\Administratoruser.language zhjava.specification.ven Oracle Corporationdorjava.version.date 2023-10-17java.home C:\Program Files\Java\jdk-21file.separator \java.vm.compressedOops Zero basedModejava.vm.specification. Oracle Corporationvendorjava.specification.nam Java Platform API Specificationeuser.scriptsun.management.compile HotSpot 64-Bit Tiered Compilersrjava.runtime.version 21.0.1+12-LTS-29user.name Administratorstdout.encoding ms936path.separator ;os.version 10.0java.runtime.name Java(TM) SE Runtime Environmentfile.encoding UTF-8java.vm.name Java HotSpot(TM) 64-Bit Server VMjava.vendor.url.bug https://bugreport.java.com/bugreport/java.io.tmpdir C:\Users\ADMINI~1\AppData\Local\Temp\java.version 21.0.1user.dir C:\Users\Administrator\.arthas\lib\3.7.1\arthasos.arch amd64java.vm.specification. Java Virtual Machine Specificationnamesun.os.patch.levelnative.encoding GBKjava.library.path C:\Program Files\Java\jdk-21\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program 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\WindowsPowerShell\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:\Program 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\Microsoft\WindowsApps;C:\Users\Administrator\go\bin;C:\Program Files\JetBrains\GoLand 2023.2\bin;;C:\Program Files\JetBrains\IntelliJ IDEA 2023.2.3\bin;;.java.vm.info mixed mode, sharingstderr.encoding ms936java.vendor Oracle Corporationjava.vm.version 21.0.1+12-LTS-29sun.io.unicode.encodin UnicodeLittlegjava.class.version 65.0
[arthas@14156]$
查看单个属性
[arthas@14156]$ sysprop java.versionKEY VALUE
------------------------------------------------------------------------------------------java.version 21.0.1
[arthas@14156]$
修改单个属性
[arthas@14156]$ sysprop user.countryKEY 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
一个程序员最重要的能力是:写出高质量的代码!!
有道无术,术尚可求也,有术无道,止于术。
无论你是年轻还是年长,所有程序员都需要记住:时刻努力学习新技术,否则就会被时代抛弃!