An overview,

1, concept,

The JVM divides heap memory into young generation, old generation, and persistent generation, depending on the object lifetime. The persistent generation mainly stores the class information of Java classes and has little to do with the Java objects to be collected by garbage collection. The division of the young generation and the old generation has a greater impact on garbage collection.

2. Generational reasons

Heap memory, which holds object instances, is the largest chunk of memory managed by the JVM and the most frequently garbage collected. Generational is to improve the efficiency of object memory allocation and garbage collection.

Heap and non-heap memory

The heap is created when the Java virtual machine is started. The memory outside of the heap in the JVM is called non-heap memory. You can see that the JVM manages two main types of memory: heap and non-heap. Simply put, the heap is the memory that is accessible to Java code and is reserved for developers. Non-heap is what the JVM keeps for itself.

Second, the specific generation principle

1. The younger generation

All newly generated objects are first placed in the young generation. The goal of the younger generation is to collect objects with short life spans as quickly as possible. The younger generation is divided into three sections. One Eden zone and two Survivor zones (generally). Most objects are generated in the Eden zone. When Eden is full, surviving objects will be copied to one of the two survivable objects in SurvivorA. When SurvivorA is full, surviving objects in SurvivorA will be copied to another SurvivorB. When SurvivorA is full, survivable objects will be copied to another SurvivorB. Objects that are still alive when copied from the first SurvivorA will be copied to “Tenured”. It should be noted that the two Survivor zones are symmetric and have no sequence relationship, so there may be objects copied from Eden and the object copied from the previous Survivor in the same zone, while only the object copied from the first Survivor to the old zone. Also, one of the Survivor zones is always empty. At the same time, Survivor zones can be configured to be multiple (more than two) depending on program requirements, which increases the duration of an object in the young generation and reduces the likelihood of it being placed in the old generation.

Default Young Generation: Old generation = 1:2

One Eden space and two Survivor Spaces (SurvivorA, SurvivorB)

Default new generation space allocation: Eden: SurvivorA: SurvivorB = 8:1:1

2. The old days

Objects that survive n garbage collections (this n is called the age threshold and the default is 15). The composition of the old age space is actually very simple, it is not like the new generation of space as divided into several regions, it has only one region, the storage of the object is not like the new generation of space most of the objects are morning news, overnight death. Almost all of the objects here have survived Survivor Spaces, and they won’t easily be taken by dogs. As a result, Full (Major) GC does not occur as frequently as Scanvage (Minor) GC, and it takes about 10 times longer to do a Full GC than a Scanvage GC.

3, durable generation

After JDK1.8, it becomes a meta-space, which no longer takes up space in the heap. The persistent generation creates space in the heap for storing static files, class information, etc., and generally does not GC.