Java8默认的是并行GC(Parallel GC)
Java 11默认的是G1 GC
Java命令行指令(Java 8)
重点
⭐️jstat
⭐️jmap
⭐️jstack
⭐️jcmd
jps
jps
查看Java程序进程的PID
jps
,jps -l
,jps -mlv
:jps -mlv
可以把对应的JVM的参数也打印出来。
PS C:\Program Files\Java> jps -l
14624 jdk.jcmd/sun.tools.jps.Jps
292 gateway-server-0.0.1-SNAPSHOT.jar
1896 org.jetbrains.idea.maven.server.RemoteMavenServer36
15788
PS C:\Program Files\Java> jps
292 gateway-server-0.0.1-SNAPSHOT.jar
1896 RemoteMavenServer36
5016 Jps
15788
PS C:\Program Files\Java> jps -mlv
292 gateway-server-0.0.1-SNAPSHOT.jar --server.port=8088
9156 jdk.jcmd/sun.tools.jps.Jps -mlv -Dapplication.home=C:\Program Files\Java\jdk-11.0.2 -Xms8m -Djdk.module.main=jdk.jcmd
1896 org.jetbrains.idea.maven.server.RemoteMavenServer36 -Djava.awt.headless=true -Dmaven.defaultProjectBuilder.disableGlobalModelCache=true -Xmx768m -Didea.maven.embedder.version=3.6.3 -Dmaven.ext.class.path=D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.3\plugins\maven\lib\maven-event-listener.jar -Dfile.encoding=UTF-8
15788 exit -Xms128m -Xmx2027m -XX:ReservedCodeCacheSize=240m -XX:+UseConcMarkSweepGC -XX:SoftRefLRUPolicyMSPerMB=50 -ea -XX:CICompilerCount=2 -Dsun.io.useCanonPrefixCache=false -Djava.net.preferIPv4Stack=true -Djdk.http.auth.tunneling.disabledSchemes="" -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -Djdk.attach.allowAttachSelf=true -Dkotlinx.coroutines.debug=off -Djdk.module.illegalAccess.silent=true -Dfile.encoding=UTF-8 -Djb.vmOptionsFile=C:\Users\simon\AppData\Roaming\JetBrains\IntelliJIdea2020.1\idea64.exe.vmoptions -Djava.library.path=D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.3\jbr\\bin;D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.3\jbr\\bin\server -Didea.jre.check=true -Dide.native.launcher=true -Didea.paths.selector=IntelliJIdea2020.1 -XX:ErrorFile=C:\Users\simon\java_error_in_idea_%p.log -XX:HeapDumpPath=C:\Users\simon\java_error_in_idea.hprof
PS C:\Program Files\Java>
jinfo
jinfo
。查看Java的配置信息,包括类路径以及虚拟机的参数等等。
打印出来的信息非常多,包括相关的classpath
等
jstat
jstat
:主要用来查看GC的情况,查看堆中各个区的使用的情况,以及YGC和FGC发生的次数,发生的平均时间等。
主要有两种使用方法。
-gc GC
查看堆内存各个区的使用情况. 用法: jstat -gc -h 10 -t 864 1s 20
-gcutil GC
查看内存各个区的使用率(utilization)
jstat -gc 12668 1000 1000
:每1000秒打1000次,相当于1秒打印一条日志,总共打印1000次
PS C:\Program Files\Java\jdk-11.0.2\bin> jps -l
2848 gateway-server-0.0.1-SNAPSHOT.jar
10008
14968 jdk.jcmd/sun.tools.jps.Jps
12668 org.jetbrains.idea.maven.server.RemoteMavenServer36
PS C:\Program Files\Java\jdk-11.0.2\bin> jstat -gc 12668 1000 1000
S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT CGC CGCT GCT
0.0 2048.0 0.0 2048.0 72704.0 61440.0 185344.0 3505.8 21504.0 20715.7 2560.0 2297.0 3 0.009 0 0.000 2 0.002 0.011
0.0 2048.0 0.0 2048.0 72704.0 61440.0 185344.0 3505.8 21504.0 20715.7 2560.0 2297.0 3 0.009 0 0.000 2 0.002 0.011
0.0 2048.0 0.0 2048.0 72704.0 61440.0 185344.0 3505.8 21504.0 20715.7 2560.0 2297.0 3 0.009 0 0.000 2 0.002 0.011
0.0 2048.0 0.0 2048.0 72704.0 61440.0 185344.0 3505.8 21504.0 20715.7 2560.0 2297.0 3 0.009 0 0.000 2 0.002 0.011
......
more
......
-gcutil
打印的是堆的各个区的占用率。也就是各个区的占用率除以该区的容量。
jstat -gcutil 12668 1000 1000
PS C:\Program Files\Java\jdk-11.0.2\bin> jstat -gcutil 12668 1000 1000
S0 S1 E O M CCS YGC YGCT FGC FGCT CGC CGCT GCT
0.00 100.00 84.51 1.89 96.33 89.73 3 0.009 0 0.000 2 0.002 0.011
0.00 100.00 84.51 1.89 96.33 89.73 3 0.009 0 0.000 2 0.002 0.011
0.00 100.00 84.51 1.89 96.33 89.73 3 0.009 0 0.000 2 0.002 0.011
0.00 100.00 84.51 1.89 96.33 89.73 3 0.009 0 0.000 2 0.002 0.011
0.00 100.00 84.51 1.89 96.33 89.73 3 0.009 0 0.000 2 0.002 0.011
0.00 100.00 84.51 1.89 96.33 89.73 3 0.009 0 0.000 2 0.002 0.011
0.00 100.00 84.51 1.89 96.33 89.73 3 0.009 0 0.000 2 0.002 0.011
0.00 100.00 84.51 1.89 96.33 89.73 3 0.009 0 0.000 2 0.002 0.011
0.00 100.00 84.51 1.89 96.33 89.73 3 0.009 0 0.000 2 0.002 0.011
0.00 100.00 84.51 1.89 96.33 89.73 3 0.009 0 0.000 2 0.002 0.011
运行一个网关的jar包
java -jar gateway-server-0.0.1-SNAPSHOT.jar --server.port=8088
使用wrk
对其进行压测:
root@simon-computer:/home/simon/dev/wrk# ./wrk http://localhost:8088/api/hello
Running 10s test @ http://localhost:8088/api/hello
2 threads and 10 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 3.23ms 5.65ms 78.22ms 90.60%
Req/Sec 3.46k 1.83k 6.83k 53.50%
68963 requests in 10.01s, 8.23MB read
Requests/sec: 6890.04
Transfer/sec: 842.33KB
root@simon-computer:/home/simon/dev/wrk#
进行压测以后可以发现YGC的次数在不断增加
当继续增加压测时,可以发现YGC的次数也在不断增加。
root@simon-computer:/home/simon/dev/wrk# ./wrk -t 30 -c 30 -d 30 http://localhost:8088/api/hello
Running 30s test @ http://localhost:8088/api/hello
30 threads and 30 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 4.31ms 9.95ms 193.84ms 92.39%
Req/Sec 646.28 279.04 4.39k 80.21%
578954 requests in 30.06s, 69.12MB read
Requests/sec: 19258.91
Transfer/sec: 2.30MB
############ 参数说明 ##############
-t 需要模拟的线程数
-c 需要模拟的连接数
–timeout 超时的时间
-d 测试的持续时间
jstat -gcnew 5269 1000 1000
只看new
区的GC
root@simon-computer:/home/simon# jstat -gcnew 5269 1000 1000
S0C S1C S0U S1U TT MTT DSS EC EU YGC YGCT
1024.0 1024.0 0.0 512.0 15 15 1024.0 113664.0 36233.3 249 0.560
1024.0 1024.0 0.0 512.0 15 15 1024.0 113664.0 36233.3 249 0.560
1024.0 1024.0 0.0 512.0 15 15 1024.0 113664.0 36233.3 249 0.560
1024.0 1024.0 0.0 512.0 15 15 1024.0 113664.0 36233.3 249 0.560
1024.0 1024.0 0.0 512.0 15 15 1024.0 113664.0 36233.3 249 0.560
1024.0 1024.0 0.0 512.0 15 15 1024.0 113664.0 36233.3 249 0.560
1024.0 1024.0 0.0 512.0 15 15 1024.0 113664.0 36233.3 249 0.560
1024.0 1024.0 0.0 512.0 15 15 1024.0 113664.0 36233.3 249 0.560
1024.0 1024.0 0.0 512.0 15 15 1024.0 113664.0 36233.3 249 0.560
1024.0 1024.0 0.0 512.0 15 15 1024.0 113664.0 36233.3 249 0.560
1024.0 1024.0 0.0 512.0 15 15 1024.0 113664.0 36233.3 249 0.560
1024.0 1024.0 0.0 512.0 15 15 1024.0 113664.0 36233.3 249 0.560
1024.0 1024.0 0.0 512.0 15 15 1024.0 113664.0 36233.3 249 0.560
jstat -gcold 5269 1000 1000
只看old
区的GC
root@simon-computer:/home/simon# jstat -gcold 5269 1000 1000
MC MU CCSC CCSU OC OU YGC FGC FGCT GCT
38360.0 36635.4 4824.0 4525.9 93184.0 56638.2 312 3 0.247 1.625
38360.0 36635.4 4824.0 4525.9 93184.0 56638.2 314 3 0.247 1.641
38360.0 36635.4 4824.0 4525.9 93184.0 56710.2 316 3 0.247 1.656
38360.0 36635.4 4824.0 4525.9 93184.0 57640.0 317 3 0.247 1.677
38360.0 36635.4 4824.0 4525.9 93184.0 57640.0 317 3 0.247 1.677
38360.0 36635.4 4824.0 4525.9 93184.0 57640.0 317 3 0.247 1.677
38360.0 36635.4 4824.0 4525.9 93184.0 57640.0 317 3 0.247 1.677
38360.0 36635.4 4824.0 4525.9 93184.0 57640.0 317 3 0.247 1.677
38360.0 36635.4 4824.0 4525.9 93184.0 57640.0 317 3 0.247 1.677
38360.0 36635.4 4824.0 4525.9 93184.0 57640.0 317 3 0.247 1.677
38360.0 36635.4 4824.0 4525.9 93184.0 57640.0 317 3 0.247 1.677
38360.0 36635.4 4824.0 4525.9 93184.0 57640.0 317 3 0.247 1.677
38360.0 36635.4 4824.0 4525.9 93184.0 57640.0 317 3 0.247 1.677
38360.0 36635.4 4824.0 4525.9 93184.0 57640.0 317 3 0.247 1.677
jmap
jmap
:查看Java内存的使用情况
主要使用两个命令:jmap -heap
和jmap -histo
jmap -heap pid
jmap -histo pid
#在Java 11上jamp -heap pid不生效了。需要使用
jhsdb jmap --heap --pid 18844
jmap -heap 6802
:查看对应Java进程的堆相关的信息
root@simon-computer:/home/simon/dev/wrk# jps -l
6802 gateway-server-0.0.1-SNAPSHOT.jar
6841 sun.tools.jps.Jps
root@simon-computer:/home/simon/dev/wrk# jmap -heap 6802
Attaching to process ID 6802, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.271-b09
using thread-local object allocation.
Parallel GC with 2 thread(s)
Heap Configuration:
MinHeapFreeRatio = 0
MaxHeapFreeRatio = 100
MaxHeapSize = 1025507328 (978.0MB)
NewSize = 21495808 (20.5MB)
MaxNewSize = 341835776 (326.0MB)
OldSize = 43515904 (41.5MB)
NewRatio = 2
SurvivorRatio = 8
MetaspaceSize = 21807104 (20.796875MB)
CompressedClassSpaceSize = 1073741824 (1024.0MB)
MaxMetaspaceSize = 17592186044415 MB
G1HeapRegionSize = 0 (0.0MB)
Heap Usage:
PS Young Generation
Eden Space:
capacity = 256901120 (245.0MB)
used = 156875144 (149.60779571533203MB)
free = 100025976 (95.39220428466797MB)
61.064406414421235% used
From Space:
capacity = 7340032 (7.0MB)
used = 7311440 (6.9727325439453125MB)
free = 28592 (0.0272674560546875MB)
99.61046491350446% used
To Space:
capacity = 12058624 (11.5MB)
used = 0 (0.0MB)
free = 12058624 (11.5MB)
0.0% used
PS Old Generation
capacity = 28835840 (27.5MB)
used = 15413560 (14.699516296386719MB)
free = 13422280 (12.800483703613281MB)
53.45278653231534% used
15784 interned Strings occupying 2113640 bytes.
root@simon-computer:/home/simon/dev/wrk#
jmap -histo 6802
:查看对应Java进程的类的的实例的信息。
JVM 的堆转储文件(heap dump)是某个时间点、Java 进程的内存快照。包含了当时内存中还没有被 full GC 回收的对象和类信息。
root@simon-computer:/home/simon# jmap -histo 6802 > tmp.txt
root@simon-computer:/home/simon# ls
core Desktop dev Documents Downloads Music Pictures Public snap Templates tmp.txt Videos
root@simon-computer:/home/simon# vim tmp.txt
num #instances #bytes class name
----------------------------------------------
1: 513263 113691832 [C # 字符类型
2: 32693 19999832 [I # int类型
3: 17657 13610256 [B # byte类型
4: 299314 7183536 java.lang.String
5: 39042 3435696 java.lang.reflect.Method
6: 39651 2537664 java.net.URL
7: 36273 2518304 [Ljava.lang.Object;
8: 104492 2125240 [Ljava.lang.Class;
9: 18103 1445384 [S
10: 36055 1153760 org.springframework.boot.loader.jar.StringSequence
11: 12312 1146352 [Ljava.util.HashMap$Node;
12: 30894 988608 java.util.HashMap$Node
13: 36055 865320 org.springframework.boot.loader.jar.JarURLConnection$JarEntryName
14: 7051 782696 java.lang.Class
15: 22508 720256 java.util.concurrent.ConcurrentHashMap$Node
16: 10789 503976 [Ljava.lang.reflect.Method;
17: 20988 503712 java.lang.StringBuilder
......
jstack
常用命令
jstack pid -l #查看所有的线程,堆栈的信息,看一些锁的状态
Java线程的状态
root@simon-computer:/home/simon# jstack -l 6802 > stack.txt
root@simon-computer:/home/simon# ls
core Desktop dev Documents Downloads Music Pictures Public snap stack.txt Templates tmp.txt Videos
root@simon-computer:/home/simon# vim stack.txt
2021-01-17 17:01:08
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.271-b09 mixed mode):
2021-01-17 17:01:08
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.271-b09 mixed mode):
"Attach Listener" #32 daemon prio=9 os_prio=0 tid=0x00007fa66c001800 nid=0x1b04 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE #线程处于RUNNABLE状态
Locked ownable synchronizers:
- None
...省略...
"http-nio-8088-AsyncTimeout" #29 daemon prio=5 os_prio=0 tid=0x00007fa6987ff000 nid=0x1ab8 waiting on condition [0x00007fa64e7f0000]
java.lang.Thread.State: TIMED_WAITING (sleeping) #线程处于sleeping状态
at java.lang.Thread.sleep(Native Method)
at org.apache.coyote.AbstractProtocol$AsyncTimeout.run(AbstractProtocol.java:1143)
at java.lang.Thread.run(Thread.java:748)
Locked ownable synchronizers:
- None
...省略...
"container-0" #14 prio=5 os_prio=0 tid=0x00007fa69b6ac000 nid=0x1aa9 waiting on condition [0x00007fa67c159000]
java.lang.Thread.State: TIMED_WAITING (sleeping) #超时等待
at java.lang.Thread.sleep(Native Method)
at org.apache.catalina.core.StandardServer.await(StandardServer.java:427)
at org.springframework.boot.web.embedded.tomcat.TomcatWebServer$1.run(TomcatWebServer.java:182)
Locked ownable synchronizers:
- None
kill
kill -9 #杀掉一个进程,其实是发一个信号量給对应经常,和CTRL + C停止一个进行的原理相同
kill -3 # 打印对应进程的Stack信息,
kill -9 10438
root@simon-computer:/home/simon/dev/wrk# jps -l
10438 gateway-server-0.0.1-SNAPSHOT.jar
10471 sun.tools.jps.Jps
root@simon-computer:/home/simon/dev/wrk# kill -9 10438
root@simon-computer:/home/simon/dev/wrk# jps -l
10523 sun.tools.jps.Jps
root@simon-computer:/home/simon/dev/wrk#
kill -3 10552
kill -3打印对应的堆栈信息
jcmd
jcmp pid 参数 #整合了前面的命令
jcmd 10552 help
查看对应的JAVA进程所支持的命令
root@simon-computer:/home/simon/dev/wrk# jps -l
10704 sun.tools.jps.Jps
10552 gateway-server-0.0.1-SNAPSHOT.jar
root@simon-computer:/home/simon/dev/wrk# jcmd 10552 help
10552:
The following commands are available:
JFR.stop
JFR.start
JFR.dump
JFR.check
VM.native_memory
VM.check_commercial_features
VM.unlock_commercial_features
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
VM.classloader_stats
GC.rotate_log
Thread.print #类似于jstack
GC.class_stats #查看类统计的信息
GC.class_histogram #与jmap -histo的作用类似,查看各个类的实例的数量
GC.heap_dump
GC.finalizer_info
GC.heap_info #查看堆内存的信息
GC.run_finalization
GC.run
VM.uptime #查看VM的运行时间
VM.dynlibs
VM.flags #查看VM开启的参数,比如用了什么GC,堆的大小等
VM.system_properties
VM.command_line #查看命令行信息,执行jar包时的命令行参数
VM.version
help
示例
root@simon-computer:/home/simon/dev/wrk# jps -l
10836 sun.tools.jps.Jps
10552 gateway-server-0.0.1-SNAPSHOT.jar
root@simon-computer:/home/simon/dev/wrk# jcmd 10552 VM.version
10552:
Java HotSpot(TM) 64-Bit Server VM version 25.271-b09
JDK 8.0_271
root@simon-computer:/home/simon/dev/wrk# jcmd 10552 GC.heap_info
10552:
PSYoungGen total 282624K, used 26725K [0x00000000eba00000, 0x0000000100000000, 0x0000000100000000)
eden space 269824K, 9% used [0x00000000eba00000,0x00000000ed419638,0x00000000fc180000)
from space 12800K, 0% used [0x00000000fcd80000,0x00000000fcd80000,0x00000000fda00000)
to space 12288K, 0% used [0x00000000fc180000,0x00000000fc180000,0x00000000fcd80000)
ParOldGen total 55296K, used 18343K [0x00000000c2e00000, 0x00000000c6400000, 0x00000000eba00000)
object space 55296K, 33% used [0x00000000c2e00000,0x00000000c3fe9dc8,0x00000000c6400000)
Metaspace used 34010K, capacity 35524K, committed 35672K, reserved 1081344K
class space used 4363K, capacity 4640K, committed 4696K, reserved 1048576K
root@simon-computer:/home/simon/dev/wrk# jcmd 10552 VM.command_line #查看命令行信息
10552:
VM Arguments:
java_command: gateway-server-0.0.1-SNAPSHOT.jar --server.port=8088
java_class_path (initial): gateway-server-0.0.1-SNAPSHOT.jar
Launcher Type: SUN_STANDARD
root@simon-computer:/home/simon/dev/wrk# jcmd 10552 VM.flags #查看VM开启的参数
10552:
-XX:CICompilerCount=2 -XX:InitialHeapSize=65011712 -XX:MaxHeapSize=1025507328 -XX:MaxNewSize=341835776 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=21495808 -XX:OldSize=43515904 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:+UseParallelGC
root@simon-computer:/home/simon/dev/wrk#
JVM图形化工具
jconsole
位于JDK的bin目录下
jvisualvm
Oracle JDK8的bin目录下提供了这个工具,从Oracle JDK9开始,作为一个单独的工具,需要单独下载安装。
特点:提供了抽样器,对一段时间内进行抽样
visual GC
在IDEA中作为一个插件提供
特点:以方格的形式查看GC
jmc(最强大)
可以将一段时间内抽样的结果保存到文件中,并生成分析报告。从Oracle JDK8以后需要单独下载。
#jmc在/root/program/jmc/jmc-7.1.2_linux-x64/bin目录下
root@simon-computer:~/program/jmc/jmc-7.1.2_linux-x64/bin# ./jmc #运行
参考:极客时间Java训练营