In-depth understanding of jvm and jvm basic tuning parameters

Java virtual machine principle

A virtual machine is a virtual machine. It is a software used to execute a series of virtual computing instructions. In general, virtual machines can be divided into: system virtual machines and program virtual machines. The famous Visual Box and Vmare belong to system virtual machines. They are completely for physical computing. Emulation, which provides a software platform that can run a complete operating system.

The typical code of the program virtual machine is the Java virtual machine, which is specially calculated for executing a single computing program, and the instructions executed in the Java virtual machine become Java's own code instructions. Whether it is a system virtual machine or a program virtual machine, the software running on it is limited to the resources provided by the virtual machine.

Before understanding jvm, let's take a brief look at the memory structure of java:
insert image description here

insert image description here

For each different part, their basic function description is summarized as follows,

  1. Class loading subsystem: responsible for loading class information from the file system or network, and the loaded information is stored in a memory space called method area;
  2. Method area: It stores class information, constant information, constant pool information, including string literals and numeric constants.
  3. Java heap: The Java heap is established when the Java virtual machine starts. It is the main memory working area of ​​the Java program. Almost all object instances are stored in the Java heap, and the heap space is shared by all threads.
  4. Direct memory: The JavaNio library allows Java programs to direct memory, thereby improving performance, usually direct memory is faster than the Java heap. It may be considered for occasions with frequent reading and writing.
  5. Each virtual machine thread has a private stack. The Java stack of a thread is created when the thread is created. The Java stack saves local variables, method parameters, method calls and return values ​​of colleagues in Java.
  6. The native method stack, the biggest difference is that the native method stack is used for native method calls. The Java Virtual Machine allows Java to directly call native methods (written in C)
  7. The garbage collection system is the core of Java, and it is also indispensable. Java has its own garbage cleaning mechanism, and developers do not need to clean it manually.
  8. The PC (Program Couneter) register is also a private space for each thread. The Java virtual machine creates a PC register for each thread. At any time, a Java thread is always executing a method. This method is called the current method. If the current method is not For local methods, the PC register always executes the currently executing instruction. If it is a local method, the PC register is Underfined, and the register stores information such as the current execution environment pointer, program technologist, operation stack pointer, and calculated variable pointer.
  9. The core component of the virtual machine is the execution engine, which is responsible for executing the bytecode of the virtual machine. Generally, users first compile it into machine code and then execute it.

Concept difference between heap, stack and method area

Java heap

Heap memory is used to store objects and arrays created by new. The memory allocated in the heap is managed by the automatic garbage collector of the Java virtual machine. After an array or object is generated in the heap, a special variable can also be defined in the stack. The value of this variable is equal to the first address of the array or object in the heap memory. This special variable in the stack becomes After the reference variable of the array or object is obtained, the reference variable in the stack memory can be used in the program to access the array or object in the heap. The reference variable is equivalent to an alias or code name for the array or object.

Depending on the garbage collection mechanism, the Java heap may have different structures. The most common one is to divide the entire Java heap into
the new generation and the old generation. Among them, the new vocal cords store new-born objects or objects of young age, and the old age stores elderly objects.

The new generation is divided into den area, s0 area, and s1 area. s0 and s1 are also called from and to areas. They are two spaces of equal size and can interact with each other.

In most cases, the object is first allocated in the eden area. After the new generation is reclaimed, if the object is still alive, it will enter the s0 or s1 area, and then every time it passes

The new generation is recycled. If the object survives, its age is incremented by 1. After the object reaches a certain age, it enters the old generation.

insert image description here

Java stack

Java stack is a thread-private space, a stack, which generally consists of three parts: local variable table, operation data stack and frame
data
area The intermediate results are also used as temporary storage space for variables in the calculation process.
Frame data area: In addition to the local variable table and the operation data stack, the stack also needs some data to support the parsing of the constant pool. Here, the frame data area holds
the pointer to access the constant pool, which is convenient for the program to access the constant pool. In addition, when the function returns or When an exception occurs, the virtual machine must have an exception handling table, which is convenient to
find the abnormal code when sending an exception. Therefore, the exception handling table is also a part of the frame data area.

insert image description here

Java method area

The Java method area is the same as the heap. The method area is a memory area shared by all threads, which saves the class information of the system.
Such as class fields, methods, constant pools, etc. The size of the method area determines how many classes the system can hold. If the system
defines too many classes, the method area overflows. The virtual machine also throws out-of-memory errors. The method area can be understood
as the permanent area.

Virtual machine parameter configuration

What is virtual machine parameter configuration

During the running of the virtual machine, if the running state of the system can be tracked, it will be helpful for troubleshooting. For this reason, the virtual machine provides some parameters for tracking the system state, and executes Java using the given parameters. The virtual machine can print relevant logs when the system is running, which can be used to analyze actual problems. We configure virtual machine parameters, in fact, around the heap, stack, method area, configuration, and the most is about the parameter configuration of the new generation and the old generation in the heap memory,

Parameter configuration of the heap

-XX:+PrintGC Print related logs every time GC is triggered
-XX:+UseSerialGC Serial recycling
-XX:+PrintGCDetails More detailed GC logs-
Xms heap initial value
-Xmx heap maximum available value-
Xmn New generation heap maximum available The value
-XX:SurvivorRatio is used to set the ratio of eden space and from/to space in the new generation.
Include -XX:SurvivorRatio=eden/from=den/to
Summary: In practical work, we can directly change the initial heap size Equal to the maximum heap size,
the benefit of this is that it can reduce the number of garbage collections when the program is running, thereby improving efficiency.
-XX:SurvivorRatio is used to set the ratio of eden space and from/to space in the new generation.

set maximum heap memory

参数: -Xms5m -Xmx20m -XX:+PrintGCDetails -XX:+UseSerialGC -XX:+PrintCommandLineFlags

Set the new generation and old generation optimization parameters

-Xmn The size of the new generation, generally set to about 1/3 to 1/4 of the entire heap
-XX:SurvivorRatio Set the ratio between the eden area and the from/to space in the new generation n/1

Set the new generation scale parameter

参数: -Xms20m -Xmx20m -Xmn1m -XX:SurvivorRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC

Set new and old generation parameters

-Xms20m -Xmx20m -XX:SurvivorRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC
-Xms20m -Xmx20m -XX:SurvivorRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC
-XX:NewRatio= 2Summary
:different The heap distribution will have a certain impact on the system execution. In actual work, a reasonable configuration should be made according to the characteristics of the system. The basic strategy is to reserve objects in the new generation as much as possible and reduce the number of GCs in the old generation. In addition to setting the absolute size of the new generation (-Xmn), you can use (-XX:NewRatio) to set the ratio of the new generation to the old generation: -XX:NewRatio=old generation/new generation

memory overflow solution

Set heap memory size

Error reason: java.lang.OutOfMemoryError: Java heap space

Solution: set heap memory size -Xms1m -Xmx70m -XX:+HeapDumpOnOutOfMemoryError

Set stack memory size

Error reason: java.lang.StackOverflowError

Stack overflow occurs from recursive calls, and loop traversal is not possible, but recursive calls occur in loop methods, and stack overflows also occur.
Solution: Set the maximum call depth of the thread
- Xss5m sets the maximum call depth

Tomcat memory overflow modifies the JVM heap memory size in catalina.sh

JAVA_OPTS=“-server -Xms800m -Xmx800m -XX:PermSize=256m -XX:MaxPermSize=512m -XX:MaxNewSize=512m”

JVM parameter tuning summary

In the JVM startup parameters, you can set some parameter settings related to memory and garbage collection. By default, the JVM will work well without any settings, but some well-configured servers and specific applications must be carefully tuned to obtain best performance. By setting some goals we hope to achieve:

  1. GC time is small enough
  2. The number of GCs is low enough
  3. The cycle of full GC is long enough

The first two are currently contradictory. If the GC time is to be small, a smaller heap must be obtained. To ensure that the number of GCs is small enough, a larger heap must be guaranteed. We can only take a balance.

  1. For the setting of the JVM heap, the minimum and maximum values ​​can generally be limited by -Xms -Xmx. In order to prevent the garbage collector from shrinking the heap between the minimum and maximum values, we usually set the maximum and minimum values ​​to the same value.
  2. The young generation and the old generation will allocate heap memory according to the default ratio (1:2). You can adjust the size between the two by adjusting the ratio NewRadio between the two, or for the collection generation, such as the young generation, through -XX:newSize -XX:MaxNewSize to set its absolute size. Also, in order to prevent young generation heap shrinkage, we usually set -XX:newSize -XX:MaxNewSize to the same size
  3. How big is the young generation and the old generation to be considered reasonable? There is no doubt that this question of mine has no answer, otherwise there would be no tuning. Let's look at the impact of the change in the size of the two
  • Larger young generation will inevitably lead to smaller old generation, large young generation will prolong the normal GC cycle, but will increase the time of each GC; small old generation will lead to more frequent Full GC
  • Smaller young generation will inevitably lead to larger old generation, small young generation will lead to frequent normal GC, but each GC time will be shorter; large old generation will reduce the frequency of Full GC
  • How to choose should depend on the distribution of the application object life cycle: if the application has a large number of temporary objects, a larger young generation should be selected; if there are relatively many persistent objects, the old generation should be appropriately increased.

However, many applications do not have such obvious features. The following two points should be considered when making a decision: (A) Based on the principle of as few Full GC as possible, let the old generation cache common objects as much as possible. The default ratio of JVM is 1:2. (B) By observing the application for a period of time to see how much memory the old generation will occupy at the peak, increase the young generation according to the actual situation without affecting the Full GC. For example, the ratio can be controlled at 1:1. But at least 1/3 of the growth space should be reserved for the old generation

Guess you like

Origin blog.csdn.net/shuux666/article/details/124122392