JVM memory Composition
The JVM stack consists of the heap, stack, local method stack, and method zone. The structure is shown as follows:
1) Heap
The memory of all objects created through new is allocated in the heap, and their size can be controlled through-xmx and-XMS. The heap is divided into the new generation and the old generation, and the new generation is further divided into the Eden and elastic vor areas. Finally, the elastic vor consists of the from space and to space. The structure is shown below:
- New generation. The newly created objects are allocated memory by the new generation. When the Eden space is insufficient, the surviving objects are transferred to the same vor. The new generation size can be controlled by-xmn, or-XX: limit vorratio to control the ratio of Eden to limit vor
- Old Generation. Used to store objects that are still alive after repeated garbage collection in the new generation.
2) Stack
Each thread applies for a stack frame in the stack when executing each method. Each stack frame includes the local variable zone and the operand stack, used to store temporary variables, parameters, and intermediate results in the method call process.
3) Local method Stack
It is used to support native method execution and stores the status of each native method call.
4) Method Area
Stores information about the classes to be loaded, static variables, final constants, attributes, and methods. JVM uses permanet generation to store the method area. You can use-XX: permsize and-XX: maxpermsize to specify the minimum and maximum values.
Garbage Collection Mechanism
JVM uses different garbage collection mechanisms for the new generation and old generation respectively.
GC of the new generation:
The new generation usually has a short survival time, so it is recycled Based on the copying algorithm. The so-called copying algorithm is to scan the surviving objects and copy them to a new space that is completely unused, corresponding to the new generation, that is, copy between Eden and from space or to space. The new generation uses the idle pointer Method to Control GC triggering. the pointer keeps the last allocated object in the new generation interval. When a new object needs to be allocated memory, it is used to check whether the space is sufficient, if not, GC is triggered. When objects are continuously allocated, the objects gradually change from Eden to movie vor, and finally to the old generation,
You can use Java visualvm to check whether the new generation is full. After the new generation is full, the object is transferred to the old generation, and then the old generation is full, an outofmemory exception is reported, as shown in:
On the execution mechanism, JVM provides the serial GC (Serial GC), parallel GC (parallel scavenge), and parallel GC (parnew)
1) Serial GC
The whole scanning and replication process is carried out in a single thread mode. It is applicable to applications with a single CPU, small new generation space, and not very demanding on the pause time. It is the default GC method at the client level, you can use-XX: + useserialgc to forcibly specify
2) Parallel GC recovery
The entire scanning and replication process adopts the multi-thread method. This method is applicable to applications with multiple CPUs and short pause time requirements. It is the default GC method at the server level.-XX is available: + useparallelgc is used to forcibly specify the number of threads.-XX: parallelgcthreads = 4 is used to specify the number of threads.
3) Parallel GC
Used in combination with the concurrent GC of the old generation
GC of the old generation:
Unlike the new generation, the old generation and the new generation have a long and stable object survival time. Therefore, the mark algorithm is used for collection. The so-called mark is to scan the surviving object, then, the unmarked objects are recycled. After the unmarked objects are recycled, the empty space is either merged or marked to facilitate the next allocation. In short, the efficiency loss caused by memory fragments is reduced. On the execution mechanism, JVM provides serial GC (Serial MSC), parallel GC (parallel MSC), and concurrent GC (CMS). Details of specific algorithms need to be further studied.
The preceding GC mechanisms need to be used in combination. The following table lists the specific methods:
Method |
New Generation GC |
GC method of the old generation |
-XX: + useserialgc |
Serial GC |
Serial GC |
-XX: + useparallelgc |
Parallel GC recovery |
Parallel GC |
-XX: + useconemarksweepgc |
Parallel GC |
Concurrent GC |
-XX: + useparnewgc |
Parallel GC |
Serial GC |
-XX: + useparalleloldgc |
Parallel GC recovery |
Parallel GC |
-XX: + useconemarksweepgc -XX: + useparnewgc |
Serial GC |
Concurrent GC |
Unsupported combination |
1.-XX: + useparnewgc-XX: + useparalleloldgc 2.-XX: + useparnewgc-XX: + useserialgc |
JVM memory composition and garbage collection