Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Why should the Xms and Xmx parameter settings of the JVM be set to the same value?


May 31, 2021 Article blog


Table of contents


The article is reproduced from the public number: Program New Horizons

Recently relearning the memory structure of the JVM and related optimizations, I overheard the following configuration in idea's VM configuration (the default configuration at installation):

# custom IntelliJ IDEA VM options


-Xms2048m
-Xmx2048m

Isn't it a little strange to see that Xms and Xmx have the same parameter settings? W rite an article here to analyze the benefits of setting the Xms and Xmx parameters of the JVM to the same value? Let's first look at the concepts and functions of the relevant parameters.

Xms and Xmx parameters are defined

When we start a Java application, we can usually configure the heap information for the JVM with Xms and Xmx Although there are default values that are not configured, if the JVM is limited by hardware or needs to be tuned, the values of these two parameters need to be specified as the case may be.

-Xms: The minimum Heap value for heap memory, which defaults to 1/64 of physical memory, but less than 1G. By default, when the spare heap memory is greater than the specified threshold, the JVM reduces the size of the heap to the size specified by -Xms

-Xmx: The maximum Heap value for heap memory, which defaults to 1/4 of physical memory. By default, when the spare heap memory is less than the specified threshold, the JVM increases the size specified by Heap to -Xmx

Changes in memory conditions

The general JVM parameters are used as follows:

java -Xms512m -Xmx1g

In this configuration, the JVM starts with 512M of heap memory space, and as the program executes, the required heap space increases, gradually increasing the heap memory space until Xmx parameter specifies the maximum heap space of 1G.

When heap memory usage decreases, the size of the memory area decreases gradually. The whole process may seem reasonable, but why do many production environments configure two values to be the same value?

Insufficient JVM garbage collection

When heap memory usage changes, it's not just about expanding and shrinking the heap memory. G C (garbage collection) operations are also performed prior to this. I f the initial value setting of -Xms is relatively small, GC operations are frequently triggered. Memory expansion occurs when GC operation cannot free up more memory.

We all know that GC operations are time-consuming, and Full GC causes "Stop the World," which means that threads stop and inevitably cause performance issues.

Benefits of the same value

Faced with the above issues, to avoid application pauses, reduce latency, and avoid JVM redistribution of memory after each garbage collection is completed in a production environment due to heap memory expansion or shrinkage. Therefore, -Xmx and -Xms are generally equally set.

Of course, if the production system has a warm-up time before it goes live, you can also set the same. F or applications that require high throughput, you can't care about this pause, such as some background apps, then memory can be appropriately scaled up. (The longer the pause, the greater the throughput) and the trade-off depends on the situation.

In fact, it is also officially recommended by Oracle to set Xms and Xmx to the same values in a production environment. There is a passage in the parameter description of Xms

Oracle recommends setting the minimum heap size (-Xms)equal to the maximum heap size (-Xmx) to minimize garbage collections.

In fact, there is a small premise here, that is, the production environment often a server or a container has only one service, exclusive server means that there is no need to resize the JVM, each adjustment will increase the cost. Dynamic adjustment of the JVM is necessary only in more development environments, such as personal computers, where there are more running processes.

Precautions

In fact, while setting it to the same value has many benefits, there are some drawbacks. For example, if two values are the same, the operation of the GC is reduced, which means that it will only be recycled when the JVM is nearing the end of use, and memory will continue to grow.

And there are many GC strategies for the same JDK that cannot be generally general. I n addition, for Hotspot virtual Xms and Xmx are set to the same to reduce the pressure of scaling heap size. For IBM virtual machines, however, setting the same increases the chances of heap fragmentation, and this negative impact is sufficient to offset the benefits of the former.

brief summary

Recent studies of Java virtual machines have been more common, and the more interesting the study, the more studies have found that many previously unexasured problems are slowly melting through. It is highly recommended that you have time to read the relevant books and study the underlying logic of some usage.

The above is W3Cschool编程狮 on JVM Xms and Xmx parameter settings why set to the same value related to the introduction, I hope to help you.