Analysis of JVM garbage collection mechanism and jvm garbage collection
First, we need to know that Java's memory allocation and collection are all automatically completed by the JVM garbage collection mechanism. Each JVM implementation may adopt different methods to implement the garbage collection mechanism. Before SUN was acquired, Oracle used JRockit JVM and HotSpot JVM. Currently, Oracle has two JVM implementations. After a while, the two JVM implementations are merged into one. HotSpot JVM is currently part of the standard core components of the Oracle SE platform. The garbage collection mechanism discussed on the market is based on the HotSpot JVM by default. OK. Let's start with the question. Let's first look at the JVM memory region model,
This figure intuitively shows the JVM memory partition. Where is the GC we are concerned about? The answer is shown in the figure below.
Comparing the two figures, we found that the garbage collector and the jit compiler constitute the JVM execution engine. Where is the gc's "sphere of influence? See
I emphasize that this is the heap model of hotspot. Other JVM (such as JRockit and IBM J9) do not have the concept of permanent generation. What is interesting is that in Java 8, hotspot will also be permanently canceled, changed to metaSpace, and uses local memory directly, which reduces the heap pressure.
Now let's take a look at the heap partitions before JDK 8, which is more widely used:
1. Young Generation: the vast majority of newly created objects will be allocated here. Because most objects become inaccessible soon after creation, many objects are created in the new Generation, then disappear. The process in which the object disappears from this region is called "minor GC".
Eden space (Eden space, any instance enters the runtime memory area through Eden space)
S0 subnet vor space (S0 subnet vor space, an instance that has been in existence for a long time will be moved from Eden space to S0 subnet vor space)
S1 Survivor space (instances with a longer period of time will be moved from the S0 slave vor space to the S1 slave vor space)
2. Old generation: objects that survive the new generation will be copied here. It occupies more space than the new generation. Due to its relatively large space, GC occurs much less in the old age than in the new generation. The process in which an object disappears from the old age is called "major GC" (or "full GC").
Instances in S1 will be upgraded to the Old Generation if the survival time is long enough ).
3. Permanent Generation contains metadata of classes, methods, and other details, that is, the method area in the JVM memory structure, which is used to save class constants and string constants. Therefore, this region is not used to permanently store objects that survive the old age. GC may also occur in this region. The GC events that occur in this region are also considered as major GC. As hotspot extends gc to the method area, it abstracts the method area into permanent generation as the logic structure of the heap. Additionally, the well-known String constant pool was moved from permanent generation to serious heap in the permanent generation before JDK7. After all, the permanent generation is not actually in the heap. hotspot only has the figure above to expand the GC sphere of influence.
Next let's take a look at the GC process of the new generation:
The new generation is used to save the objects created for the first time. It can be divided into three spaces.
An Eden Space)
Two Survivor Spaces)
There are a total of three spaces, including two survivor spaces. The execution sequence of each space is as follows:
1. Most newly created objects are stored in the Eden space.
2. After performing the first GC in the Eden space, the surviving objects are moved to one of the survivor spaces.
3. After GC is executed in the Eden space, the surviving objects will be stacked in the same survivor space.
4. When a survivor's space is saturated, the surviving object will be moved to another survivor's space. The saturated survivor space will be cleared later.
5. objects that remain alive several times in the preceding steps will be moved to the old age.
If you carefully observe these steps, you will find that one of the survivor spaces must be empty. If both survivor spaces have data, or both spaces are empty, it must mark an error in your system.