


Meituan interview: What JVM tuning parameters are familiar with? Fortunately, I have prepared!
Aug 24, 2023 pm 03:25 PM
Let’s get familiar with it today, about JVM
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:[+-]<name> 表示啟用或者禁用name屬性。 例子:-XX:+UseG1GC(表示啟用G1垃圾收集器)

View the relevant parameters currently set by JVM
:

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
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 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: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 first Metaspace 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 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
: 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:MaxGCPauseMillisto 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=nThe 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異??煺?/span>
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath
堆內(nèi)存出現(xiàn)OOM
的概率是所有內(nèi)存耗盡異常中最高的,出錯時的堆內(nèi)信息對解決問題非常有幫助。
所以給JVM
設(shè)置這個參數(shù)(-XX:+HeapDumpOnOutOfMemoryError
),讓JVM
遇到OOM
異常時能輸出堆內(nèi)信息,并通過(-XX:+HeapDumpPath
)參數(shù)設(shè)置堆內(nèi)存溢出快照輸出的文件地址。
這對于特別是對相隔數(shù)月才出現(xiàn)的OOM
異常尤為重要。
-Xms10M -Xmx10M -Xmn2M -XX:SurvivorRatio=8 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:\study\log_hprof\gc.hprof
-XX:OnOutOfMemoryError
表示發(fā)生OOM后
,運(yùn)行jconsole.exe
程序。
這里可以不用加“”,因?yàn)?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);">jconsole.exe路徑Program Files含有空格。利用這個參數(shù),我們可以在系統(tǒng)OOM
后,自定義一個腳本,可以用來發(fā)送郵件告警信息,可以用來重啟系統(tǒng)等等。
-XX:OnOutOfMemoryError="C:\Program Files\Java\jdk1.8.0_151\bin\jconsole.exe"
8G內(nèi)存的服務(wù)器該如何設(shè)置
java -Xmx3550m -Xms3550m -Xss128k -XX:NewRatio=4 -XX:SurvivorRatio=4 -XX:MaxPermSize=16m -XX:MaxTenuringThreshold=0
-Xmx3500m
設(shè)置JVM
最大可用內(nèi)存為3550M。
-Xms3500m
設(shè)置JVM
初始
內(nèi)存為3550m
。此值可以設(shè)置與-Xmx
相同,以避免每次垃圾回收完成后JVM重新分配內(nèi)存。-Xmn2g
設(shè)置年輕代大小為2G
。
整個堆大小=年輕代大小 + 年老代大小 + 方法區(qū)大小
-Xss128k
設(shè)置每個線程的堆棧大小。
JDK1.5
以后每個線程堆棧大小為1M,以前每個線程堆棧大小為256K。更具應(yīng)用的線程所需內(nèi)存大小進(jìn)行調(diào)整。在相同物理內(nèi)存下,減小這個值能生成更多的線程。但是操作系統(tǒng)對一個進(jìn)程內(nèi)的線程數(shù)還是有限制的,不能無限生成,經(jīng)驗(yàn)值在3000~5000左右。
-XX:NewRatio=4
設(shè)置年輕代(包括Eden和兩個Survivor區(qū))與年老代的比值(除去持久代)。設(shè)置為4,則年輕代與年老代所占比值為1:4,年輕代占整個堆棧的1/5 。
-XX:SurvivorRatio=4
設(shè)置年輕代中Eden區(qū)與Survivor區(qū)的大小比值。
設(shè)置為4,則兩個Survivor區(qū)與一個Eden區(qū)的比值為2:4,一個Survivor區(qū)占整個年輕代的1/6 -XX:MaxPermSize=16m
設(shè)置持久代大小為16m。
-XX:MaxTenuringThreshold=0
設(shè)置垃圾最大年齡。
如果設(shè)置為0的話,則年輕代對象不經(jīng)過Survivor區(qū),直接進(jìn)入年老代。對于年老代比較多的應(yīng)用,可以提高效率。如果將此值設(shè)置為一個較大值,則年輕代對象會在Survivor區(qū)進(jìn)行多次復(fù)制,這樣可以增加對象在年輕代的存活時間,增加在年輕代即被回收的概論。
項(xiàng)目中,GC日志配置
比如,我們啟動一個user-service項(xiàng)目:
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
參數(shù)解釋:
-Xloggc:/opt/app/ard-user/user-service-gc-%t.log 設(shè)置日志目錄和日志名稱 -XX:+UseGCLogFileRotation 開啟滾動生成日志 -XX:NumberOfGCLogFiles=5 滾動GC日志文件數(shù),默認(rèn)0,不滾動 -XX:GCLogFileSize=20M GC文件滾動大小,需開啟UseGCLogFileRotation -XX:+PrintGCDetails 開啟記錄GC日志詳細(xì)信息(包括GC類型、各個操作使用的時間),并且在程序運(yùn)行結(jié)束打印出JVM的內(nèi)存占用情況 -XX:+ PrintGCDateStamps 記錄系統(tǒng)的GC時間 -XX:+PrintGCCause 產(chǎn)生GC的原因(默認(rèn)開啟)
項(xiàng)目中沒用過怎么辦?
對于很多沒用過的人來說,面試官問項(xiàng)目中這些參數(shù)是怎么用?此時,很容易選擇妥協(xié),傻傻的回答沒用過。
偷偷的告訴你,很多面試官也沒有用過。
另外,你可以自己搞個小項(xiàng)目,把JVM
參數(shù)設(shè)置小點(diǎn),使用測試工具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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)