Previous post: Learning the underlying principles of JVM (four) garbage collection
JVM parameter configuration
Standard parameter
- (-) At the beginning, all JVM implementations must implement the functions of these parameters, and the backward compatibility of standard parameters is relatively fixed and will not be easily changed. The release of the JDK version will not change the standard parameters.
-version 输出JDK的版本信息
-help 输出JDK的帮助信息
-server/client 指定JVM的运行为server/client的运行
-cp/classpath 指定classpath路径
-DpropertyName=value 指定系统变量propertyName=value
示例:
System.getProperty("propertyName") //得到value
Non-standard parameters (-X)
(-X) header, the default JVM implements the functions of these parameters, but it does not guarantee that all JVM implementations are satisfied, and backward compatibility is not guaranteed
-Xms256M 设置最小堆(Heap)空间
-Xmx256M 设置最大堆(Heap)空间
-Xmn512M 设置Young区的大小
-Xss356k 设置线程栈的大小(深度)JDK5.0以后每个线程堆栈大小为1M,
以前每个线程堆栈大小为256K,应该基于应用的线程所需内存大小进行调整。
在相同物理内存下,减小这个值能生成更多的线程
Non-stable parameters (unstable parameters) (-XX)
At the beginning of (-XX), each JVM implementation of this type of parameter will be different, and it may be cancelled at any time in the future. It needs to be used with caution. So it is also called unstable parameter.
- Assignment parameter- XX:propertyName=value
-XX:MaxGCPauseMillis=500 垃圾回收器最大的停顿时间 -XX:InitialHeapSize=1000M -XX:MaxHeapSize=1000M -XX:ThreadStackSize=256K 能进行修改的参数(linux环境下) java -XX:+PrintFlagsFinal -version |grep manageable --linux
- Boolean parameter -XX:+propertyName
-XX:+UseConcMarkSweepGC 指定CMS垃圾回收器
-XX:+UseG1GC 指定G1垃圾回收器
-XX:+UseCompressedOops 开启压缩指针
-XX:+PrintFlagsFinal 打印所有的参数
Check the situation that is not specified in JDK1.8. Who is the default garbage collector.
The combination of PS+PO is used by default
前面加:=的参数是修改过的参数[jvm启动系统修改或者认为修改都是:=]
[manageable]的表示可以在程序运行期间通过jinfo进行修改的
[product]的表示不可以在程序运行期间通过jinfo进行修改的.
bool HeapDumpOnOutOfMemoryError //自动heap溢出异常保存heap快照
ccstr HeapDumpPath //heap快照保存位置
List of common parameters
parameter | meaning | Description |
---|---|---|
-XX:InitialHeapSize=100M | Set the initial heap size | Same as -Xms100M |
-XX:MaxHeapSize=100M | Set the maximum size of Heap | Same as -Xms100M |
-XX:NewSize=20M | Set the size of Heap Young generation | Young generation |
-XX:MaxNewSize=50M | Set the maximum size of Heap Young | Young generation |
-XX:OldSize=50M | Set Heap Old size | Old age |
-XX:MetaspaceSize=50M | Set the size of Metaspace | There is meta space before 1.6 |
-XX:MaxMetaspaceSize=50M | Set the maximum size of Metaspace space | |
-XX:+UseParallelGC | Use UseParallelGC | Cenozoic GC, throughput priority |
-XX:+UseParallelOldGC | Use UseParallelOldGC | In the old age, throughput first |
-XX:+UseConcMarkSweepGC | Use CMS GC | In the old age, pause time has priority |
-XX:+UseG1GC | Use G1GC | New generation, old generation, pause time priority |
-XX:NewRatio | Ratio of new and old generations | For example -XX:Ratio=4, it means that the new generation: old generation=1:4, that is, the new generation occupies 1/5 of the entire heap memory |
-XX:SurvivorRatio | The ratio of the two S-zones to the Eden zone | For example-XX:SurvivorRatio=8, which is (S0+S1):Eden=2:8, which means that one S accounts for 1/10 of the entire new generation |
-XX:+HeapDumpOnOutOfMemoryError | Start heap overflow printing | When the JVM heap memory overflows, that is, OOM, a dump file is automatically generated |
-XX:HeapDumpPath=heap.hprof | Specify the print location of Heap snapshot | Indicates that a heap.hprof file is generated in the current directory |
-XX:+PrintGCDetails -XX:+PrintGCTimeStamps - XX:+PrintGCDateStamps -Xloggc:g1- gc.log | Print out the GC log | You can use different garbage collectors to compare and view the GC situation |
-Xss128k | Set the stack size of each thread | The experience value is 3000-5000 best |
-XX:MaxTenuringThreshold=6 | Raise the maximum threshold of the old generation | The default value is 15 |
-XX:InitiatingHeapOccupancyPercent | Heap memory usage percentage when starting concurrent GC cycle | Garbage collectors such as G1 use it to trigger concurrent GC cycles, based on the usage of the entire heap, not just the usage of a certain generation of memory. A value of 0 means "always execute GC cycles". The default value is 45. |
-XX:G1HeapWastePercent | Allowable percentage of wasted heap space | The default is 10%. If the reclaimable space of concurrent marking is less than 10%, MixedGC will not be triggered |
-XX:MaxGCPauseMillis=100ms | G1 maximum pause time | The pause time cannot be too small. If it is too small, it will cause G1 to fail to keep up with the rate of garbage generation. Eventually degenerate into Full GC. Therefore, the tuning of this parameter is a continuous process, gradually adjusting to the best state. |
-XX:ConcGCThreads=n | The number of threads used by the concurrent garbage collector | The default value varies with the platform on which the JVM runs |
-XX:+UseAdaptiveSizePolicy | 动态调整java堆中各个区域的大小以及进入老年代的年龄 | |
-XX:CompileThreshold=1000 | 表示一个方法被调用1000次之后, 会被认为是热点代码,并触发即时编译 |
优化问题排查及分析工具
jdk自带工具
- jps 罗列当前系统中运行的JVM进程
jps
不带参数,默认显示 进程ID 和 启动类的名称
jps -m
含义:参数 -m 可以输出传递给 Java 进程(main 方法)的参数。
jps -l
输出主函数的完整路径(类的全路径)
jps -v
(显示传递给 JVM 的参数)
- jinfo 主要用于打印配置信息,包括命令行参数、系统变量和主动修改部分配置信息
官网位置: https://docs.oracle.com/en/java/javase/11/tools/jinfo. html#GUID-69246B58-28C4-477D-B375-278F5F9830A5
jinfo -flag {PID}
打印{PID}对应java进程的 属性的值) 如:jinfo -flag PrintGC {PID}
jinfo -flags {PID}
打印{PID}对应java进程曾经赋值过的参数 如: jinfo -flags {PID}
jinfo -sysprops {PID}
打印所有{PID}对应Java进程的系统参数键值对
jinfo -flag [+|-] {PID} jinfo -flag = {PID}
给对应{PID}JVM进程修改对应的属性.
-XX:+PrintFlagsFinal 打印的结果中 [manageable]的可以通过jinfo进行修改
如 :jinfo -flag +HeapDumpOnOutOfMemoryError {PID} jinfo -flag HeapDumpPath=/root/jvmcamp/test.hprof
- jstat
对JVM应用程序的资源和性能进行实时的反馈的命令行工具. 其中包括了对类加载,堆内存使用情况和垃圾回收等诸多方面…
命令解释:jstat [-命令选项] {PID} [间隔时间] [查询次数]
官网:https://docs.oracle.com/en/java/javase/11/tools/jstat.html# GUID-5F72A7F9-5D5A-4486-8201-E1D1BA8ACCB5
jstat -options
获取jstat可以获取哪些信息的监控.
- -class 用于查看类加载情况的统计
- -compiler 用于查看HotSpot中即时编译器编译情况的统计
- -gc 用于查看JVM中堆的垃圾收集情况的统计
- -gccapacity 用于查看新生代、老生代及持久代的存储容量情况
- -gcmetacapacity 显示metaspace的情况
- -gcnew 用于查看新生代垃圾收集的情况
- -gcnewcapacity 用于查看新生代存储容量的情况
- -gcold 用于查看老生代及持久代垃圾收集的情况
- -gcoldcapacity 用于查看老生代的容量
- -gcutil 显示垃圾收集信息
- -gccause 显示垃圾回收的相关信息(同-gcutil),同时显示最后一次仅当前正在发生的垃圾收集的原因
- -printcompilation 输出JIT编译的方法信息
示例:
- 查看类装载信息
jstat -class {PID} 3000 20
输出{PID}JVM进程的类装载信息,每3000毫秒输出一次,共输出20次
- 查看垃圾回收的信息
jstat -gc {PID} 2000 5
输出{PID}JVM进程的GC信息,每2000毫秒输出一次,共计输出5次
- jstack
Jstack是Jdk自带的线程跟踪工具,用于获取指定JVM进程的线程堆栈信息。
我们可基于jstack获取的线程堆栈等信息去定位线程出现长时间停 顿的原因,线程间死锁、死循环、请求外部资源导致的长时间等待等等JVM性能缓慢的情况
官网: https://docs.oracle.com/en/java/javase/11/tools/jstack.html#GUID-721096FC-237B-473C-A461-DBBBB79E4F6A
用法:jstack {pid}
利用jstack排查死锁
CPU飙升排查
top 指令找到对象的进程. 如果是java进程
top -Hp {PID}找到CPU使用最高的线程pid 将此值改变为16进制
jstack {PID} | grep 16进制PID -A20
关注线程状态
RUNNABLE,线程处于执行中
BLOCKED,线程被阻塞
WAITING,线程正在等待
- jmap
jmap命令是一个可以输出JVM某一时刻运行状态的命令[内存,GC情况,对象存活情况]
查看 GC使用的算法,heap(堆)的配置及JVM堆内存的使用情况
jmap -heap {PID}
Young各个部分比例E:S1:S2为什么不是8:1:1?
Old:Young的比例好像也不是2:1
原因:是因为JVM开启了自适应调整的参数
jinfo -flag UsePSAdaptiveSurvivorSizePolicy {PID}
查看JVM中各个对象数量,大小[live]
jmap -histo:live {PID}
dump堆里的对象信息和内存信息,进行相关的分析 慎用
为什么慎用? 整个堆的dump过程中,是暂停所有执行线程的业务逻辑的.可以认为是STW的
jmap -dump:format=b,file=J://resource//andy.hprof {PID}
jmap - dump:live,format=b,file=J://resource//any.hprof {PID}
开发过程中,我们可以加上
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=heap.hprof
这两个参数在JVM程序出现堆异常时,自动dump 内存快照信息
使用MAT工具进行分析
使用在线的MAT(Memory Analyzer Tool)工具分析 https://heaphero.io/
- jhat is
a very difficult tool for statistical analysis based on the hprof file of jmap dump, which is very unfriendly to Chinese programmers...
jhat -port 8888 e: //demo.hprof
- jconsole
is a real-time monitoring tool provided by the JDK. It is not easy to use. - jvisualvm
is a real-time monitoring tool provided by the JDK. It is easier to use. (recommended)- To monitor the native JVM process, just double-click to open it... You can view the configuration information, CPU, thread execution details, etc.
- Analyze the memory snapshot hprof file
- Analyze the JVM process of the remote server
Useful third-party tools
- arthas
arthas is Alibaba's open source Java real-time monitoring and troubleshooting tool. It is monitored and checked in a command-line interactive manner. The
official code hosting github https://github.com/alibaba/arthas
You can go to its official website to learn more And use. Common commands