配置JVM参数:
jvm全局配置
- xx: + UseGCOverheadLimit
启用一种策略,该策略限制在抛出OutOfMemoryError异常之前JVM在GC上花费的时间比例。默认情况下,这个选项是启用的,如果花费超过98%的总时间用于垃圾收集,并且恢复不到2%的堆,并行GC将抛出OutOfMemoryError。当堆很小时,该特性可用于防止应用程序长时间运行而很少或没有进展。要禁用此选项,请指定-XX:-UseGCOverheadLimit。
-XX:+UseAdaptiveSizePolicy
Enables the use of adaptive generation sizing. This option is enabled by default. To disable adaptive generation sizing, specify -XX:-UseAdaptiveSizePolicy and set the size of the memory allocation pool explicitly (see the -XX:SurvivorRatio option).
-XX:StringDeduplicationAgeThreshold=threshold
String objects reaching the specified age are considered candidates for deduplication. An object's age is a measure of how many times it has survived garbage collection. This is sometimes referred to as tenuring; see the -XX:+PrintTenuringDistribution option. Note that String objects that are promoted to an old heap region before this age has been reached are always considered candidates for deduplication. The default value for this option is 3. See the -XX:+UseStringDeduplication option.
- xx: + ScavengeBeforeFullGC
在每次全GC之前启用年轻代的GC。该选项默认启用。Oracle建议您不要禁用它,因为在完全GC之前清除年轻代可以减少从老代空间到年轻代空间的可访问对象数量。要在每次全GC之前禁用年轻代的GC,请指定-XX:-ScavengeBeforeFullGC。
-XX:+PrintAdaptiveSizePolicy
Enables printing of information about adaptive generation sizing. By default, this option is disabled.
-XX:MaxRAMPercentage=percent
Sets the maximum amount of memory that the JVM may use for the Java heap before applying ergonomics heuristics as a percentage of the maximum amount determined as described in the -XX:MaxRAM option. The default value is 25 percent.
Specifying this option disables automatic use of compressed oops if the combined result of this and other options influencing the maximum amount of memory is larger than the range of memory addressable by compressed oops. See -XX:UseCompressedOops for further information about compressed oops.
The following example shows how to set the percentage of the maximum amount of memory used for the Java heap:-XX:MaxRAMPercentage=75
-XX:MinRAMPercentage=percent
Sets the maximum amount of memory that the JVM may use for the Java heap before applying ergonomics heuristics as a percentage of the maximum amount determined as described in the -XX:MaxRAM option for small heaps. A small heap is a heap of approximately 125 MB. The default value is 50 percent.
The following example shows how to set the percentage of the maximum amount of memory used for the Java heap for small heaps:
-XX:MinRAMPercentage=75
-XX:MinHeapFreeRatio=percent
Sets the minimum allowed percentage of free heap space (0 to 100) after a GC event. If free heap space falls below this value, then the heap will be expanded. By default, this value is set to 40%.
The following example shows how to set the minimum free heap ratio to 25%:-XX:MinHeapFreeRatio=25
-XX:MaxHeapSize=size
Sets the maximum size (in byes) of the memory allocation pool. This value must be a multiple of 1024 and greater than 2 MB. Append the letter k or K to indicate kilobytes, m or M to indicate megabytes, g or G to indicate gigabytes. The default value is chosen at runtime based on system configuration. For server deployments, -XX:InitialHeapSize and -XX:MaxHeapSize are often set to the same value. See the section "Ergonomics" in Java SE HotSpot Virtual Machine Garbage Collection Tuning Guide at http://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/index.html.
The following examples show how to set the maximum allowed size of allocated memory to 80 MB using various units:
-XX:MaxHeapSize=83886080 -XX:MaxHeapSize=81920k -XX:MaxHeapSize=80m
The -XX:MaxHeapSize option is equivalent to -Xmx.
-XX:MaxHeapFreeRatio=percent
Sets the maximum allowed percentage of free heap space (0 to 100) after a GC event. If free heap space expands above this value, then the heap will be shrunk. By default, this value is set to 70%.
The following example shows how to set the maximum free heap ratio to 75%:
-XX:MaxHeapFreeRatio=75
-XX:InitiatingHeapOccupancyPercent=percent
Sets the percentage of the heap occupancy (0 to 100) at which to start a concurrent GC cycle. It is used by garbage collectors that trigger a concurrent GC cycle based on the occupancy of the entire heap, not just one of the generations (for example, the G1 garbage collector).
By default, the initiating value is set to 45%. A value of 0 implies nonstop GC cycles. The following example shows how to set the initiating heap occupancy to 75%: -XX:InitiatingHeapOccupancyPercent=75
-XX:MaxGCPauseMillis=time
Sets a target for the maximum GC pause time (in milliseconds). This is a soft goal, and the JVM will make its best effort to achieve it. By default, there is no maximum pause time value.
The following example shows how to set the maximum target pause time to 500 ms: -XX:MaxGCPauseMillis=500
-XX:+ExplicitGCInvokesConcurrent
Enables invoking of concurrent GC by using the System.gc() request. This option is disabled by default and can be enabled only together with the -XX:+UseConcMarkSweepGC option.
- xx: + AggressiveHeap
启用Java堆优化。这将根据计算机(RAM和CPU)的配置,将各种参数设置为长时间运行的作业的最佳内存分配。默认情况下,该选项是禁用的,并且堆没有优化。
-XX:+PrintCommandLineFlags
Enables printing of ergonomically selected JVM flags that appeared on the command line. It can be useful to know the ergonomic values set by the JVM, such as the heap space size and the selected garbage collector. By default, this option is disabled and flags are not printed.
-XX:+PrintNMTStatistics
Enables printing of collected native memory tracking data at JVM exit when native memory tracking is enabled (see -XX:NativeMemoryTracking). By default, this option is disabled and native memory tracking data is not printed.
-XX:-UseCompressedOops
Disables the use of compressed pointers. By default, this option is enabled, and compressed pointers are used when Java heap sizes are less than 32 GB. When this option is enabled, object references are represented as 32-bit offsets instead of 64-bit pointers, which typically increases performance when running the application with Java heap sizes less than 32 GB. This option works only for 64-bit JVMs.
It is also possible to use compressed pointers when Java heap sizes are greater than 32GB. See the -XX:ObjectAlignmentInBytes option.
-XX:-OmitStackTraceInFastThrow jvm对异常优化,如果是统一个异常连续抛出则只有第一次有栈信息
-XX:+PrintFlagsInitial查看初始值
-XX:+PrintFlagsFinal查看最终的值,因为初始值有可能被修改
-XX:+UnlockExperimentalVMOptions解锁实验参数,JVM中有些实验参数无法直接赋值,必须先使用此参数才能赋值
-XX:+UnlockDiagnosticVMOptions解锁诊断参数
PrintNMTStatistics必须配合参数-XX:+UnlockDiagnosticVMOptions使用,并且只能加在其后才能生效
你可以通过-XX:+PrintNMTStatistics开启,或者-XX:-PrintNMTStatistics关闭
-XX:+PrintCommandLineFlags打印命令行参数
-XX:+DisableExplicitGC 禁止显示的GC
-XX:+UseStringCache 默认开启,启动缓存常用的字符串
-XX:+AlwaysPreTouch 减少新生代晋升到老年代时停顿。JDK官方文档关于 AlwaysPreTouch 的解释是:
Pre-touch the Java heap during JVM initialization. Every page of the heap is thus demand-zeroed during initialization rather than incrementally during application execution.
也就是说:在启动时就把参数里说好了的内存全部都分配了,会使得启动慢上一点,但后面访问时会更流畅,比如页面会连续分配,比如不会在晋升新生代到老生代时才去访问页面,导致GC停顿时间加长。
既然提到了这个参数,我想说一下Linux内存分配、Linux OOM Killer、内存交换vm.swappiness参数 之间的一些理解:当ES进程向操作系统MMU申请分配内存时,操作系统内核分配的是虚拟内存,
比如指定 -Xms=16G 那么只是告诉内核这个ES进程在启动的时候最需要16G内存,但是ES进程在启动后并不是立即就用了16G的内存。
因此,随着ES的运行,ES进程访问虚拟内存时产生缺页错误(page fault),然后内核为之分配实际的物理页面(这个过程也是需要开销的)。
而如果在JVM启动时指定了AlwaysPreTouch,就会分配实际的物理内存,这样在发生YGC的时候,新生代对象晋升到老年代,减少老年代空间分配产生的缺页异常,从而减少YGC停顿时间。
正是由于Linux内存管理机制,应用程序进程可以申请比物理内存大得多的内存,而进程而言,它看到的是逻辑地址空间,因为通过内存交换(vm.swapiness),操作系统允许将那些长期不用的物理页面交换到磁盘上去,
因此程序能够申请的内存空间范围是很大的。由于进程可以申请到一块很大的地址空间,但不一定把这些空间都使用了。但万一进程真的实实在在地占用了这么多空间,怎么办?于是OOM Killer 就出马了,OOM Killer 会选择一个进程将之杀死。
-XX:MinHeapFreeRatio = 0
空闲堆空间的最小百分比,计算公式为:HeapFreeRatio =(CurrentFreeHeapSize/CurrentTotalHeapSize) * 100,值的区间为0到100,默认值为 40。如果HeapFreeRatio < MinHeapFreeRatio,则需要进行堆扩容,扩容的时机应该在每次垃圾回收之后。
-XX:MaxHeapFreeRatio = 100
空闲堆空间的最大百分比,计算公式为:HeapFreeRatio =(CurrentFreeHeapSize/CurrentTotalHeapSize) * 100,值的区间为0到100,默认值为 70。如果HeapFreeRatio > MaxHeapFreeRatio,则需要进行堆缩容,缩容的时机应该在每次垃圾回收之后。
By default, the JVM grows or shrinks the heap at each GC to try to keep the proportion of free space to the living objects at each collection within a specific range. This range is set as a percentage
-XX:+UseLargePages
-XX:LargePageSizeInBytes=10m。
Enables the use of large page memory. By default, this option is disabled and large page memory is not used.
-XX:-UseLargePages
On Solaris, sets the maximum size (in bytes) for large pages used for Java heap. The size argument must be a power of 2 (2, 4, 8, 16, ...). Append the letter k or K to indicate kilobytes, m or M to indicate megabytes, g or G to indicate gigabytes. By default, the size is set to 0, meaning that the JVM chooses the size for large pages automatically.
The following example illustrates how to set the large page size to 4 megabytes (MB):
MMU (Memory Management Unit,内存管理单元)的核心思想是利用虚拟地址替代物理地址,即CPU寻址时使用虚址,由MMU负责将虚址映射为物理地址。
调整OS和JVM内存分页
在Linux确认是否支持,请在终端敲如下命令:
$cat /proc/meminfo | grep Huge
HugePages_Total: 0
HugePages_Free: 0
Hugepagesize: 2048 kB
如果有HugePage字样的输出内容,说明OS是支持大内存分页的。Hugepagesize就是默认的大内存页size。接下来,为了让JVM可以调整大内存页size,需要设置下OS 共享内存段最大值和大内存页数量。
共享内存段最大值,建议这个值大于Java Heap size,这个例子里设置了4G内存。
$ echo 4294967295 > /proc/sys/kernel/shmmax
大内存页数量, 这个值一般是 Java进程占用最大内存/单个页的大小,比如java设置1.5G,单个页10M,那么数量为1536/10 = 154。
$ echo 154 > /proc/sys/vm/nr_hugepages
单个页大小调整,JVM启用时加参数 -XX:LargePageSizeInBytes=10m。如果JDK是在1.5 update5以前的,还需要手动加 -XX:+UseLargePages,作用是启用大内存页支持。
JVM 堆空间允许的最大值。
-XX:MaxHeapSize = 268435456 (256.0MB)
-Xms9216m jvm启动时堆内存的初始大小
-Xmx9216m 堆内存的最大值
栈配置
-Xss1M 配置为1M。线程占用栈内存,默认每条线程为1M。从这个参数可看出一个进程下可创建的线程数量是有限制的,可视为创建线程的开销。
新生代配置参数
- xx: + UseNUMA
通过增加应用程序对低延迟内存的使用,在具有非统一内存体系结构(NUMA)的机器上实现应用程序的性能优化。默认情况下,该选项是禁用的,并且不会对NUMA进行优化。该选项仅在使用并行垃圾收集器时可用(-XX:+UseParallelGC)。
-XX:TargetSurvivorRatio=percent
Sets the desired percentage of survivor space (0 to 100) used after young garbage collection. By default, this option is set to 50%.
The following example shows how to set the target survivor space ratio to 30%:
-XX:TargetSurvivorRatio=30
-XX:MaxNewSize=size
Sets the maximum size (in bytes) of the heap for the young generation (nursery). The default value is set ergonomically.
-Xmn1000m 新生代空间大小,剩下的为老年代空间大小
-XX:NewSize=4096m JVM 新生代堆空间的默认值。
-XX:MaxNewSize = 89128960 (85.0MB) JVM 新生代堆空间的最大值。
-XX:NewRatio= 2 老年代和新生代的内存比例 -XX:NewSize和-XX:MaxNewSize 优先级比XX:NewRatio=2高,覆盖了这个参数配置的值
Sets the ratio between young and old generation sizes. By default, this option is set to 2. The following example shows how to set the young/old ratio to 1:-XX:NewRatio=1
-XX:SurvivorRatio= 8 survivor区域eden伊甸园区的比值
-XX:MaxTenuringThreshold=threshold
Sets the maximum tenuring threshold for use in adaptive GC sizing. The largest value is 15. The default value is 15 for the parallel (throughput) collector, and 6 for the CMS collector.
The following example shows how to set the maximum tenuring threshold to 10: -XX:MaxTenuringThreshold=10
老年代配置参数
-XX:PretenureSizeThreshold=15 JVM里有个参数可以设置对象的大小超过在指定的大小之后,直接晋升老年代。 使用PretenureSizeThreshold可以进行指定进入老年代的对象大小,但是要注意TLAB区域优先分配空间。虚拟机对于体积不大的对象 会优先把数据分配到TLAB区域中,因此就失去了在老年代分配的机会.
-XX:PretenureSizeThreshold=1000 -XX:-UseTLAB
-XX:OldSize = 16777216 (16.0MB) JVM老年代堆空间的默认值。
Metaspace 配置参数
-XX:MetaspaceSize=size
Sets the size of the allocated class metadata space that will trigger a garbage collection the first time it is exceeded. This threshold for a garbage collection is increased or decreased depending on the amount of metadata used. The default size depends on the platform.
-XX:MaxMetaspaceSize = 17592186044415 MB 元空间最大的大小
-XX:PermGen 设置永久代内存的初始化大小,jdk1.8开始废弃永久代
-XX:MaxPermGen 设置永久代的最大值
-XX:MaxMetaspaceSize=size
Sets the maximum amount of native memory that can be allocated for class metadata. By default, the size is not limited. The amount of metadata for an application depends on the application itself, other running applications, and the amount of memory available on the system.
The following example shows how to set the maximum class metadata size to 256 MB: -XX:MaxMetaspaceSize=256m
压缩类空间
-XX:CompressedClassSpaceSize = 1073741824 (1024.0MB)
GC回收器配置
G1垃圾回收
-XX:+UseStringDeduplication
Enables string deduplication. By default, this option is disabled. To use this option, you must enable the garbage-first (G1) garbage collector. See the -XX:+UseG1GC option.
String deduplication reduces the memory footprint of String objects on the Java heap by taking advantage of the fact that many String objects are identical. Instead of each String object pointing to its own character array, identical String objects can point to and share the same character array.
-XX:+UseG1GC 使用G1收集器,进行垃圾收集的时候它会有限收集存活
Enables the use of the garbage-first (G1) garbage collector. It is a server-style garbage collector, targeted for multiprocessor machines with a large amount of RAM. It meets GC pause time goals with high probability, while maintaining good throughput. The G1 collector is recommended for applications requiring large heaps (sizes of around 6 GB or larger) with limited GC latency requirements (stable and predictable pause time below 0.5 seconds).
By default, this option is disabled and the collector is chosen automatically based on the configuration of the machine and type of the JVM.
-XX:G1HeapRegionSize=size
Sets the size of the regions into which the Java heap is subdivided when using the garbage-first (G1) collector. The value can be between 1 MB and 32 MB. The default region size is determined ergonomically based on the heap size.
The following example shows how to set the size of the subdivisions to 16 MB:-XX:G1HeapRegionSize=16m
-XX:+G1PrintHeapRegions
Enables the printing of information about which regions are allocated and which are reclaimed by the G1 collector. By default, this option is disabled.
-XX:G1ReservePercent=percent
Sets the percentage of the heap (0 to 50) that is reserved as a false ceiling to reduce the possibility of promotion failure for the G1 collector. By default, this option is set to 10%.
The following example shows how to set the reserved heap to 20%: -XX:G1ReservePercent=20
-XX:InitialHeapSize=size
Sets the initial size (in bytes) of the memory allocation pool. This value must be either 0, or a multiple of 1024 and greater than 1 MB. Append the letter k or K to indicate kilobytes, m or M to indicate megabytes, g or G to indicate gigabytes. The default value is chosen at runtime based on system configuration. See the section "Ergonomics" in Java SE HotSpot Virtual Machine Garbage Collection Tuning Guide at http://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/index.html.
The following examples show how to set the size of allocated memory to 6 MB using various units:
-XX:InitialHeapSize=6291456
-XX:InitialHeapSize=6144k
-XX:InitialHeapSize=6m
If you set this option to 0, then the initial size will be set as the sum of the sizes allocated for the old generation and the young generation. The size of the heap for the young generation can be set using the -XX:NewSize option.
Note that the -Xms option sets both the minimum and the initial heap size of the heap. If -Xms appears after -XX:InitialHeapSize on the command line, then the initial heap size gets set to the value specified with -Xms.
-XX:InitiatingHeapOccupancyPercent=percent
Sets the percentage of the heap occupancy (0 to 100) at which to start a concurrent GC cycle. It is used by garbage collectors that trigger a concurrent GC cycle based on the occupancy of the entire heap, not just one of the generations (for example, the G1 garbage collector).
By default, the initiating value is set to 45%. A value of 0 implies nonstop GC cycles. The following example shows how to set the initiating heap occupancy to 75%: -XX:InitiatingHeapOccupancyPercent=75
新生代
-XX:+ParallelRefProcEnabled
Enables parallel reference processing. By default, this option is disabled.
-XX:+UseSerialGC 使用Serial收集器串行收集
Enables the use of the serial garbage collector. This is generally the best choice for small and simple applications that do not require any special functionality from garbage collection. By default, this option is disabled and the collector is chosen automatically based on the configuration of the machine and type of the JVM.
-XX:+UseParNewGC 新生代使用ParNew收集器 并行
Enables the use of parallel threads for collection in the young generation. By default, this option is disabled. It is automatically enabled when you set the -XX:+UseConcMarkSweepGC option. Using the -XX:+UseParNewGC option without the -XX:+UseConcMarkSweepGC option was deprecated in JDK 8.
-XX:+UseParallelGC 使用Parallel Scavenge收集器,关注系统的吞吐量
Enables the use of the parallel scavenge garbage collector (also known as the throughput collector) to improve the performance of your application by leveraging multiple processors.
By default, this option is disabled and the collector is chosen automatically based on the configuration of the machine and type of the JVM. If it is enabled, then the -XX:+UseParallelOldGC option is automatically enabled, unless you explicitly disable it.
-XX:ParallelGCThreads=threads
Sets the number of threads used for parallel garbage collection in the young and old generations. The default value depends on the number of CPUs available to the JVM.
For example, to set the number of threads for parallel GC to 2, specify the following option:-XX:ParallelGCThreads=2
-XX:+PrintTenuringDistribution
Enables printing of tenuring age information. The following is an example of the output:
Desired survivor size 48286924 bytes, new threshold 10 (max 10) - age 1: 28992024 bytes, 28992024 total - age 2: 1366864 bytes, 30358888 total - age 3: 1425912 bytes, 31784800 total ...
Age 1 objects are the youngest survivors (they were created after the previous scavenge, survived the latest scavenge, and moved from eden to survivor space). Age 2 objects have survived two scavenges (during the second scavenge they were copied from one survivor space to the next). And so on.
In the preceding example, 28 992 024 bytes survived one scavenge and were copied from eden to survivor space, 1 366 864 bytes are occupied by age 2 objects, etc. The third value in each row is the cumulative size of objects of age n or less.
By default, this option is disabled.
-XX:InitialSurvivorRatio=ratio
Sets the initial survivor space ratio used by the throughput garbage collector (which is enabled by the -XX:+UseParallelGC and/or -XX:+UseParallelOldGC options). Adaptive sizing is enabled by default with the throughput garbage collector by using the -XX:+UseParallelGC and -XX:+UseParallelOldGC options, and survivor space is resized according to the application behavior, starting with the initial value. If adaptive sizing is disabled (using the -XX:-UseAdaptiveSizePolicy option), then the -XX:SurvivorRatio option should be used to set the size of the survivor space for the entire execution of the application.
The following formula can be used to calculate the initial size of survivor space (S) based on the size of the young generation (Y), and the initial survivor space ratio (R):
S=Y/(R+2)
The 2 in the equation denotes two survivor spaces. The larger the value specified as the initial survivor space ratio, the smaller the initial survivor space size.
By default, the initial survivor space ratio is set to 8. If the default value for the young generation space size is used (2 MB), the initial size of the survivor space will be 0.2 MB.
The following example shows how to set the initial survivor space ratio to 4:
-XX:InitialSurvivorRatio=4
老年代GC
-XX:+UseParallelOldGC 使用Parallel Scavenge收集器作用在老年代
Enables the use of the parallel garbage collector for full GCs. By default, this option is disabled. Enabling it automatically enables the -XX:+UseParallelGC option.
-XX:+UseConcMarkSweepGC 老年代使用CMS收集器 并行并发
-XX:CMSMaxAbortablePrecleanTime=5000
-XX:+ UseCMSCompactAtFullCollection Full GC后,进行一次碎片整理;整理过程是独占的,会引起停顿时间变长
-XX: + CMSClassUnloadingEnabled
在使用并发标记-清除(CMS)垃圾收集器时启用类卸载。该选项默认启用。要禁用CMS垃圾收集器的类卸载,请指定-XX:-CMSClassUnloadingEnabled。
-XX:+CMSFullGCsBeforeCompaction 设置进行几次Full GC后,进行一次碎片整理
CMSFullGCsBeforeCompaction 说的是,在上一次CMS并发GC执行过后,到底还要再执行多少次full GC才会做压缩。默认是0
-XX:+ParallelCMSThreads 设定CMS的线程数量
-XX:+UseCMSInitiatingOccupancyOnly 只有达到阈值后才进行CMS回收
-XX:CMSInitiatingOccupancyFraction=74 CMS垃圾收集器,当老年代达到多少百分比时候,触发CMS垃圾回收 设置成75%。主要是因为CMS是并发收集,垃圾回收线程和用户线程同时运行,用户线程运行时可能继续无用的垃圾对象,如果到90%再去回收就太晚了。老年代使用到75%就回收可减少OOM异常发生的概率。
设置启动CMS收集周期的老一代占用率(0到100)。默认值为-1。任何负值(包括默认值)都意味着-XX:CMSTriggerRatio用于定义初始占用率的值。
示例如下:设置占用率为20%。
- xx: CMSInitiatingOccupancyFraction = 20
-XX:+CMSScavengeBeforeRemark
Enables scavenging attempts before the CMS remark step. By default, this option is disabled.
- xx: CMSTriggerRatio = %
设置CMS收集周期开始前分配的-XX:MinHeapFreeRatio指定值的百分比(0到100)。缺省值为80%。
以下示例显示如何将占用率设置为75%: - xx: CMSTriggerRatio = 75
-XX:ConcGCThreads=threads
Sets the number of threads used for concurrent GC. The default value depends on the number of CPUs available to the JVM.
For example, to set the number of threads for concurrent GC to 2, specify the following option:
-XX:ConcGCThreads=2
- xx: + ExplicitGCInvokesConcurrentAndUnloadsClasses
通过使用System.gc()请求和在并发GC周期中卸载类来启用并发GC的调用。该选项默认是禁用的,只能与-XX:+UseConcMarkSweepGC选项一起启用。
-XX:CMSExpAvgFactor=percent
Sets the percentage of time (0 to 100) used to weight the current sample when computing exponential averages for the concurrent collection statistics. By default, the exponential averages factor is set to 25%. The following example shows how to set the factor to 15%:
-XX:CMSExpAvgFactor=15
GC日志配置
-XX:+PrintGC
-verbose:gc 输出GC详情
-XX:+PrintGCDetails
-XX:+PrintGCDateStamps
-XX:+PrintGCTimeStamps
-XX:+UseGCLogFileRotation
-XX:NumberOfGCLogFiles=10
-XX:GCLogFileSize=100M
-Xloggc:/home/admin/nacos/logs/nacos_gc.log
-XX:+PrintGCApplicationConcurrentTime
Enables printing of how much time elapsed since the last pause (for example, a GC pause). By default, this option is disabled.
-XX:+PrintGCApplicationStoppedTime
Enables printing of how much time the pause (for example, a GC pause) lasted. By default, this option is disabled.
-XX:+PrintGCDateStamps
Enables printing of a date stamp at every GC. By default, this option is disabled.
-XX:+PrintGCDetails
Enables printing of detailed messages at every GC. By default, this option is disabled.
-XX:+PrintGCTaskTimeStamps
Enables printing of time stamps for every individual GC worker thread task. By default, this option is disabled.
-XX:+PrintGCTimeStamps
Enables printing of time stamps at every GC. By default, this option is disabled.
堆dump配置
-XX:+HeapDumpOnOutOfMemoryError 抛出OOM时进行HeapDump
-XX:HepDumpPath=/home/admin/Demo3.dump 指定HeapDump的文件路径或目录
在Java程序的运行过程中,如果对空间不足,则会抛出内存溢出的错误(Out Of Memory)OOM,一旦这类问题发生在生产环境,则可能引起严重的业务中断,Java虚拟机提供了-XX:+HeapDumpOnOutOfMemoryError,
使用该参数可以在内存溢出时导出整个堆信息,与之配合使用的还有参数-XX:HeapDumpPath,可以设置导出堆的存放路径。
内存分析工具:Memory Analyzer
直接内存
-XX:MaxDirectMemorySize=4096m ,如果不设置,默认值为最大堆空间,即-Xmx。直接内存使用达到上限时,就会触发垃圾回收,如果不能有效的释放空间,就会引起系统的OOM。
Sets the maximum total size (in bytes) of the New I/O (the java.nio package) direct-buffer allocations. Append the letter k or K to indicate kilobytes, m or M to indicate megabytes, g or G to indicate gigabytes. By default, the size is set to 0, meaning that the JVM chooses the size for NIO direct-buffer allocations automatically.
The following examples illustrate how to set the NIO size to 1024 KB in different units:
TLAB参数配置
TLAB全称是Thread Local Allocation Buffer即线程本地分配缓存,从名字上看是一个线程专用的内存分配区域,是为了加速对象分配对象而生的。
每一个线程都会产生一个TLAB,该线程独享的工作区域,Java虚拟机使用这种TLAB区来避免多线程冲突问题,提高了对象分配的效率。
TLAB空间一般不会太大,当大对象无法在TLAB分配时,则会直接分配到堆上。
-XX:+UseTLAB使用TLAB
-XX:+TLABSize设置TLAB大小
-XX:TLABRefillWasteFraction设置维护进入TLAB空间的单个对象大小,它是一个比例值,默认为64,即如果对象大于整个空间的1/64,则在堆创建对象。
-XX:+PrintTLAB查看TLAB信息
-XX:ResizeTLAB自调整TLABRefillWasteFraction阈值。
参数:-XX:+UseTLAB -XX:+PrintTLAB -XX:+PrintGC -XX:TLABSize=102400 -XX:-ResizeTLAB -XX:TLABRefillWasteFraction=100 -XX:-DoEscapeAnalysis -server
-XX:-DoEscapeAnalysis
进行逃逸分析之后,产生的后果是所有的对象都将由栈上分配,而非从JVM内存模型中的堆来分配。
逃逸分析/栈上分配的优劣分析
优势表现在以下两个方面:
消除同步。线程同步的代价是相当高的,同步的后果是降低并发性和性能。逃逸分析可以判断出某个对象是否始终只被一个线程访问,如果只被一个线程访问,那么对该对象的同步操作就可以转化成没有同步保护的操作,这样就能大大提高并发程度和性能。
矢量替代。逃逸分析方法如果发现对象的内存存储结构不需要连续进行的话,就可以将对象的部分甚至全部都保存在CPU寄存器内,这样能大大提高访问速度。
劣势: 栈上分配受限于栈的空间大小,一般自我迭代类的需求以及大的对象空间需求操作,将导致栈的内存溢出;故只适用于一定范围之内的内存范围请求。
栈上分配可以提升代码性能,降低在多线程情况下的锁使用,但是会受限于其空间的大小。
Linux内存可分成2种类型:Page cache和应用程序内存。
应用程序内存会被Linux的swap机制交换出去,而page cache则是由Linux后台的异步flush策略刷盘。当OS内存满了时,就需要把一部分内存数据写入磁盘,因此就需要决定是swap应用程序内存呢?还是清理page cache?
OS有个系统参数/proc/sys/vm/swappiness默认值60,一般将之设置成0,表示优先刷新page cache而不是将应用程序的内存交换出去。
Linux总会把系统中还没被应用使用的内存挪来给Page Cache,在命令行输入free,或者cat /proc/meminfo,"Cached"的部分就是Page Cache。
当Linux系统内存不足时,要么就回收page cache,要么就内存交换(swap),而内存交换就有可能把应用程序的数据换出到磁盘上去了,这就有可能造成应用的长时间停顿了。
Linux有个很怪的癖好,当内存不足时,有很大机率不是把用作IO缓存的Page Cache收回,而是把冷的应用内存page out到磁盘上。当这段内存重新要被访问时,再把它重新page in回内存(所谓的主缺页错误),
这个过程进程是停顿的。增长缓慢的老生代,池化的堆外内存,都可能被认为是冷内存,用 cat /proc/[pid]/status 看看 VmSwap的大小, 再dstat里看看监控page in发生的时间。
随着系统的运行,JVM堆内存会被使用,引用不可达对象就是垃圾对象,而操作系统有可能会把这些垃圾对象(长期未用的物理页面)交换到磁盘上去。当JVM堆使用到一定程度时,
触发FullGC,FullGC过程中发现这些未引用的对象都被交换到磁盘上去了,于是JVM垃圾回收进程得把它们重新读回来到内存中,然而戏剧性的是:这些未引用的对象本身就是垃圾,读到内存中的目的是回收被丢弃它们!而这种来回读取磁盘的操作导致了FullGC 消耗了大量时间,(有可能)发生长时间的stop the world。因此,需要禁用内存交换以避免这种现象,这也是为什么在部署Kafka和ES的机器上都应该要禁用内存交换的原因吧(如果因长时间STW导致node与master之间的"心跳包"失效,集群的master节点认为该节点发生了故障,于是是自动进行failover,对于ES来说可能就发生自动rebalance分片迁移)
















