Getting Started with JVM Parameters
JVM memory parameters, but in practice the main focus on the size of the heap memory and the size of the heap memory of the new generation and the old generation of the size of the heap memory, the following look at a simple JVM startup parameter settings case:
java -server
-Xms3g -Xmx3g
-XX:NewSize=1g
-XX:MetaspaceSize=128m
-XX:NewRatio=3
-XX:SurvivorRatio=8
-XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath= -jar
-Xms -Xmx: -Xms means initial heap size, -Xmx means maximum heap size. Generally, Xms and Xmx are set to the same value to avoid memory oscillation caused by JVM reallocating the heap size after garbage collection, which may affect the performance. The size of the heap memory can be simply understood as the total memory available to the JVM during operation.
-XX:NewSize: -XX:NewSize=1g means to set the size of the new generation to 1GB, it is generally recommended to set it to 1/3 of the total heap memory
-XX:MetaspaceSize: indicates that the size of the metaspace is 128MB, when there are too many libraries to load, you can appropriately raise this value
-XX:NewRatio: -XX:NewRatio=3 means to set the ratio of the new generation to the old generation to 1:3, so the new generation takes up 1/4 of the entire stack and the old generation takes up 3/4 of the entire heap memory
-XX:SurvivorRatio: -XX:SurvivorRatio=8 means that the ratio of Eden zone and two Survivor zones is 8:1, i.e., Eden:SurvivorTo=8:1, Eden:SurvivorFrom=8:1. The final result is Eden:SurvivorTo. SurvivorFrom=8:1:1
-XX:+UseParNewGC XX:+UseConcMarkSweepGC: Garbage Collector Setting -XX:+UseParNewGC means to set the young generation garbage collector to ParNew garbage collector. -XX:+UseConcMarkSweepGC means set old generation garbage collector to CMS garbage collector.
-OOM Exception Diagnostic Settings: XX:HeapDumpOnOutOfMemoryError indicates that the heap is dumped to a file when an OOM occurs. xx:HeapDumpPath indicates the address of the heap's dump file path. The combination of these two parameters can print out the heap information in time when OOM occurs in the program, which is convenient for subsequent analysis of the failure.
JVM Parameter Setting in Action
When setting JVM parameters, you need to focus on the garbage collector settings and JVM memory settings. Let's take the example of running a Netty application service named Netty on an 8GB server to introduce the process of memory settings.
- Reserve operating system memory: first determine the total memory of the operating system is 8GB, reserve 2GB of memory for the operating system to ensure smooth operation of the operating system, and allocate the remaining 6GB of memory to the application programs.
- Determining Direct Memory: Since our application is a Netty server and Netty services use direct memory to improve performance during operation, the application will have a large amount of direct memory usage during operation. In order to ensure that the application has enough direct memory to ensure the efficient operation of the service, and does not consume too much out-of-heap memory, resulting in insufficient system memory and OOM problems, we reserve 2GB (1/3 of the application's available memory) of memory for the direct memory, which can be used by the
-XX:MaxDirectMemorySize-2g
Set the maximum available off-heap memory to 2GB. enough memory will be allocated to direct memory on demand during use, up to a maximum of 2GB - Determine the size of the Java heap: with 4GB of memory remaining, allocate 3GB to the Java heap so that the
-Xm3g -Xmx3g
- Determine the size of the new generation and old generation: Since there are no special large objects or objects with too many long life cycles, you can allocate 1/3 of the heap memory to the new generation, that is -XX:NewSize=1g, and allocate the other remaining 2GB memory to the old generation. Meanwhile, since our program is a general Java program, the configuration of Survivor and Eden zones can adopt the values suggested by the official website, and we don't make any special settings here.
- Determine the metaspace area: the next 1GB of memory remaining is available for the application, and since the application and its dependent JAR packages are not large, it can be used by the
XX:MetaspaceSize=128m
Set the meta space size to 128MB, leaving a small amount of memory for the operating system or other applications. - Configuring GC: Finally, set up the garbage collector, OOM exception data dump path, and GC logs. GC is configured using the
-XX:+UserConcMarkSweepGC
You can set older generations to use the CMS garbage collector and newer generations to use the default ParNew garbage collector. The default ParNew garbage collector is used for new generations using the-XX:+UseG1GC
Can be configured to use the G1 garbage collector
The specific configurations are as follows:
java -server
-XX:MaxDirectMemorySize=2g # Direct memory size is 2GB
-Xms3g -Xmx3g # Java heap memory size is 3GB
-XX:NewSize=1g # New generation size is 1GB
-XX:MetaspaceSize=128m # Metaspace is 128MB
-XX:+UseParNewGC -XX:+UseConcMarkSweepGC # ParNewGC for fresh generation, CMS for old generation
-xx:+HeapDumponCutOfMemoryError # Print logs when OOM occurs
-XX:HeapDumpPath= # OOM log storage address
-XX:+PrintGC # Output GC logs
-XX:+PrintGCDetails # Output GC detail logs
-XX:+PrintGCDatestamps # Output timestamps for GCs
-XX:+PrintHeapAtGC # Print heap information before and after the JVM performs a GC operation.
-Xlogge:... /gc/ # Output GC logs.
-Xlogge:.../gc/ # Output address of GC logs -jar
Also, note that different JVM versions have different configuration parameters, such as-XX:PermSizeXX
respond in singing-XX:MaxPermsize
The initialized size of the permanent generation and the maximum size of the permanent generation, respectively. However, there are no more permanents in Java 8, so this configuration parameter does not exist.