Let’s get familiar with it today, aboutJVM
Common tuning some parameters.
Anything starting with
There have been changes in the version, but for now, the non-standardized parameters starting with X have changed very little. -XX: PrintCommandLineFlagsView the relevant parameters currently set byJVM
:
The parameter type can be distinguished according to the beginning of theJVM
parameter, there are three types in total Classes: "-
", "-X
", "-XX
",
Standard parameters (-): all JVM implementations The functions of these parameters must be implemented and backward compatible;
Examples:-verbose:class
,-verbose:gc
,-verbose:jni... …
Non-standard parameters (-X): 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;
Example :Xms20m
,-Xmx20m
,-Xmn20m
,-Xss128k...
Non-Stable parameters (-XX) : Each jvm implementation of such parameters will be different and may be canceled at any time in the future, so you need to use it with caution;
Example:-XX: PrintGCDetails
,-XX:-UseParallelGC
,-XX: PrintGCTimeStamps......
-Xms
Initial heap size, ms is the abbreviation of memory start, equivalent to-XX:InitialHeapSize
-Xmx
Maximum heap size, mx is the abbreviation of memory max, equivalent to the parameter-XX:MaxHeapSize
Note: Under normal circumstances, server projects During operation, the heap space will continue to shrink and expand, which will inevitably cause unnecessary system pressure.
So in a production environment,
Xms
andXmx
ofJVM
should be set to the same size to avoidGC
Unnecessary pressure caused by adjusting the heap size.
-XX:NewSize=n
Set the young generation size-XX:NewRatio=n
Set the ratio of the young generation to the old generation.
For example:-XX:NewRatio=3
, which means that the ratio between the young generation and the old generation is1:3
, and the young generation accounts for the total sum of the young generation and the old generation. 1/4,The default ratio between the new generation and the old generation is 1:2
.-XX:SurvivorRatio=n
The ratio of the Eden area to the two Survivor areas in the young generation.
Note that there are two Survivor areas, the default is 8, which means:Eden:S0:S1=8:1:1
For example:-XX: SurvivorRatio=3
, meansEden: Survivor
=3:2, one Survivor area accounts for 1/5 of the entire young generation.
-XX:MetaspaceSize:Metaspace
The initial size of the space, if If not set, the default is 20.79M. This initial size is the threshold that triggers the firstMetaspace Full GC
.
For example:-XX:MetaspaceSize=256M
-XX:MaxMetaspaceSize:Metaspace
The maximum value, the default is not to limit the size, but Recommended settings for online environments.
For example:-XX:MaxMetaspaceSize=256M
##-XX:MinMetaspaceFreeRatio: Minimum idle ratio whenMetaspaceoccurs After GC, the free ratio of
Metaspacewill be calculated. If the free ratio (free space/current
Metaspacesize) is less than this value,
Metaspaceexpansion will be triggered. The default value is 40, which is 40%, for example -XX:MinMetaspaceFreeRatio=40
: Maximum idle ratio, when GC occurs inMetaspace, The free ratio of It is recommended to set -Xss: stack space size, the stack is exclusive to the thread, so Is the size of the stack space used by a thread. For example: Serial garbage collector (new generation) Enable: -XX: UseSerialGC Turn off: -XX:-UseSerialGC //The new generation uses Serial and the old generation uses SerialOld ParNew garbage collector (new generation) Enable -XX: UseParNewGC Turn off -XX:-UseParNewGC //The new generation uses the function ParNew and the old generation uses the function CMS Parallel Scavenge collector (new generation) Turn on -XX: UseParallelOldGC Turn off -XX:-UseParallelOldGC //The new generation uses the function Parallel Scavenge. The old generation will use the Parallel Old collector ParallelOl garbage collector (old generation) Enable -XX: UseParallelGC Turn off -XX:-UseParallelGC //The new generation uses the function Parallel Scavenge. The old generation will use the Parallel Old collector CMS garbage collector (old generation) Enable -XX: UseConcMarkSweepGC Turn off -XX:-UseConcMarkSweepGC G1 Garbage Collector Turn on -XX: UseG1GC Close -XX:-UseG1GC Enter the minimum GC age of the old generation, and the young generation object is converted to the minimum age value of the old generation object. The default value is 7 -XX:InitialTenuringThreshol=7 Upgrade the age of the old generation, the maximum value is 15 -XX:MaxTenuringThreshold Number of GC parallel execution threads -XX: ParallelGCThreads=16 Disable System.gc(). Since this method triggers FGC by default and ignores UseG1GC and UseConcMarkSweepGC in the parameters, this method can be disabled if necessary. -XX:- DisableExplicitGC Set the throughput size, default is 99 XX:GCTimeRatio Turn on the adaptive strategy, the ratio of each area, the age of promotion to the old generation and other parameters will be automatically adjusted. To achieve a balance between throughput and pause time. XX:UseAdaptiveSizePolicy Set the percentage of GC time occupied by program running time GCTimeRatio 堆内存出现 所以给 这对于特别是对相隔数月才出现的 表示发生 这里可以不用加“”,因为 整个堆大小=年轻代大小 + 年老代大小 + 方法区大小 设置为4,则两个Survivor区与一个Eden区的比值为2:4,一个Survivor区占整个年轻代的1/6 如果设置为0的话,则年轻代对象不经过Survivor区,直接进入年老代。对于年老代比较多的应用,可以提高效率。如果将此值设置为一个较大值,则年轻代对象会在Survivor区进行多次复制,这样可以增加对象在年轻代的存活时间,增加在年轻代即被回收的概论。 比如,我们启动一个user-service项目: 参数解释: 对于很多没用过的人来说,面试官问项目中这些参数是怎么用?此时,很容易选择妥协,傻傻的回答没用过。 偷偷的告诉你,很多面试官也没有用过。 另外,你可以自己搞个小项目,把Metaspace
will be calculated. If the free ratio (free space/current Metaspace size) is greater than this value,Metaspace
will be triggered to release the space. The default value is 70, which is 70%, for example -XX:MaxMetaspaceFreeRatio=70
MetaspaceSize
andMaxMetaspaceSize
to the same size to avoid frequent expansion.Stack parameter setting
-Xss256K
, if this parameter is not set, the default value is1M
. Generally speaking, setting it to256K
is enough.Collector parameter settings
##GC strategy parameter configuration
GC pause time, the garbage collector will try various means When this time is reached, for example, reducing the proportion of the young generation
-XX:MaxGCPauseMillis
to trigger the GC, the Java heap occupancy of the marking cycle is triggered. rate threshold. The default occupancy rate is 45% of the entire Java heap
-XX:InitiatingHeapOccupancyPercent=n
The largest object that can be accommodated in the new generation. If it is larger than the maximum object that can be accommodated in the new generation, it will be allocated directly to the old generation. , 0 represents no limit.
-XX:PretenureSizeThreshold=1000000 //
Dump异常快照
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath
OOM
的概率是所有内存耗尽异常中最高的,出错时的堆内信息对解决问题非常有帮助。JVM
设置这个参数(-XX:+HeapDumpOnOutOfMemoryError
),让JVM
遇到OOM
异常时能输出堆内信息,并通过(-XX:+HeapDumpPath
)参数设置堆内存溢出快照输出的文件地址。OOM
异常尤为重要。-Xms10M -Xmx10M -Xmn2M -XX:SurvivorRatio=8 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:\study\log_hprof\gc.hprof
-XX:OnOutOfMemoryError
OOM后
,运行jconsole.exe
程序。jconsole.exe
路径Program Files含有空格。利用这个参数,我们可以在系统OOM
后,自定义一个脚本,可以用来发送邮件告警信息,可以用来重启系统等等。-XX:OnOutOfMemoryError="C:\Program Files\Java\jdk1.8.0_151\bin\jconsole.exe"
8G内存的服务器该如何设置
java -Xmx3550m -Xms3550m -Xss128k -XX:NewRatio=4 -XX:SurvivorRatio=4 -XX:MaxPermSize=16m -XX:MaxTenuringThreshold=0
-Xmx3500m
设置JVM
最大可用内存为3550M。-Xms3500m
设置JVM
初始
内存为3550m
。此值可以设置与-Xmx
相同,以避免每次垃圾回收完成后JVM重新分配内存。-Xmn2g
设置年轻代大小为2G
。
-Xss128k
设置每个线程的堆栈大小。JDK1.5
以后每个线程堆栈大小为1M,以前每个线程堆栈大小为256K。更具应用的线程所需内存大小进行调整。在相同物理内存下,减小这个值能生成更多的线程。但是操作系统对一个进程内的线程数还是有限制的,不能无限生成,经验值在3000~5000左右。-XX:NewRatio=4
设置年轻代(包括Eden和两个Survivor区)与年老代的比值(除去持久代)。设置为4,则年轻代与年老代所占比值为1:4,年轻代占整个堆栈的1/5 。-XX:SurvivorRatio=4
设置年轻代中Eden区与Survivor区的大小比值。-XX:MaxPermSize=16m
设置持久代大小为16m。-XX:MaxTenuringThreshold=0
设置垃圾最大年龄。项目中,GC日志配置
java -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+UseGCLogFileRotation -XX:+PrintHeapAtGC -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=20M -Xloggc:/opt/user-service-gc-%t.log -jar user-service-1.0-SNAPSHOT.jar
-Xloggc:/opt/app/ard-user/user-service-gc-%t.log 设置日志目录和日志名称 -XX:+UseGCLogFileRotation 开启滚动生成日志 -XX:NumberOfGCLogFiles=5 滚动GC日志文件数,默认0,不滚动 -XX:GCLogFileSize=20M GC文件滚动大小,需开启UseGCLogFileRotation -XX:+PrintGCDetails 开启记录GC日志详细信息(包括GC类型、各个操作使用的时间),并且在程序运行结束打印出JVM的内存占用情况 -XX:+ PrintGCDateStamps 记录系统的GC时间 -XX:+PrintGCCause 产生GC的原因(默认开启)
项目中没用过怎么办?
JVM
参数设置小点,使用测试工具JMeter
,多线程测试一下。
The above is the detailed content of Meituan interview: What JVM tuning parameters are familiar with? Fortunately, I have prepared!. For more information, please follow other related articles on the PHP Chinese website!