JAVA -Xms -Xmx -XX:PermSize -XX:MaxPermSize 区别

java  -Xms -Xmx -XX:PermSize -XX:MaxPermSize


     When doing java development, especially when developing large-scale software, you often encounter memory overflow problems, such as OutOfMemoryError. This is a very painful and tangled problem for developers, because we sometimes don't know what kind of operation caused this problem to happen. So we had to refactor again and again by constantly optimizing our code structure. Although constantly refactoring our own code is a very good thing for both public and private, but sometimes we find that sometimes we can solve such problems not only by refactoring our own code, because it may also be due to The improper allocation of memory during the runtime of the java virtual machine leads to the continuous occurrence of memory overflow. In order to solve this problem, the java development team provides a user-defined way to configure the memory required by the java virtual machine runtime on demand - parameter allocation customization is realized in the form of parameter configuration.

     Before analyzing how to configure JVM runtime memory allocation through parameters, let's explain the pattern of JVM runtime memory.

The JVM allocates the required memory into two parts: the heap area and the non-heap area according to the content of the stored data:
the so-called heap area is the memory space occupied by objects (class instances) created by new; the non-heap area is Code, constants, external access (such as resources occupied by file access streams), etc. However, although Java's garbage collection mechanism can solve the problem of memory waste very well, this mechanism only recycles the resources in the heap area, and is helpless for the resources in the non-heap area. Resolved by the developer's own constraints. Even so (the heap area has a java recycling mechanism, and non-heap area developers can solve it very well), when the memory required for runtime suddenly surges, the JVM has no choice but to suspend the operation of the program. So this article is about how to solve the latter problem.
Well, having said so much, it's time to talk about several parameters for configuring JVM memory and how to use them.
First, common parameter types (configured memory): (-Xms, -Xmx, -XX:newSize, -XX:MaxnewSize, -Xmn), (-XX:PermSize, -XX:MaxPermSize). You can see my intention from the way of enumeration. The configuration of parameters is grouped, the former is used to configure the heap area, and the latter is used to configure the non-heap area. The first set of configuration parameters: -Xms, -Xmx, -XX:newSize, -XX:MaxnewSize, -Xmn
    
    
     1. -Xms: Indicates the size of the initial memory allocation of the Java virtual machine heap memory, which is usually 1/64 of the available memory of the operating system, but it still needs to be allocated according to the actual situation. It is possible that when such a rule is assigned, the designed software hangs before it can run. 2. -Xmx: Indicates the maximum upper limit of the heap memory of the Java virtual machine that can be allocated, which is usually 1/4 of the available memory of the operating system. However, in the development process, the two parameters of -Xms and -Xmx are usually configured with the same value. The purpose is to waste resources without needing to re-calculate the size of the heap area after the Java garbage collection mechanism cleans up the heap area.
    

    Generally speaking, for the memory allocation of the heap area, you only need to configure the above two parameters reasonably. However, if you want to perform more fine-grained allocation, you can further refine the memory of the heap area, then you need to use the following three The parameters are -XX:newSize, -XX:MaxnewSize, -Xmn. Of course, this stems from the further refinement of the heap area: Cenozoic, Mesozoic, and Old. The memory space occupied by each new object in java is the space of the new generation. When the Java garbage collection mechanism reclaims the resources in the heap area, those resources that have not been recovered in the new generation will be transferred to the Mesozoic, and the Mesozoic will be transferred. to the old generation. The next three parameters are used to control the size of the new generation memory.
 
     1. -XX:newSize: indicates the initial memory size of the new generation, which should be smaller than the value of -Xms; 2. -XX:MaxnewSize: indicates the maximum upper limit of the memory that can be allocated in the new generation; of course, this value should be smaller than the value of -Xmx ; 3. -Xmn: As for this parameter, the two parameters -XX:newSize and -XX:MaxnewSize are configured at the same time, that is to say, if the memory size of the new generation is configured by -Xmn, then -XX:newSize = - XX:MaxnewSize = -Xmn, although it will be very convenient, it should be noted that this parameter is only used after the JDK1.4 version.
    
    

     The above are the parameters of the configurable heap area provided by the java virtual machine externally. Next, the two parameters of the non-heap memory configuration of the java virtual machine are described:
    
     1. -XX:PermSize: indicates the initial memory allocation size of the non-heap area, which is abbreviated as permanent size (persistent memory) 2. -XX:MaxPermSize: indicates the maximum upper limit of the memory allocated to the non-heap area.
    
    
     A very important point to note here is that you must carefully consider the size of the non-heap memory required by your own software before configuring, because the memory here will not be processed by the Java garbage collection mechanism. And it should be noted that the sum of the maximum heap memory and the maximum non-heap memory must not exceed the available memory of the operating system.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326328787&siteId=291194637