An article detailed JVM memory management and garbage collection

Whether for Java programmers or big data developers, JVM is one of the skills that must be mastered. It is not only a frequently asked question in interviews, but also the key to tuning programs and troubleshooting problems similar to memory overflow, stack overflow, and memory leak in actual business.

Here I have sorted out the documentation for each knowledge point module (microservices, databases, mysql, jvm, Redis, etc.) and the real interview questions from big factories. Friends in need can click the link below to get them for free

Link: 1103806531 Password: CSDN

Insert picture description here

This article mainly describes JVM memory management, direct memory, garbage collection and common garbage collection algorithms:

Runtime data area

When JVM executes some programs based on JVM operation, such as Java programs and Scala programs, it divides the memory it manages into multiple different data areas. These areas have various functions, creation and destruction times. Some areas depend on the start and end of the user thread's life cycle, and some areas exist with the start of the virtual machine. The following figure shows the data area division of the JVM at runtime :

1. Method area

The method area is a memory area shared by each thread. It is mainly used to store things that "will not change from beginning to end", such as final-defined constants, class information (class instances), static variables, etc., method information. Because these things are almost never GC once loaded, the method area is also called permanent generation (note that the two are not essentially equivalent).

There is a part of the method area called the constant pool, which is used to store some literal variables, symbol references and some constants (such as String constant pool) generated during compilation. The static area in the method area is used to store class variables, static blocks, etc.

The method area, also known as non-heap, has a size limit. If the memory used by the method area exceeds the allocated size, an error similar to OutOfMemory: PermGen Space will be reported.

2. Java virtual machine stack

The Java virtual machine stack is thread-private, and its life cycle is the same as that of a thread. It executes Java methods for the virtual machine, that is, bytecode services, and is a memory model that describes the execution of Java methods.

When each method is executed, a stack frame is created to store the local variable table (such as the basic data types known at compile time, object references, etc.), operation stack, dynamic link, method exit and other information. The process from which each method is called to the completion of execution corresponds to the process of a stack frame from pushing to popping in the virtual machine stack.

If the stack depth requested by the thread is greater than the depth allowed by the virtual machine, StackOverFlowError will be reported; if the virtual machine stack cannot apply for enough memory, it will report OutOfMemoryError.

The way to adjust the size of the virtual machine stack: -Xss.

3. Local method stack

The native method stack serves the native method used, and the native method interface will use a certain native method stack.

When a thread calls a Java method, the virtual machine creates a new stack frame and pushes it onto the Java stack. However, when it calls a local method, the virtual machine keeps the Java stack unchanged, and does not push a new stack frame in the thread's Java stack, but dynamically connects and directly calls the specified local method.

4. Heap

The heap is the largest area in the JVM management memory, shared by Java threads, mainly used to store new objects and arrays, and this area is created when the virtual machine is started. The heap can be in a logically contiguous but physically discontinuous memory space.

The heap is the main area managed by the garbage collector, which can be subdivided into the new generation and the old generation. The new generation is divided into the eden area, from survivor area, and to survivor area.

When an object is created, it is first allocated in the young generation. The eden area stores the newly generated objects. The two survivor areas are used to store the objects that survived each garbage collection in the young generation. But when the newly created object is very large, the object will directly enter the old age.

5. Program Counter

The program counter is thread-private, that is, each thread has its own program counter, which is used to record the bytecode position of the thread execution, and is an area without OOM.

Direct memory

Direct memory is not part of the JVM runtime data area. It is off-heap memory and will be frequently used. Therefore, set aside some physical memory when setting each memory range, otherwise it is easy to throw OutOfMemoryError.

Garbage collection

Garbage collection is GC, which is the process of memory recovery by JVM.

Developers pay more attention to the realization of business requirements, and memory management is done by JVM. If garbage collection is not performed or wrongly performed, the program will be unstable or even crash. The GC function provided by Java can automatically detect whether the object exceeds the scope, etc., so as to achieve the purpose of automatic memory recovery, which can effectively prevent memory leakage and effectively use available memory.

GC is mainly divided into three types: minor GC, major GC and full GC.

Minor GC occurs in the young generation, and major GC occurs in the old generation. For full GC, there are many reasons for starting, such as insufficient space in the old generation, it will start stop world, poor handling will often affect the stability of the entire program, and cause the system to be unavailable, which requires special attention.

Common garbage collection algorithms

1. Mark removal algorithm

First mark all objects that need to be recycled, and collect all marked objects uniformly after the marking is completed.

There are two disadvantages as follows:

  1. low efficiency

It is necessary to mark the objects to be recycled first, and then remove them uniformly. However, the two processes of marking and removing are very inefficient.

  1. Memory fragmentation problem

After the mark is cleared, a large number of discontinuous memory fragments will be generated. Too much space fragmentation may lead to the failure to find enough contiguous memory when larger objects need to be allocated during the program running, and another garbage collection action has to be triggered in advance. performance.

2. Copy Algorithm

First divide the available memory into two pieces of equal size according to the capacity, and use only one piece at a time. When the used memory is used up, the surviving objects are copied to another, and then the used memory space is cleaned up again.

Advantages : In this way, the entire half-area is reclaimed every time, and there is no need to consider complex situations such as memory fragmentation during memory allocation. As long as the pointer on the top of the heap is moved, the memory can be allocated in order, which is simple to implement and efficient.

Disadvantages : Not suitable for scenes with high object survival rate, because this kind of scene requires more copy operations to affect efficiency; the actual available memory becomes half of the allocated memory, because only half of the memory is used each time.

3. Marking and sorting algorithm

Mark first (the marking process is the same as the mark removal algorithm), so that all surviving objects move to one end, and then directly clean up the memory outside the end boundary. This can solve the memory fragmentation problem.

4. Generational collection algorithm

It is to adopt different garbage collection algorithms for the new generation and old generation in the Java heap memory. For example, in the new generation, often only a small number of objects survive (and eventually enter the old generation), it is suitable to use the replication algorithm. In the old age, the survival rate of objects is higher, and there is no extra space to guarantee it, so the mark removal algorithm is used.

Of course, in actual applications, the algorithm used depends on the garbage collector used.

At last

Friends who need more knowledge points, interview materials, and real interview questions can click the link below to get it for free

Link: 1103806531 Password: CSDN

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_48655626/article/details/109312135