JVM 分析工具概述
-
JPS
-
Jstat
-
Jinfo
-
Jmap
-
Jhat (一般不用,記憶體佔用較高)
-
Jstack
-
Jconsole
-
visualVm
JPS
java process status 檢視程序
jps
C:\Users\84407>jps
13680 Jps
14704 JConsole
10148 Launcher
8788
10844 ThreadConsole
jps -q
僅僅顯示VM 標示,不顯示jar,class, main引數等資訊
C:\Users\84407>jps -q
18324
15336
19144
19816
jps -m
輸出主函式傳入的引數
C:\Users\84407>jps -m 15336 ThreadConsole 19144 19816 Launcher C:/Program Files/JetBrains/IntelliJ IDEA 2018.1/lib/annotations.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2018.1/lib/httpclient-4.5.2.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2018.1/lib/slf4j-api-1.7.10.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2018.1/lib/log4j.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2018.1/lib/snappy-in-java-0.5.1.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2018.1/lib/util.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2018.1/lib/jna.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2018.1/lib/idea_rt.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2018.1/lib/jps-model.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2018.1/lib/jna-platform.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2018.1/lib/oro-2.0.8.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2018.1/lib/aether-dependency-resolver.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2018.1/lib/httpcore-4.4.5.jar;C:/Program Files/JetBrains/IntelliJ IDEA 2018.1/lib/netty-all-4.1.13.Final.jar;C:/Program Files/ 19148 Jps -m
IntelliJ IDEA的pid:19816,引數就是後面那一大堆
jps -l
輸出應用程式主類完整package名稱或jar完整名稱
C:\Users\84407>jps -l
15336 com.jvm.jconsole.ThreadConsole
19144
19816 org.jetbrains.jps.cmdline.Launcher
9704 jdk.jcmd/sun.tools.jps.Jps
jps -v
列出jvm引數
15336 ThreadConsole -javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1\lib\idea_rt.jar=61866:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1\bin -Dfile.encoding=UTF-8 19144 -Xms128m -Xmx750m -XX:ReservedCodeCacheSize=240m -XX:+UseConcMarkSweepGC -XX:SoftRefLRUPolicyMSPerMB=50 -ea -Dsun.io.useCanonCaches=false -Djava.net.preferIPv4Stack=true -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -Dide.no.platform.update=true -Djb.vmOptionsFile=C:\Program Files\JetBrains\IntelliJ IDEA 2018.1\bin\idea64.exe.vmoptions -Didea.jre.check=true -Dide.native.launcher=true -Didea.paths.selector=IntelliJIdea2018.1 -XX:ErrorFile=C:\Users\84407\java_error_in_idea_%p.log -XX:HeapDumpPath=C:\Users\84407\java_error_in_idea.hprof 19816 Launcher -Xmx700m -Djava.awt.headless=true -Djava.endorsed.dirs="" -Djdt.compiler.useSingleThread=true -Dpreload.project.path=D:/workspace/java-nio-and-netty-spring-demo -Dpreload.config.path=C:/Users/84407/.IntelliJIdea2018.1/config/options -Dcompile.parallel=false -Drebuild.on.dependency.change=true -Djava.net.preferIPv4Stack=true -Dio.netty.initialSeedUniquifier=-6623792082140627021 -Dfile.encoding=GBK -Duser.language=zh -Duser.country=CN -Didea.paths.selector=IntelliJIdea2018.1 -Didea.home.path=C:\Program Files\JetBrains\IntelliJ IDEA 2018.1 -Didea.config.path=C:\Users\84407\.IntelliJIdea2018.1\config -Didea.plugins.path=C:\Users\84407\.IntelliJIdea2018.1\config\plugins -Djps.log.dir=C:/Users/84407/.IntelliJIdea2018.1/system/log/build-log -Djps.fallback.jdk.home=C:/Program Files/JetBrains/IntelliJ IDEA 2018.1/jre64 -Djps.fallback.jdk.version=1.8.0_152-release -Dio.netty.noUnsafe=true -Djava.io.tmpdir=C:/Users/84407/.IntelliJIdea2018.1/system/compile-server/java-nio-and-netty-spring-demo_64d579b4/_temp_ -Djps.backw 15052 Jps -Denv.class.path=.;C:\Program Files\Java\jdk-10\lib;C:\Program Files\Java\jre-10\lib; -Dapplication.home=C:\Program Files\Java\jdk-10 -Xms8m -Djdk.module.main=jdk.jcmd
詳情請參考:https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jps.html
Jstat
類裝載、記憶體、垃圾收集、jit編譯資訊
M:表示元空間,不再虛擬機器中,而是使用本地記憶體。
Jinfo
實時檢視和調整虛擬機器的各項引數
jinfo -flags
檢視jvm的引數
C:\Users\84407>jinfo -flags 15336
VM Flags:
-XX:CICompilerCount=3 -XX:InitialHeapSize=201326592 -XX:MaxHeapSize=3196059648 -XX:MaxNewSize=1065353216 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=67108864 -XX:OldSize=134217728 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
jinfo -sysprops
檢視java系統引數
...
user.name=84407
java.vm.specification.version=1.8
sun.java.command=com.jvm.jconsole.ThreadConsole
java.home=C\:\\Program Files\\Java\\jdk1.8.0_144\\jre
sun.arch.data.model=64
user.language=zh
java.specification.vendor=Oracle Corporation
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode
java.version=1.8.0_144
java.ext.dirs=C\:\\Program Files\\Java\\jdk1.8.0_144\\jre\\lib\\ext;C\:\\WINDOWS\\Sun\\Java\\lib\\ext
sun.boot.class.path=C\:\\Program Files\\Java\\jdk1.8.0_144\\jre\\lib\\resources.jar;C\:\\Program Files\\Java\\jdk1.8.0_144\\jre\\lib\\rt.jar;C\:\\Program Files\\Java\\jdk1.8.0_144\\jre\\lib\\sunrsasign.jar;C\:\\Program Files\\Java\\jdk1.8.0_144\\jre\\lib\\jsse.jar;C\:\\Program Files\\Java\\jdk1.8.0_144\\jre\\lib\\jce.jar;C\:\\Program Files\\Java\\jdk1.8.0_144\\jre\\lib\\charsets.jar;C\:\\Program Files\\Java\\jdk1.8.0_144\\jre\\lib\\jfr.jar;C\:\\Program Files\\Java\\jdk1.8.0_144\\jre\\classes
java.vendor=Oracle Corporation
file.separator=\\
java.vendor.url.bug=http\://bugreport.sun.com/bugreport/
sun.io.unicode.encoding=UnicodeLittle
sun.cpu.endian=little
sun.desktop=windows
sun.cpu.isalist=amd64
...
jinfo -flag -PrintGC
表示禁用GC日誌的列印,注意是減號
jinfo -flag +PrintGC
表示啟用GC日誌的列印,注意是加號
jinfo -flag PrintGC
檢視是否開啟了GC日誌的列印,注意沒有加減號
詳情請參考:https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jinfo.html#BCGEBFDD
Jmap
列印JVM堆內物件情況,查詢Java堆和永久代的詳細資訊,使用率,使用大小
jmap -finalizerinfo
列印正等候回收的物件的資訊。
jmap -heap
列印heap的概要資訊,GC使用的演算法,heap的配置及wise heap的使用情況
jmap -clstats
列印classload的資訊。包含每個classloader的名字、活潑性、地址、父classloader和載入的class數量
jmap -histo[:live]
列印堆的直方圖
jmap -clstats
列印classload的資訊。包含每個classloader的名字、活潑性、地址、父classloader和載入的class數量
jmap -dump:format=b,file=d:\a.bin pid
生成堆資訊,並存到d:\a.bin這個檔案
jmap -histo pid | more
檢視類和例項數量和詳情:
Jhat (一般不用,記憶體佔用較高)
jvm heap analysis tool jvm堆分析工具
jhat d:\a.bin
分析堆資訊檔案,然後預設開7000埠,以網頁的形式顯示
oql
object query language,物件查詢語言
Jstack
列印執行緒堆疊資訊
jstack
檢視某個Java程序內的執行緒堆疊資訊
jstack -l
在發生死鎖時可以用jstack -l pid來觀察鎖持有情況
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x00000000033b9000 nid=0x268c in Object.wait() [0x000000001b7be000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000780808ec8> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x0000000780808ec8> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
Locked ownable synchronizers:
- None
jstack -m
不僅會輸出Java堆疊資訊,還會輸出C/C++堆疊資訊(比如Native方法)
Jconsole
記憶體監控
執行緒監控
用下面這段程式碼來測試:
public class ThreadConsole {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
scanner.next();
new Thread(new Runnable() {
@Override
public void run() {
while (true){
//空轉
}
}
},"while thread").start();
scanner.next();
new Thread(new Runnable() {
@Override
public void run() {
Object object = new Object();
synchronized (object){
try {
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
檢查死鎖
/**
* 模擬死鎖並用jconsole進行檢測
*
* Created by Fant.J.
*/
public class DeadLock {
public static void main(String[] args) {
Object o1 = new Object();
Object o2 = new Object();
new Thread(new Runnable() {
@Override
public void run() {
synchronized (o1){
try {
System.out.println("獲取到o1的鎖");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o2){
System.out.println("獲取到o2的鎖");
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
synchronized (o2){
try {
System.out.println("獲取到o2的鎖");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o1){
System.out.println("獲取到o1的鎖");
}
}
}
}).start();
}
}
visualVm
下載地址:https://visualvm.github.io/download.html 使用一看就懂,不做演示