Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the reason for setting the Xms and Xmx parameters to the same value in JVM

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the JVM Xms and Xmx parameters to be set to the same value of what is the relevant knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that you read this JVM Xms and Xmx parameters should be set to the same value of what the article is what the article will have a harvest, let's take a look.

The following configuration is available in IDEA's VM configuration, which is the default 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? Let's write an article to analyze the benefits of setting the Xms and Xmx parameters of JVM to the same value. First of all, let's understand the concept and function of the relevant parameters.

Xms and Xmx parameter definition

When starting a Java application, we can usually configure the heap information of JVM with the parameters Xms and Xmx. No configuration, although there will be default values, but if you are limited by hardware or need to tune JVM, you need to specify the values of these two parameters as appropriate.

-Xms: the minimum heap value of heap memory, which defaults to 1x64 of physical memory, but less than 1G. By default, when the free heap memory is greater than the specified threshold, JVM reduces the size of the heap to the size specified by-Xms.

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

Changes in memory conditions

The general JVM parameters are used as follows:

Java-Xms512m-Xmx1g

In this configuration, JVM will allocate 512m of heap memory space when it starts. With the execution of the program, the heap memory space will gradually increase until the maximum heap space specified by the Xmx parameter is 1G.

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

Deficiency of JVM garbage collection

When heap memory usage changes, it's not just about expanding and shrinking heap memory. Prior to this, a GC (garbage collection) operation is performed. If the initial value of-Xms is set to a small value, then the GC operation is triggered frequently. Memory expansion occurs when the GC operation cannot release more memory.

We all know that GC operations are time-consuming, and Full GC can cause "Stop the World", that is, thread stops, which inevitably leads to performance problems.

The benefits of the same value

In the face of the above problems, in order to avoid application standstill caused by the expansion or reduction of heap memory in the production environment, reduce the latency, and avoid JVM reallocation of memory after each garbage collection. So,-Xmx and-Xms are generally set to be equal.

Of course, if there is a warm-up time before the production system goes online, equality can not be set. For applications that need high throughput, you can ignore this pause, such as some background applications, then the memory can be appropriately increased. (the longer the pause time, the greater the throughput), which needs to be weighed on a case-by-case basis.

In fact, it is also officially recommended by Oracle to set Xms and Xmx to the same value in a production environment. There is a paragraph 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, in a production environment, there is often only one server or container with one service. Monopolizing the server means that there is no need to resize the JVM, and each adjustment will increase the overhead. Dynamic adjustment of JVM is necessary only when there are many running processes in multiple development environments, such as personal computers.

Matters needing attention

In fact, although setting to the same value has many advantages, it also has some disadvantages. For example, if the two values are the same, the operation of the GC will be reduced, which means that the JVM will only be recycled when it is about to be used up, and the memory will continue to grow.

And there are many kinds of GC strategies for the same JDK, which can not be generalized. In addition, for Hotspot virtual machines, Xms and Xmx are set to the same to reduce the pressure caused by scaling the heap size. However, for IBM virtual machines, setting to the same setting increases the chance of heap fragmentation, and this negative effect is sufficient to offset the benefits of the former.

This is the end of the article on why the Xms and Xmx parameters in JVM should be set to the same value. Thank you for reading! I believe you all have a certain understanding of the knowledge of "why the Xms and Xmx parameters in JVM should be set to the same value". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report