Meituan interview: What JVM tuning parameters are familiar with? Fortunately, I have prepared!

Release: 2023-08-24 15:25:56
forward
1074 people have browsed it


Meituan interview: What JVM tuning parameters are familiar with? Fortunately, I have prepared!
img

Let’s get familiar with it today, about JVMCommon 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:[+-]<name> 表示启用或者禁用name属性。
例子:-XX:+UseG1GC(表示启用G1垃圾收集器)
Copy after login
Meituan interview: What JVM tuning parameters are familiar with? Fortunately, I have prepared!
-XX: PrintCommandLineFlags

View the relevant parameters currently set by JVM:

Meituan interview: What JVM tuning parameters are familiar with? Fortunately, I have prepared!

JVM parameter classification

The parameter type can be distinguished according to the beginning of the JVM 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......

Heap Parameter settings

-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 and Xmx of JVM should be set to the same size to avoid GCUnnecessary 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 is 1: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, means Eden: Survivor=3:2, one Survivor area accounts for 1/5 of the entire young generation.

Metaspace parameters

-XX:MetaspaceSizeMetaspace The initial size of the space, if If not set, the default is 20.79M. This initial size is the threshold that triggers the first Metaspace Full GC.

For example: -XX:MetaspaceSize=256M

-XX:MaxMetaspaceSizeMetaspace 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 when Metaspace occurs After GC, the free ratio of Metaspace will be calculated. If the free ratio (free space/current Metaspace size) is less than this value, Metaspace expansion will be triggered. The default value is 40, which is 40%, for example -XX:MinMetaspaceFreeRatio=40

##-XX:MaxMetaspaceFreeRatio

: Maximum idle ratio, when GC occurs in Metaspace, The free ratio of 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<blockquote data-tool="mdnice编辑器" style="border-top: none;border-right: none;border-bottom: none;font-size: 0.9em;overflow: auto;color: rgb(106, 115, 125);padding: 10px 10px 10px 20px;margin-bottom: 20px;margin-top: 20px;border-left-color: rgb(239, 112, 96);background: rgb(255, 249, 249);"><p style="font-size: 16px;padding-top: 8px;padding-bottom: 8px;color: black;line-height: 26px;">It is recommended to set <code style="font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(239, 112, 96);">MetaspaceSize and MaxMetaspaceSize to the same size to avoid frequent expansion.

Stack parameter setting

-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: -Xss256K, if this parameter is not set, the default value is 1M. Generally speaking, setting it to 256K is enough.

Collector parameter settings

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

##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 //

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

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
Copy after login

-XX:OnOutOfMemoryError

表示发生OOM后,运行jconsole.exe程序。

这里可以不用加“”,因为jconsole.exe路径Program Files含有空格。利用这个参数,我们可以在系统OOM后,自定义一个脚本,可以用来发送邮件告警信息,可以用来重启系统等等。

-XX:OnOutOfMemoryError="C:\Program Files\Java\jdk1.8.0_151\bin\jconsole.exe"
Copy after login

8G内存的服务器该如何设置

java -Xmx3550m -Xms3550m -Xss128k -XX:NewRatio=4 -XX:SurvivorRatio=4 -XX:MaxPermSize=16m -XX:MaxTenuringThreshold=0
Copy after login

-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区的大小比值。

设置为4,则两个Survivor区与一个Eden区的比值为2:4,一个Survivor区占整个年轻代的1/6 -XX:MaxPermSize=16m 设置持久代大小为16m。

-XX:MaxTenuringThreshold=0 设置垃圾最大年龄。

如果设置为0的话,则年轻代对象不经过Survivor区,直接进入年老代。对于年老代比较多的应用,可以提高效率。如果将此值设置为一个较大值,则年轻代对象会在Survivor区进行多次复制,这样可以增加对象在年轻代的存活时间,增加在年轻代即被回收的概论。

项目中,GC日志配置

比如,我们启动一个user-service项目:

 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
Copy after login

参数解释:

 -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的原因(默认开启)
Copy after login

项目中没用过怎么办?

对于很多没用过的人来说,面试官问项目中这些参数是怎么用?此时,很容易选择妥协,傻傻的回答没用过

偷偷的告诉你,很多面试官也没有用过。

另外,你可以自己搞个小项目,把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!

Related labels:
source:Java后端技术全栈
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template