JVM learning notes: JVM memory management and JVM garbage collection concepts. The JVM memory structure consists of heap, stack, local method stack, method area and other parts. In addition, JVM uses different garbage collection mechanisms for the download addresses of the new generation and the old generation respectively.
First, let’s take a look at the JVM memory structure, which is composed of heap, stack, local method stack, method area and other parts. The structure diagram is shown below.
JVM Learning Notes JVM Memory Management and JVM Garbage Collection
JVM memory structure
The JVM memory structure consists of heap, stack, local method stack, method area and other parts. The structure diagram is as follows:
1) Heap
The memory of all objects created by new is allocated in the heap, and their size can be controlled by -Xmx and -Xms. The heap is divided into the Ceponymous and the Old Generation, and the Ceponymous is further divided into the Eden and Survivor zones. Finally, Survivor is composed of FromSpace and ToSpace. The structure diagram is as follows:
A new generation. New objects are allocated memory by the new generation. When Eden is insufficient, the surviving objects will be transferred to Survivor. The size of the new generation can be controlled by -Xmn, or -XX:SurvivorRatio can be used to control the ratio of Eden and Survivor. Used to store objects that have survived multiple garbage collections in the new generation.
2) Stack
When each thread executes each method, it will apply for a stack frame in the stack. Each stack frame includes a local variable area and an operand stack, which is used to store temporary variables, parameters and intermediate results during this method call.
3) Local method stack
Used to support the execution of native methods, storing the status of each native method call
4) Method area
Stores the class information to be loaded, static variables, and final type constants, attributes and method information. JVM uses PermanetGeneration to store method areas, and the minimum and maximum values can be specified by -XX:PermSize and -XX:MaxPermSize. After introducing the JVM memory structure, let’s take a look at the download address of the JVM garbage collection mechanism.
JVM garbage collection mechanism
JVM adopts different garbage collection mechanisms for the new and old generations respectively
The new generation of GC:
The Ceponymous usually have a short survival time, so it is recycled based on the Copying algorithm. The so-called Copying algorithm is to scan out the surviving objects and copy them into a new completely unused space, corresponding to the Ceponymous, which is to copy between Eden and FromSpace or ToSpace. The new generation uses an idle pointer to control GC triggering. The pointer keeps the last allocated object in the new generation interval. When there is a new object to allocate memory, it is used to check whether the space is sufficient. If it is not enough, it will trigger GC. When objects are allocated continuously, the objects will gradually go from eden to survivor, and finally to the old generation.
Use javavisualVM to view it clearly and observe that after the new generation is full, the object will be transferred to the old generation, and then clear and continue to load. When the old generation is full, an outofmemory exception will be reported, as shown in the figure below:
In terms of execution mechanism, JVM provides serial GC (SerialGC), parallel recycling GC (ParallelScavenge) and parallel GC (ParNew)
1) Serial GC
The entire scanning and copying process is carried out in a single threaded way. It is suitable for applications with a single CPU, small space in the new generation and not very high requirements for pause time. It is the default GC method at the client level. It can be forced to specify it through -XX:+UseSerialGC
2) Parallel recycling of GC
The entire scanning and replication process is performed in a multi-threaded manner. It is suitable for multi-CPU and applications with short pause time requirements. It is the GC method used by the server level by default. It can be used to force the specification of -XX:+UseParallelGC, and use -XX:ParallelGCThreads=4 to specify the number of threads.
3) Parallel GC
Use with old-generation concurrent GC
Old Generation GC:
Different from the New Generation, objects survive for a long time and are relatively stable. Therefore, the Mark algorithm is used to recycle. The so-called mark means scanning out the surviving objects, and then recycling unmarked objects. After recycling, the empty space will either be merged or marked out for easy allocation next time. In short, it is necessary to reduce the efficiency loss caused by memory fragmentation. In terms of execution mechanism, JVM provides serial GC (SerialMSC), parallel GC (parallelMSC) and concurrent GC (CMS). The specific algorithm details need to be further studied.
The above GC mechanisms need to be used in combination, and the specified method is shown in the following table:
The above is all the contents of the in-depth understanding of the working principles of Java GC that the editor brings to you. I hope everyone will support Wulin.com more~