查看JVM默认参数及微调JVM启动参数
参考廖雪峰老师的这篇
JVM调优的正确姿势: https://www.liaoxuefeng.com/article/1336345083510818
查看某个JVM进程堆内存信息
更多java工具命令可以在 jdk的bin目录下看到,命令怎么用可以使用 -help
# 查询端口号
C:\Users\admin>netstat -ano|findstr 8081
TCP 0.0.0.0:8081 0.0.0.0:0 LISTENING 8792
TCP [::]:8081 [::]:0 LISTENING 8792
# jmap -heap <pid>
C:\Users\admin>jmap -heap 8792
Attaching to process ID 8792, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.251-b08
using thread-local object allocation.
Parallel GC with 4 thread(s)
Heap Configuration:
MinHeapFreeRatio = 0
MaxHeapFreeRatio = 100
MaxHeapSize = 2147483648 (2048.0MB)
NewSize = 715653120 (682.5MB)
MaxNewSize = 715653120 (682.5MB)
OldSize = 1431830528 (1365.5MB)
NewRatio = 2
SurvivorRatio = 8
MetaspaceSize = 536870912 (512.0MB)
CompressedClassSpaceSize = 528482304 (504.0MB)
MaxMetaspaceSize = 536870912 (512.0MB)
G1HeapRegionSize = 0 (0.0MB)
Heap Usage:
PS Young Generation
Eden Space:
capacity = 537395200 (512.5MB)
used = 390193272 (372.11730194091797MB)
free = 147201928 (140.38269805908203MB)
72.60825403725228% used
From Space:
capacity = 89128960 (85.0MB)
used = 27779432 (26.492530822753906MB)
free = 61349528 (58.507469177246094MB)
31.167683320886947% used
To Space:
capacity = 89128960 (85.0MB)
used = 0 (0.0MB)
free = 89128960 (85.0MB)
0.0% used
PS Old Generation
capacity = 1431830528 (1365.5MB)
used = 122896 (0.1172027587890625MB)
free = 1431707632 (1365.382797241211MB)
0.008583138688323874% used
23174 interned Strings occupying 2401368 bytes.
微调JVM启动参数
JVM参数大致可以分为三类
-
标准指令:
-
开头,这些是所有的HotSpot
都支持的参数。可以用java -help
打印出来。 -
非标准指令:
-X
开头,这些指令通常是跟特定的HotSpot
版本对应的。可以用java -X
打印出来。 -
不稳定参数:
-XX
开头,这一类参数是跟特定HotSpot
版本对应的,并且变化非常大。详细的文档资料非常少。
在JDK1.8版本下有几个常用的不稳定指令
java -XX:+PrintCommandLineFlags
:查看当前命令的不稳定指令
java -XX:+PrintFlagsInitial
:查看所有不稳定指令的默认值
java -XX:+PrintFlagsFinal
:查看所有不稳定指令最终生效的实际值
JDK1.8之前元空间叫永久代
STW(Stop-The-World):是指JVM在执行垃圾收集算法时,所有的用户线程都被挂起(除了垃圾收集帮助器之外)。Java中一种全局暂停现象,全局停顿,所有Java代码停止,native代码可以执行,但不能与JVM交互;这些停顿现象多半是由于GC引起。
堆内存默认分配:新生区占1/3,老年区占2/3
新生区分为3块 Eden、From(S0)、To(S1),默认占比是 8:1:1
# -Xms: 设置堆初始化内存大小, 默认是 1/64
# -Xmx: 设置最大分配内存大小, 默认是 1/4
# -Xss: 线程栈大小
# -XX:MetaspaceSize: 元空间大小
# -XX:MaxMetaspaceSize: 元空间最大大小
java -Xms2G -Xmx2G -Xss1M -XX:MetaspaceSize=512M -XX:MaxMetaspaceSize=512M -jar app.jar
# -XX:MaxGCPauseMillis: GC进行STW的最大停顿时间, JVM将尽可能(但不保证)停顿小于这个时间
# -XX:+UseG1GC 使用G1垃圾收集器
java -Xms2G -Xmx2G -Xss1M -XX:+UseG1GC -XX:MaxGCPauseMillis=100ms -XX:MetaspaceSize=512M -XX:MaxMetaspaceSize=512M -jar app.jar
查看JVM的一些默认参数
C:\Users\admin>java -XX:+PrintCommandLineFlags -version
-XX:InitialHeapSize=132884864 -XX:MaxHeapSize=2126157824 -XX:+PrintCommandLineFlags
-XX:+UseCompressedClassPointers -XX:+UseCompressedOops
-XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)
从上面可以看出JDK8默认使用的GC是 ParallelGC
= Parallel Scavenge + Parallel Old
JDK各个版本的GC,来源:https://www.cnblogs.com/zeze/p/6610163.html
新生代
- 1.3:Seriall
- 1.4:ParNew、Parallel Scavenge
老年代
- 1.5:CMS、Serial Old(MSC)
- 1.6:Parallel Old
- 1.7:G1(收集器在JDK 1.7 u4版本正式投入使用)
查看GC的详细信息
C:\Users\admin>java -XX:+PrintGCDetails -version
java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)
Heap
PSYoungGen total 38400K, used 2005K [0x00000000d5c00000, 0x00000000d8680000, 0x0000000100000000)
eden space 33280K, 6% used [0x00000000d5c00000,0x00000000d5df54b8,0x00000000d7c80000)
from space 5120K, 0% used [0x00000000d8180000,0x00000000d8180000,0x00000000d8680000)
to space 5120K, 0% used [0x00000000d7c80000,0x00000000d7c80000,0x00000000d8180000)
ParOldGen total 87552K, used 0K [0x0000000081400000, 0x0000000086980000, 0x00000000d5c00000)
object space 87552K, 0% used [0x0000000081400000,0x0000000081400000,0x0000000086980000)
Metaspace used 2360K, capacity 4480K, committed 4480K, reserved 1056768K
class space used 255K, capacity 384K, committed 384K, reserved 1048576K